-
-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Naming] Handle Grouped use import on UseImportsResolver (#2348)
Co-authored-by: Dominik Ritter <dritter03@googlemail.com> Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
a9c6d9b
commit f8c204a
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Naming\Naming\UseImportsResolver\Fixture; | ||
|
||
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\OtherClass; | ||
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\{FirstClass, SecondClass}; | ||
|
||
class MyGroupUse1 | ||
{ | ||
private FirstClass $firstClass; | ||
private SecondClass $secondClass; | ||
private OtherClass $otherClass; | ||
} |
14 changes: 14 additions & 0 deletions
14
rules-tests/Naming/Naming/UseImportsResolver/Fixture/group_use_aliased.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Naming\Naming\UseImportsResolver\Fixture; | ||
|
||
# Valid import for reference | ||
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\OtherClass; | ||
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\{FirstClass as ClassA, SecondClass as ClassB}; | ||
|
||
class MyGroupUse2 | ||
{ | ||
private ClassA $firstClass; | ||
private ClassB $secondClass; | ||
private OtherClass $otherClass; | ||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/Naming/Naming/UseImportsResolver/Source/FirstClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Naming\Naming\UseImportsResolver\Source; | ||
|
||
class FirstClass | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/Naming/Naming/UseImportsResolver/Source/OtherClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Naming\Naming\UseImportsResolver\Source; | ||
|
||
class OtherClass | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/Naming/Naming/UseImportsResolver/Source/SecondClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Naming\Naming\UseImportsResolver\Source; | ||
|
||
class SecondClass | ||
{ | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
rules-tests/Naming/Naming/UseImportsResolver/UseImportsResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Naming\Naming\UseImportsResolver; | ||
|
||
use Iterator; | ||
use PhpParser\Node\Stmt\Property; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Naming\Naming\UseImportsResolver; | ||
use Rector\Testing\PHPUnit\AbstractTestCase; | ||
use Rector\Testing\TestingParser\TestingParser; | ||
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\FirstClass; | ||
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\SecondClass; | ||
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UseImportsResolverTest extends AbstractTestCase | ||
{ | ||
private UseImportsResolver $useImportsResolver; | ||
|
||
private TestingParser $testingParser; | ||
|
||
private BetterNodeFinder $nodeFinder; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->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<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
$directory = __DIR__ . '/Fixture'; | ||
return StaticFixtureFinder::yieldDirectoryExclusively($directory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters