• 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

93.33
/src/Dic/DeadCode/DicUsageProvider.php
1
<?php declare(strict_types = 1);
2

3
namespace OriPhpstan\Nette\Dic\DeadCode;
4

5
use OriPhpstan\Nette\Dic\Metadata\MultiContainerRegistry;
6
use PhpParser\Node;
7
use PHPStan\Analyser\DependencyTracker;
8
use PHPStan\Analyser\Scope;
9
use PHPStan\Node\InClassNode;
10
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef;
11
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage;
12
use ShipMonk\PHPStan\DeadCode\Graph\UsageOrigin;
13
use ShipMonk\PHPStan\DeadCode\Provider\MemberUsageProvider;
14
use ShipMonk\PHPStan\DeadCode\Provider\VirtualUsageData;
15
use function assert;
16
use function strtolower;
17

18
final class DicUsageProvider implements MemberUsageProvider
19
{
20

21
        private const Note = 'Called by compiled Nette DI container';
22

23
        private MultiContainerRegistry $registry;
24

25
        private ContainerUsageExtractor $extractor;
26

27
        /** @var array<string, array<string, true>>|null */
28
        private ?array $usages = null;
29

30
        public function __construct(MultiContainerRegistry $registry, ContainerUsageExtractor $extractor)
31
        {
32
                $this->registry = $registry;
10 ✔
33
                $this->extractor = $extractor;
10 ✔
34
        }
35

36
        /**
37
         * The usage is emitted against the class the container CONSTRUCTS, never against the class that
38
         * declares the called method: `new Helper` reaches `HelperBase::__construct` through a hierarchy
39
         * edge that lives in a third file, and nothing salts it - the compiled $wiring carries the
40
         * ancestors of registered SERVICES only, so a class the container merely constructs can change its
41
         * `extends` clause without moving one container byte. Resolving the hop here would mean caching
42
         * HelperBase.php's verdict against Helper.php's contents, which PHPStan requeues in the opposite
43
         * direction. Emitted this way, the hop is resolved by the dead-code detector's own aggregate stage
44
         * from collected class definitions, and the only cross-file read left - the analysed class's own
45
         * inherited members - runs along the descendant-to-ancestor edge PHPStan already tracks.
46
         *
47
         * @return list<ClassMethodUsage>
48
         */
49
        public function getUsages(Node $node, Scope $scope): array
50
        {
51
                if (!$node instanceof InClassNode) { // @phpstan-ignore phpstanApi.instanceofAssumption
10 ✔
52
                        return [];
×
53
                }
54

55
                if (!$this->registry->isActive()) {
10 ✔
56
                        return [];
2 ✔
57
                }
58

59
                $classReflection = $node->getClassReflection();
8 ✔
60
                $className = $classReflection->getName();
8 ✔
61
                // shipmonk's collector passes its own analysis scope through under the plain Scope type.
62
                assert($scope instanceof DependencyTracker);
8 ✔
63
                $scope->trackValueDependency(DicUsageValueExtension::class, $className);
8 ✔
64
                $calledMethodNames = $this->calledMethodNames($className);
8 ✔
65

66
                if ($calledMethodNames === []) {
8 ✔
67
                        return [];
2 ✔
68
                }
69

70
                $usages = [];
6 ✔
71

72
                foreach ($classReflection->getNativeReflection()->getMethods() as $method) {
6 ✔
73
                        if (!isset($calledMethodNames[strtolower($method->getName())])) {
4 ✔
74
                                continue;
×
75
                        }
76

77
                        $usages[] = new ClassMethodUsage(
4 ✔
78
                                UsageOrigin::createVirtual($this, VirtualUsageData::withNote(self::Note)),
4 ✔
79
                                new ClassMethodRef($className, $method->getName(), false),
4 ✔
80
                        );
4 ✔
81
                }
82

83
                return $usages;
6 ✔
84
        }
85

86
        /**
87
         * @return array<string, true>
88
         */
89
        public function calledMethodNames(string $className): array
90
        {
91
                return $this->resolveUsages()[$className] ?? [];
8 ✔
92
        }
93

94
        /**
95
         * @return array<string, array<string, true>>
96
         */
97
        private function resolveUsages(): array
98
        {
99
                if ($this->usages !== null) {
8 ✔
100
                        return $this->usages;
2 ✔
101
                }
102

103
                $usages = [];
8 ✔
104

105
                foreach ($this->registry->getContainerFilePaths() as $file) {
8 ✔
106
                        foreach ($this->extractor->extractFromFile($file) as $class => $methods) {
8 ✔
107
                                $usages[$class] = ($usages[$class] ?? []) + $methods;
8 ✔
108
                        }
109
                }
110

111
                return $this->usages = $usages;
8 ✔
112
        }
113

114
}
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