-
-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow namespace import to be resolved #5123
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\NodeAnalyzer\UseImportNameMatcher\Fixture\FixtureAnnotation; | ||
|
||
/** | ||
* @Annotation | ||
*/ | ||
class CustomAnnotation | ||
{ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\NodeAnalyzer\UseImportNameMatcher\Fixture; | ||
|
||
|
||
use Rector\Tests\CodingStyle\NodeAnalyzer\UseImportNameMatcher\Fixture\FixtureAnnotation; | ||
use Rector\Tests\CodingStyle\NodeAnalyzer\UseImportNameMatcher\Fixture\FixtureAnnotation as Test; | ||
|
||
class UseClass | ||
{ | ||
/** | ||
* @var float | ||
* | ||
* @FixtureAnnotation\CustomAnnotation() | ||
* @Test\CustomAnnotation() | ||
*/ | ||
private $property; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodingStyle\NodeAnalyzer\UseImportNameMatcher; | ||
|
||
use Iterator; | ||
use PhpParser\Node\Stmt\Use_; | ||
use PhpParser\ParserFactory; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Naming\Naming\UseImportsResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Testing\PHPUnit\AbstractLazyTestCase; | ||
use Rector\Testing\TestingParser\TestingParser; | ||
use Rector\Tests\CodingStyle\NodeAnalyzer\UseImportNameMatcher\Fixture\FixtureAnnotation\CustomAnnotation; | ||
|
||
class UseImportNameMatcherTest extends AbstractLazyTestCase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer real https://github.com/rectorphp/rector-src/tree/main/tests/Issues and add fixture with expected output with config there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I'll need to see how to do what you ask. |
||
{ | ||
private TestingParser $testingParser; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->testingParser = $this->make(TestingParser::class); | ||
} | ||
|
||
#[DataProvider('provideData')] | ||
public function testMatchNameWithUses(string $tag, string $expectedResolvedClass): void | ||
{ | ||
$file = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/UseClass.php'); | ||
|
||
$betterNodeFinder = $this->make(BetterNodeFinder::class); | ||
$useImportsResolver = $this->make(UseImportsResolver::class); | ||
$useImportNameMatcher = new UseImportNameMatcher($betterNodeFinder, $useImportsResolver); | ||
|
||
$parserFactory = new ParserFactory(); | ||
$phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7); | ||
|
||
$stmts = $phpParser->parse($file->getFileContent()); | ||
if ($stmts === null) { | ||
$this->fail('No statements parsed'); | ||
} | ||
|
||
$uses = $betterNodeFinder->findInstanceOf($stmts, Use_::class); | ||
foreach ($uses as $useKey => $use) { | ||
foreach ($use->uses as $useUseKey => $useUse) { | ||
$useUse->setAttribute(AttributeKey::ORIGINAL_NODE, $useUse); | ||
unset($use->uses[$useUseKey]); | ||
$use->uses[$useUseKey] = $useUse; | ||
} | ||
unset($uses[$useKey]); | ||
$uses[$useKey] = $use; | ||
} | ||
$resolvedName = $useImportNameMatcher->matchNameWithUses($tag, $uses); | ||
|
||
$this->assertEquals($expectedResolvedClass, $resolvedName); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
yield ['FixtureAnnotation\CustomAnnotation', CustomAnnotation::class]; | ||
yield ['Test\CustomAnnotation', CustomAnnotation::class]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems need a test with
\
prefix for fqcn ensure for:ensure that different namespace for FQCN?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand, is annotation starting with
@\
exists ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, annotation work with
@\
usage as FQCN