• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orisai / phpstan-nette / 43698240932

15 Nov 2026 05:30AM UTC coverage: 78.278% (-0.9%) from 79.16%
43698240932

push

github

mabar
Record the measured cost of the reflection cache bypass

21744 of 27778 relevant lines covered (78.28%)

84.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/Dic/Type/HasServiceTypeSpecifyingExtension.php
1
<?php declare(strict_types = 1);
2

3
namespace OriPhpstan\Nette\Dic\Type;
4

5
use Nette\DI\Container;
6
use OriPhpstan\Nette\Configuration\ConfigurationGuard;
7
use OriPhpstan\Nette\Dic\Metadata\MultiContainerRegistry;
8
use PhpParser\Node\Expr\MethodCall;
9
use PHPStan\Analyser\Scope;
10
use PHPStan\Analyser\SpecifiedTypes;
11
use PHPStan\Analyser\TypeSpecifier;
12
use PHPStan\Analyser\TypeSpecifierAwareExtension;
13
use PHPStan\Analyser\TypeSpecifierContext;
14
use PHPStan\Reflection\MethodReflection;
15
use PHPStan\Type\Accessory\HasMethodType;
16
use PHPStan\Type\MethodTypeSpecifyingExtension;
17
use PHPStan\Type\TypeCombinator;
18
use function array_merge;
19
use function strtolower;
20

21
final class HasServiceTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
22
{
23

24
        private ConfigurationGuard $guard;
25

26
        private MultiContainerRegistry $registry;
27

28
        private TypeSpecifier $typeSpecifier;
29

30
        public function __construct(ConfigurationGuard $guard, MultiContainerRegistry $registry)
31
        {
32
                $this->guard = $guard;
36 ✔
33
                $this->registry = $registry;
36 ✔
34
        }
35

36
        public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
37
        {
38
                $this->typeSpecifier = $typeSpecifier;
36 ✔
39
        }
40

41
        public function getClass(): string
42
        {
43
                return Container::class;
21 ✔
44
        }
45

46
        public function isMethodSupported(
47
                MethodReflection $methodReflection,
48
                MethodCall $node,
49
                TypeSpecifierContext $context
50
        ): bool
51
        {
52
                $this->guard->validate();
24 ✔
53

54
                return $this->registry->isActive()
24 ✔
55
                        && ($context->true() || $context->false())
24 ✔
56
                        && strtolower($methodReflection->getName()) === 'hasservice'
24 ✔
57
                        && !$node->isFirstClassCallable()
24 ✔
58
                        && $node->getArgs() !== [];
24 ✔
59
        }
60

61
        public function specifyTypes(
62
                MethodReflection $methodReflection,
63
                MethodCall $node,
64
                Scope $scope,
65
                TypeSpecifierContext $context
66
        ): SpecifiedTypes
67
        {
68
                $receiverType = $scope->getType($node->var);
8 ✔
69

70
                // Narrowing is only meaningful for base-class receivers; a known-class receiver
71
                // answers existence exactly and an unknown container has an unknown service universe.
72
                $resolution = $this->registry->resolveProfiles($receiverType, $scope);
8 ✔
73

74
                if ($resolution === null || !$resolution->isBase()) {
8 ✔
75
                        return new SpecifiedTypes([], []);
2 ✔
76
                }
77

78
                $profiles = $resolution->getProfiles();
6 ✔
79

80
                $constantStrings = $scope->getType($node->getArgs()[0]->value)->getConstantStrings();
6 ✔
81

82
                if ($constantStrings === []) {
6 ✔
83
                        return new SpecifiedTypes([], []);
×
84
                }
85

86
                $methodName = null;
6 ✔
87

88
                foreach ($constantStrings as $constantString) {
6 ✔
89
                        $resolved = $this->registry->getResolvedServiceMethodName(
6 ✔
90
                                $constantString->getValue(),
6 ✔
91
                                false,
6 ✔
92
                                $profiles,
6 ✔
93
                                $scope,
6 ✔
94
                        );
6 ✔
95

96
                        if ($resolved === null) {
6 ✔
97
                                return new SpecifiedTypes([], []);
×
98
                        }
99

100
                        if ($methodName === null) {
6 ✔
101
                                $methodName = $resolved;
6 ✔
102
                        } elseif ($methodName !== $resolved) {
×
103
                                return new SpecifiedTypes([], []);
×
104
                        }
105
                }
106

107
                if ($context->true()) {
6 ✔
108
                        return $this->typeSpecifier->create(
6 ✔
109
                                $node->var,
6 ✔
110
                                TypeCombinator::intersect($receiverType, new HasMethodType($methodName)),
6 ✔
111
                                $context,
6 ✔
112
                                $scope,
6 ✔
113
                        );
6 ✔
114
                }
115

116
                $missingMethodNames = [$methodName];
6 ✔
117

118
                if ($receiverType instanceof ContainerMissingServicesType) {
6 ✔
119
                        $missingMethodNames = array_merge($receiverType->getMissingMethodNames(), $missingMethodNames);
4 ✔
120
                }
121

122
                return $this->typeSpecifier->create(
6 ✔
123
                        $node->var,
6 ✔
124
                        new ContainerMissingServicesType($missingMethodNames),
6 ✔
125
                        TypeSpecifierContext::createTruthy(),
6 ✔
126
                        $scope,
6 ✔
127
                );
6 ✔
128
        }
129

130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc