From f8c204adba7515823ad213f30d541bf87e921177 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 23 May 2022 00:34:31 +0700 Subject: [PATCH] [Naming] Handle Grouped use import on UseImportsResolver (#2348) Co-authored-by: Dominik Ritter Co-authored-by: GitHub Action --- .../Fixture/group_use.php.inc | 13 ++++ .../Fixture/group_use_aliased.php.inc | 14 ++++ .../UseImportsResolver/Source/FirstClass.php | 8 +++ .../UseImportsResolver/Source/OtherClass.php | 8 +++ .../UseImportsResolver/Source/SecondClass.php | 8 +++ .../UseImportsResolverTest.php | 64 +++++++++++++++++++ rules/Naming/Naming/UseImportsResolver.php | 29 ++++++++- 7 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use.php.inc create mode 100644 rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use_aliased.php.inc create mode 100644 rules-tests/Naming/Naming/UseImportsResolver/Source/FirstClass.php create mode 100644 rules-tests/Naming/Naming/UseImportsResolver/Source/OtherClass.php create mode 100644 rules-tests/Naming/Naming/UseImportsResolver/Source/SecondClass.php create mode 100644 rules-tests/Naming/Naming/UseImportsResolver/UseImportsResolverTest.php 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; } }