[CodingStyle] Fix ImportFullyQualifiedNamesRector for imported namespaces

This commit is contained in:
Tomas Votruba 2019-08-22 09:43:12 +02:00
parent b82d6dc8af
commit 1792dcc195
4 changed files with 25 additions and 1 deletions

View File

@ -5,6 +5,7 @@ namespace Rector\CodingStyle\Imports;
use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\UseUse;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class AliasUsesResolver
{
@ -23,6 +24,10 @@ final class AliasUsesResolver
*/
public function resolveForNode(Node $node): array
{
if (! $node instanceof Namespace_) {
$node = $node->getAttribute(AttributeKey::NAMESPACE_NODE);
}
if ($node instanceof Namespace_) {
return $this->resolveForNamespace($node);
}

View File

@ -221,6 +221,13 @@ CODE_SAMPLE
return false;
}
foreach ($this->aliasedUses as $aliasedUse) {
// its aliased, we cannot just rename it
if (Strings::endsWith($aliasedUse, '\\' . $shortName)) {
return true;
}
}
return $this->useAddingCommander->canImportBeAdded($name, $fullyQualifiedName);
}
@ -237,7 +244,6 @@ CODE_SAMPLE
private function canBeNameImported(Name $name): bool
{
$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Namespace_) {
return false;
}

View File

@ -0,0 +1,12 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
final class KeepAlaised
{
private const SUPPORTED_HTTP_METHODS = [
HttpRequest::METHOD_GET
];
}

View File

@ -36,6 +36,7 @@ final class ImportFullyQualifiedNamesRectorTest extends AbstractRectorTestCase
// keep
yield [__DIR__ . '/Fixture/keep.php.inc'];
yield [__DIR__ . '/Fixture/keep_aliased.php.inc'];
yield [__DIR__ . '/Fixture/keep_same_end.php.inc'];
yield [__DIR__ . '/Fixture/keep_trait_use.php.inc'];