diff --git a/rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use.php.inc b/rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use.php.inc new file mode 100644 index 000000000000..8ebd63f478c2 --- /dev/null +++ b/rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use.php.inc @@ -0,0 +1,13 @@ +boot(); + $this->useImportsResolver = $this->getService(UseImportsResolver::class); + $this->testingParser = $this->getService(TestingParser::class); + $this->nodeFinder = $this->getService(BetterNodeFinder::class); + } + + /** + * @dataProvider provideData() + */ + public function testUsesFromProperty(SmartFileInfo $file): void + { + $nodes = $this->testingParser->parseFileToDecoratedNodes($file->getRelativeFilePath()); + + $firstProperty = $this->nodeFinder->findFirstInstanceOf($nodes, Property::class); + $resolvedUses = $this->useImportsResolver->resolveForNode($firstProperty); + + $stringUses = []; + + foreach ($resolvedUses as $resolvedUse) { + foreach ($resolvedUse->uses as $useUse) { + $stringUses[] = $useUse->name->tostring(); + } + } + + $this->assertContains(FirstClass::class, $stringUses); + $this->assertContains(SecondClass::class, $stringUses); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + $directory = __DIR__ . '/Fixture'; + return StaticFixtureFinder::yieldDirectoryExclusively($directory); + } +} diff --git a/rules/Naming/Naming/UseImportsResolver.php b/rules/Naming/Naming/UseImportsResolver.php index fbc208773877..11666d14a9db 100644 --- a/rules/Naming/Naming/UseImportsResolver.php +++ b/rules/Naming/Naming/UseImportsResolver.php @@ -5,9 +5,11 @@ namespace Rector\Naming\Naming; use PhpParser\Node; -use PhpParser\Node\Stmt; +use PhpParser\Node\Name; +use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Use_; +use PhpParser\Node\Stmt\UseUse; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; @@ -31,6 +33,29 @@ public function resolveForNode(Node $node): array return []; } - return array_filter($namespace->stmts, fn (Stmt $stmt): bool => $stmt instanceof Use_); + $collectedUses = []; + + foreach ($namespace->stmts as $stmt) { + if ($stmt instanceof Use_) { + $collectedUses[] = $stmt; + continue; + } + + if ($stmt instanceof GroupUse) { + $groupUseUses = []; + foreach ($stmt->uses as $useUse) { + $groupUseUses[] = new UseUse( + new Name($stmt->prefix . '\\' . $useUse->name), + $useUse->alias, + $useUse->type, + $useUse->getAttributes() + ); + } + + $collectedUses[] = new Use_($groupUseUses, $stmt->type, $stmt->getAttributes()); + } + } + + return $collectedUses; } }