Merge pull request #980 from rectorphp/get-name

Use getName()
This commit is contained in:
Tomáš Votruba 2019-01-22 14:09:33 -08:00 committed by GitHub
commit 3872fa4cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 59 deletions

View File

@ -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(

View File

@ -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;

View File

@ -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');

View File

@ -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;

View File

@ -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;

View File

@ -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());