|
14 | 14 | use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
|
15 | 15 | use PHPStan\DependencyInjection\Container;
|
16 | 16 | use PHPStan\DependencyInjection\ParameterNotFoundException;
|
| 17 | +use PHPStan\Node\InClassMethodNode; |
17 | 18 | use PHPStan\Node\InClassNode;
|
18 | 19 | use PHPStan\Reflection\ClassReflection;
|
19 | 20 | use PHPStan\Reflection\ExtendedMethodReflection;
|
|
26 | 27 | use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantUsage;
|
27 | 28 | use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef;
|
28 | 29 | use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage;
|
| 30 | +use ShipMonk\PHPStan\DeadCode\Graph\UsageOriginDetector; |
29 | 31 | use SimpleXMLElement;
|
30 | 32 | use SplFileInfo;
|
31 | 33 | use UnexpectedValueException;
|
@@ -62,12 +64,16 @@ class SymfonyUsageProvider implements MemberUsageProvider
|
62 | 64 | */
|
63 | 65 | private array $dicConstants = [];
|
64 | 66 |
|
| 67 | + private UsageOriginDetector $usageOriginDetector; |
| 68 | + |
65 | 69 | public function __construct(
|
66 | 70 | Container $container,
|
| 71 | + UsageOriginDetector $usageOriginDetector, |
67 | 72 | ?bool $enabled,
|
68 | 73 | ?string $configDir
|
69 | 74 | )
|
70 | 75 | {
|
| 76 | + $this->usageOriginDetector = $usageOriginDetector; |
71 | 77 | $this->enabled = $enabled ?? $this->isSymfonyInstalled();
|
72 | 78 | $resolvedConfigDir = $configDir ?? $this->autodetectConfigDir();
|
73 | 79 | $containerXmlPath = $this->getContainerXmlPath($container);
|
@@ -97,6 +103,13 @@ public function getUsages(Node $node, Scope $scope): array
|
97 | 103 | ];
|
98 | 104 | }
|
99 | 105 |
|
| 106 | + if ($node instanceof InClassMethodNode) { // @phpstan-ignore phpstanApi.instanceofAssumption |
| 107 | + $usages = [ |
| 108 | + ...$usages, |
| 109 | + ...$this->getMethodUsagesFromAttributeReflection($node, $scope), |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
100 | 113 | if ($node instanceof Return_) {
|
101 | 114 | $usages = [
|
102 | 115 | ...$usages,
|
@@ -216,6 +229,75 @@ private function getMethodUsagesFromReflection(InClassNode $node): array
|
216 | 229 | return $usages;
|
217 | 230 | }
|
218 | 231 |
|
| 232 | + /** |
| 233 | + * @return list<ClassMethodUsage> |
| 234 | + */ |
| 235 | + private function getMethodUsagesFromAttributeReflection(InClassMethodNode $node, Scope $scope): array |
| 236 | + { |
| 237 | + $usages = []; |
| 238 | + |
| 239 | + foreach ($node->getMethodReflection()->getParameters() as $parameter) { |
| 240 | + foreach ($parameter->getAttributes() as $attributeReflection) { |
| 241 | + if ($attributeReflection->getName() === 'Symfony\Component\DependencyInjection\Attribute\AutowireLocator') { |
| 242 | + $arguments = $attributeReflection->getArgumentTypes(); |
| 243 | + |
| 244 | + if (!isset($arguments['services']) || !isset($arguments['defaultIndexMethod'])) { |
| 245 | + continue; |
| 246 | + } |
| 247 | + |
| 248 | + if ($arguments['services']->isArray()->yes()) { |
| 249 | + $classNames = $arguments['services']->getIterableValueType()->getConstantStrings(); |
| 250 | + } else { |
| 251 | + $classNames = $arguments['services']->getConstantStrings(); |
| 252 | + } |
| 253 | + |
| 254 | + $defaultIndexMethod = $arguments['defaultIndexMethod']->getConstantStrings(); |
| 255 | + |
| 256 | + if ($classNames === [] || !isset($defaultIndexMethod[0])) { |
| 257 | + continue; |
| 258 | + } |
| 259 | + |
| 260 | + foreach ($classNames as $className) { |
| 261 | + $usages[] = new ClassMethodUsage( |
| 262 | + $this->usageOriginDetector->detectOrigin($scope), |
| 263 | + new ClassMethodRef( |
| 264 | + $className->getValue(), |
| 265 | + $defaultIndexMethod[0]->getValue(), |
| 266 | + true, |
| 267 | + ), |
| 268 | + ); |
| 269 | + } |
| 270 | + } elseif ($attributeReflection->getName() === 'Symfony\Component\DependencyInjection\Attribute\AutowireIterator') { |
| 271 | + $arguments = $attributeReflection->getArgumentTypes(); |
| 272 | + |
| 273 | + if (!isset($arguments['tag']) || !isset($arguments['defaultIndexMethod'])) { |
| 274 | + continue; |
| 275 | + } |
| 276 | + |
| 277 | + $classNames = $arguments['tag']->getConstantStrings(); |
| 278 | + $defaultIndexMethod = $arguments['defaultIndexMethod']->getConstantStrings(); |
| 279 | + |
| 280 | + if ($classNames === [] || !isset($defaultIndexMethod[0])) { |
| 281 | + continue; |
| 282 | + } |
| 283 | + |
| 284 | + foreach ($classNames as $className) { |
| 285 | + $usages[] = new ClassMethodUsage( |
| 286 | + $this->usageOriginDetector->detectOrigin($scope), |
| 287 | + new ClassMethodRef( |
| 288 | + $className->getValue(), |
| 289 | + $defaultIndexMethod[0]->getValue(), |
| 290 | + true, |
| 291 | + ), |
| 292 | + ); |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + return $usages; |
| 299 | + } |
| 300 | + |
219 | 301 | protected function shouldMarkAsUsed(ReflectionMethod $method): bool
|
220 | 302 | {
|
221 | 303 | return $this->isBundleConstructor($method)
|
@@ -395,7 +477,8 @@ private function isSymfonyInstalled(): bool
|
395 | 477 | || InstalledVersions::isInstalled('symfony/routing')
|
396 | 478 | || InstalledVersions::isInstalled('symfony/contracts')
|
397 | 479 | || InstalledVersions::isInstalled('symfony/console')
|
398 |
| - || InstalledVersions::isInstalled('symfony/http-kernel'); |
| 480 | + || InstalledVersions::isInstalled('symfony/http-kernel') |
| 481 | + || InstalledVersions::isInstalled('symfony/dependency-injection'); |
399 | 482 | }
|
400 | 483 |
|
401 | 484 | private function createUsage(ExtendedMethodReflection $methodReflection): ClassMethodUsage
|
|
0 commit comments