diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e7e5760..c3a66f7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Added +- Added an `InstanceofStrategy` which detects usages in `instanceof` expressions [#100](https://github.com/composer-unused/composer-unused/pull/100) ### Fixed - Fixed an issue where `ext-ds` classes where not recognized as used [#88](https://github.com/composer-unused/composer-unused/pull/88) - Fixed an issue where `extends` and `implements` of FQN was not markes as used [#90](https://github.com/composer-unused/composer-unused/pull/90) diff --git a/src/Parser/PHP/Factory/NodeVisitorFactory.php b/src/Parser/PHP/Factory/NodeVisitorFactory.php index 64a5af7f..707341cc 100644 --- a/src/Parser/PHP/Factory/NodeVisitorFactory.php +++ b/src/Parser/PHP/Factory/NodeVisitorFactory.php @@ -9,6 +9,7 @@ use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ClassConstStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ExtendsParseStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ImplementsParseStrategy; +use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\InstanceofStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\NewParseStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\PhpExtensionStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\StaticParseStrategy; @@ -31,6 +32,7 @@ public function __invoke(ContainerInterface $container): NodeVisitor ), new ExtendsParseStrategy(), new ImplementsParseStrategy(), + new InstanceofStrategy(), ], $container->get(ErrorHandlerInterface::class)); } } diff --git a/src/Parser/PHP/Strategy/InstanceofStrategy.php b/src/Parser/PHP/Strategy/InstanceofStrategy.php new file mode 100644 index 00000000..b6329934 --- /dev/null +++ b/src/Parser/PHP/Strategy/InstanceofStrategy.php @@ -0,0 +1,37 @@ +class instanceof Node\Name) { + return false; + } + + return $node->class->isFullyQualified() || $node->class->isQualified(); + } + + /** + * @param Node&Node\Expr\Instanceof_ $node + * @return array + */ + public function extractNamespaces(Node $node): array + { + /** @var Node\Name $class */ + $class = $node->class; + + return [ + $class->toString(), + ]; + } +} diff --git a/tests/Integration/Parser/PHP/NodeVisitorTest.php b/tests/Integration/Parser/PHP/NodeVisitorTest.php index c589eee9..3f83a058 100644 --- a/tests/Integration/Parser/PHP/NodeVisitorTest.php +++ b/tests/Integration/Parser/PHP/NodeVisitorTest.php @@ -10,6 +10,7 @@ use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ClassConstStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ExtendsParseStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ImplementsParseStrategy; +use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\InstanceofStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\NewParseStrategy; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\ParseStrategyInterface; use Icanhazstring\Composer\Unused\Parser\PHP\Strategy\PhpExtensionStrategy; @@ -177,7 +178,15 @@ public function itShouldParseUsagesDataProvider(): array ], 'inputFile' => ASSET_DIR . '/TestFiles/ClassImplements.php', 'strategy' => new ImplementsParseStrategy() - ] + ], + 'Instanceof' => [ + 'expectedUseNamespaces' => [ + 'Foo\Bar\Qux', + 'Foo\Bar\Quz', + ], + 'inputFile' => ASSET_DIR . '/TestFiles/Instanceof.php', + 'strategy' => new InstanceofStrategy(), + ], ]; } diff --git a/tests/assets/TestFiles/Instanceof.php b/tests/assets/TestFiles/Instanceof.php new file mode 100644 index 00000000..4af6edee --- /dev/null +++ b/tests/assets/TestFiles/Instanceof.php @@ -0,0 +1,33 @@ +