From 5488e5cfa467b4f8396e9693afda2726c5896375 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 22 Jan 2019 22:13:03 +0100 Subject: [PATCH] use getName() instead of resolved-name attribute --- docs/AllRectorsOverview.md | 2 +- .../AbstractToConstructorInjectionRector.php | 2 +- .../ServiceLocatorToDIRector.php | 13 ++--- .../Class_/ParentClassToTraitsRector.php | 11 +--- .../Interface_/MergeInterfacesRector.php | 8 +-- .../Namespace_/NamespaceReplacerRector.php | 54 +++++++------------ 6 files changed, 31 insertions(+), 59 deletions(-) diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 024004bdd3e..05c25866259 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -1808,7 +1808,7 @@ Null is no more allowed in get_class() - class: `Rector\Php\Rector\FuncCall\TrailingCommaArgumentsRector` -Adds trailing commas to function and methods calls +Adds trailing commas to function and methods calls ```diff calling( diff --git a/packages/Symfony/src/Rector/FrameworkBundle/AbstractToConstructorInjectionRector.php b/packages/Symfony/src/Rector/FrameworkBundle/AbstractToConstructorInjectionRector.php index 3fcc223882e..9590a6ee405 100644 --- a/packages/Symfony/src/Rector/FrameworkBundle/AbstractToConstructorInjectionRector.php +++ b/packages/Symfony/src/Rector/FrameworkBundle/AbstractToConstructorInjectionRector.php @@ -74,7 +74,7 @@ abstract class AbstractToConstructorInjectionRector extends AbstractRector } if ($argument->class instanceof Name) { - return $argument->class->getAttribute(Attribute::RESOLVED_NAME)->toString(); + return $this->getName($argument->class); } return null; diff --git a/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php b/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php index 627de9e25a4..d0c2b22a858 100644 --- a/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php +++ b/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php @@ -137,10 +137,11 @@ CODE_SAMPLE { $entityFqnOrAlias = $this->entityFqnOrAlias($methodCallNode); - $repositoryClassName = $this->doctrineEntityAndRepositoryMapper->mapEntityToRepository($entityFqnOrAlias); - - if ($repositoryClassName !== null) { - return $repositoryClassName; + if ($entityFqnOrAlias !== null) { + $repositoryClassName = $this->doctrineEntityAndRepositoryMapper->mapEntityToRepository($entityFqnOrAlias); + if ($repositoryClassName !== null) { + return $repositoryClassName; + } } throw new RectorProviderException(sprintf( @@ -150,7 +151,7 @@ CODE_SAMPLE )); } - private function entityFqnOrAlias(MethodCall $methodCallNode): string + private function entityFqnOrAlias(MethodCall $methodCallNode): ?string { $repositoryArgument = $methodCallNode->args[0]->value; @@ -159,7 +160,7 @@ CODE_SAMPLE } if ($repositoryArgument instanceof ClassConstFetch && $repositoryArgument->class instanceof Name) { - return $repositoryArgument->class->getAttribute(Attribute::RESOLVED_NAME)->toString(); + return $this->getName($repositoryArgument->class); } throw new ShouldNotHappenException('Unable to resolve repository argument'); diff --git a/src/Rector/Class_/ParentClassToTraitsRector.php b/src/Rector/Class_/ParentClassToTraitsRector.php index 99065873b58..a72c22fabfb 100644 --- a/src/Rector/Class_/ParentClassToTraitsRector.php +++ b/src/Rector/Class_/ParentClassToTraitsRector.php @@ -6,7 +6,6 @@ use PhpParser\Node; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\TraitUse; -use Rector\NodeTypeResolver\Node\Attribute; use Rector\PhpParser\Node\Maintainer\ClassMaintainer; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\ConfiguredCodeSample; @@ -79,7 +78,7 @@ CODE_SAMPLE return null; } - $nodeParentClassName = $this->getClassNodeParentClassName($node); + $nodeParentClassName = $this->getName($node->extends); if (! isset($this->parentClassToTraits[$nodeParentClassName])) { return null; } @@ -99,14 +98,6 @@ CODE_SAMPLE return $node; } - private function getClassNodeParentClassName(Class_ $classNode): string - { - /** @var FullyQualified $fullyQualifiedName */ - $fullyQualifiedName = $classNode->extends->getAttribute(Attribute::RESOLVED_NAME); - - return $fullyQualifiedName->toString(); - } - private function removeParentClass(Class_ $classNode): void { $classNode->extends = null; diff --git a/src/Rector/Interface_/MergeInterfacesRector.php b/src/Rector/Interface_/MergeInterfacesRector.php index 9c438923ed2..c7ef58a457f 100644 --- a/src/Rector/Interface_/MergeInterfacesRector.php +++ b/src/Rector/Interface_/MergeInterfacesRector.php @@ -5,7 +5,6 @@ namespace Rector\Rector\Interface_; use PhpParser\Node; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; -use Rector\NodeTypeResolver\Node\Attribute; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\ConfiguredCodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -88,12 +87,7 @@ CODE_SAMPLE { $alreadyAddedNames = []; foreach ($classNode->implements as $key => $name) { - if ($name->hasAttribute(Attribute::RESOLVED_NAME)) { - $fqnName = (string) $name->getAttribute(Attribute::RESOLVED_NAME); - } else { - $fqnName = $name->toString(); - } - + $fqnName = $this->getName($name); if (in_array($fqnName, $alreadyAddedNames, true)) { unset($classNode->implements[$key]); continue; diff --git a/src/Rector/Namespace_/NamespaceReplacerRector.php b/src/Rector/Namespace_/NamespaceReplacerRector.php index 0bfaec6c5d8..88a51b33bcc 100644 --- a/src/Rector/Namespace_/NamespaceReplacerRector.php +++ b/src/Rector/Namespace_/NamespaceReplacerRector.php @@ -61,7 +61,11 @@ final class NamespaceReplacerRector extends AbstractRector */ public function refactor(Node $node): ?Node { - $name = $this->resolveNameFromNode($node); + $name = $this->getName($node); + if ($name === null) { + return null; + } + if (! $this->isNamespaceToChange($name)) { return null; } @@ -71,14 +75,14 @@ final class NamespaceReplacerRector extends AbstractRector } if ($node instanceof Namespace_) { - $newName = $this->resolveNewNameFromNode($node); + $newName = $this->resolveNewNameFromNode($name); $node->name = new Name($newName); return $node; } if ($node instanceof Use_) { - $newName = $this->resolveNewNameFromNode($node); + $newName = $this->resolveNewNameFromNode($name); $node->uses[0]->name = new Name($newName); return $node; @@ -87,7 +91,11 @@ final class NamespaceReplacerRector extends AbstractRector if ($this->isPartialNamespace($node)) { $newName = $this->resolvePartialNewName($node); } else { - $newName = $this->resolveNewNameFromNode($node); + $newName = $this->resolveNewNameFromNode($name); + } + + if ($newName === null) { + return null; } $node->parts = explode('\\', $newName); @@ -95,29 +103,6 @@ final class NamespaceReplacerRector extends AbstractRector return $node; } - private function resolveNameFromNode(Node $node): string - { - if ($node instanceof Namespace_ && $node->name) { - return $node->name->toString(); - } - - if ($node instanceof Use_) { - return $node->uses[0]->name->toString(); - } - - if ($node instanceof Name) { - /** @var FullyQualified|null $resolveName */ - $resolveName = $node->getAttribute(Attribute::RESOLVED_NAME); - if ($resolveName) { - return $resolveName->toString(); - } - - return $node->toString(); - } - - return ''; - } - private function isNamespaceToChange(string $namespace): bool { return (bool) $this->getNewNamespaceForOldOne($namespace); @@ -146,10 +131,8 @@ final class NamespaceReplacerRector extends AbstractRector return array_key_exists($newClassName, $this->oldToNewNamespaces); } - private function resolveNewNameFromNode(Node $node): string + private function resolveNewNameFromNode(string $name): string { - $name = $this->resolveNameFromNode($node); - [$oldNamespace, $newNamespace] = $this->getNewNamespaceForOldOne($name); return str_replace($oldNamespace, $newNamespace, $name); @@ -169,11 +152,14 @@ final class NamespaceReplacerRector extends AbstractRector return false; } - private function resolvePartialNewName(Name $nameNode): string + private function resolvePartialNewName(Name $nameNode): ?string { - /** @var FullyQualified $resolvedName */ - $resolvedName = $nameNode->getAttribute(Attribute::RESOLVED_NAME); - $completeNewName = $this->resolveNewNameFromNode($resolvedName); + $name = $this->getName($nameNode); + if ($name === null) { + return null; + } + + $completeNewName = $this->resolveNewNameFromNode($name); // first dummy implementation - improve $cutOffFromTheLeft = strlen($completeNewName) - strlen($nameNode->toString());