Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

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:

     * @\FixtureAnnotation\CustomAnnotation()

ensure that different namespace for FQCN?

Copy link
Contributor Author

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 ?

Copy link
Member

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

* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer real rules() usage instead of creating new test class, as it hard to debug, you can add more test with specific config under tests/Issues

https://github.com/rectorphp/rector-src/tree/main/tests/Issues

and add fixture with expected output with config there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I'll need to see how to do what you ask.
I'll take a look to the otheres tests.

{
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];
}
}
9 changes: 9 additions & 0 deletions rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ private function resolveName(string $prefix, string $tag, UseUse $useUse): strin
}

if (! $originalUseUseNode->alias instanceof Identifier) {
$parts = $originalUseUseNode->name->getParts();
$tagExploded = explode('\\', $tag);
if (count($tagExploded) > 1 && reset($tagExploded) === end($parts)) {
//no alias and not a direct use import of a class, search the class
array_pop($parts);
$FQNClassArray = [$prefix, ...$parts, ...$tagExploded];
$FQNClassArray = array_filter($FQNClassArray);
return implode('\\', $FQNClassArray);
}
return $prefix . $originalUseUseNode->name->toString();
}

Expand Down