diff --git a/examples/MergeIsCandidateRector.php b/examples/MergeIsCandidateRector.php index 3d3e5c8831f..5bbad21587e 100644 --- a/examples/MergeIsCandidateRector.php +++ b/examples/MergeIsCandidateRector.php @@ -158,7 +158,7 @@ final class MergeIsCandidateRector extends AbstractRector continue; } - if ((string) $stmt->name === $name) { + if ($this->isName($stmt->name, $name)) { return [$i, $stmt]; } } @@ -253,7 +253,7 @@ final class MergeIsCandidateRector extends AbstractRector private function renameNodeToParamNode(ClassMethod $classMethod, string $nodeName): void { $this->callbackNodeTraverser->traverseNodesWithCallable([$classMethod], function (Node $node) use ($nodeName): ?Node { - if (! $node instanceof Variable || $node->name !== 'node') { + if (! $node instanceof Variable || ! $this->isName($node, 'node')) { return null; } @@ -286,7 +286,7 @@ final class MergeIsCandidateRector extends AbstractRector private function removeReturnTrue(ClassMethod $classMethod): void { $this->callbackNodeTraverser->traverseNodesWithCallable([$classMethod], function (Node $node): ?Node { - if (! $node instanceof Return_ || ! $node->expr instanceof ConstFetch || (string) $node->expr->name !== 'true') { + if (! $node instanceof Return_ || ! $node->expr instanceof ConstFetch || ! $this->isTrue($node->expr)) { return null; } diff --git a/packages/CodeQuality/src/Rector/Foreach_/ForeachToInArrayRector.php b/packages/CodeQuality/src/Rector/Foreach_/ForeachToInArrayRector.php index 5c345068d65..a0d8dcac07a 100644 --- a/packages/CodeQuality/src/Rector/Foreach_/ForeachToInArrayRector.php +++ b/packages/CodeQuality/src/Rector/Foreach_/ForeachToInArrayRector.php @@ -146,12 +146,12 @@ CODE_SAMPLE /** @var Variable $foreachVariable */ $foreachVariable = $foreachNode->valueVar; if ($leftValue instanceof Variable) { - if ($leftValue->name === $foreachVariable->name) { + if ($this->areNodesEqual($leftValue, $foreachVariable)) { return $rightValue; } } - if ($rightValue->name === $foreachVariable->name) { + if ($this->areNodesEqual($rightValue, $foreachVariable)) { return $leftValue; } } diff --git a/packages/PHPUnit/src/Rector/TryCatchToExpectExceptionRector.php b/packages/PHPUnit/src/Rector/TryCatchToExpectExceptionRector.php index e82abbc8d8e..190d46f0730 100644 --- a/packages/PHPUnit/src/Rector/TryCatchToExpectExceptionRector.php +++ b/packages/PHPUnit/src/Rector/TryCatchToExpectExceptionRector.php @@ -144,7 +144,7 @@ CODE_SAMPLE $argumentVariableName = $this->getName($methodCallNode->args[1]->value); // is na exception variable - if ($exceptionVariableNode->name !== $argumentVariableName) { + if (! $this->isName($exceptionVariableNode, $argumentVariableName)) { return; } diff --git a/packages/Php/src/Rector/FuncCall/CallUserMethodRector.php b/packages/Php/src/Rector/FuncCall/CallUserMethodRector.php index b923ce822bd..3c17a2c8983 100644 --- a/packages/Php/src/Rector/FuncCall/CallUserMethodRector.php +++ b/packages/Php/src/Rector/FuncCall/CallUserMethodRector.php @@ -55,11 +55,11 @@ final class CallUserMethodRector extends AbstractRector */ public function refactor(Node $node): ?Node { - $newName = $this->matchNewFunctionName($node); - if ($newName === null) { + if (! $this->isNames($node, array_keys($this->oldToNewFunctions))) { return null; } + $newName = $this->oldToNewFunctions[$this->getName($node)]; $node->name = new Name($newName); $argNodes = $node->args; @@ -72,10 +72,4 @@ final class CallUserMethodRector extends AbstractRector return $node; } - - private function matchNewFunctionName(FuncCall $funcCallNode): ?string - { - $currentFunction = $this->getName($funcCallNode); - return $this->oldToNewFunctions[$currentFunction] ?? null; - } } diff --git a/src/Rector/Constant/RenameClassConstantsUseToStringsRector.php b/src/Rector/Constant/RenameClassConstantsUseToStringsRector.php index 68b11cdbf2e..71b61902187 100644 --- a/src/Rector/Constant/RenameClassConstantsUseToStringsRector.php +++ b/src/Rector/Constant/RenameClassConstantsUseToStringsRector.php @@ -4,10 +4,7 @@ namespace Rector\Rector\Constant; use PhpParser\Node; use PhpParser\Node\Expr\ClassConstFetch; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; -use Rector\NodeTypeResolver\Node\Attribute; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\ConfiguredCodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -56,34 +53,20 @@ final class RenameClassConstantsUseToStringsRector extends AbstractRector */ public function refactor(Node $node): ?Node { - $className = $this->getClassNameFromClassConstFetch($node); - foreach ($this->oldConstantsToNewValuesByType as $type => $oldConstantsToNewValues) { - if ($className !== $type) { + if (! $this->isType($node->class, $type)) { continue; } foreach ($oldConstantsToNewValues as $oldConstant => $newValue) { - if ($this->isName($node, $oldConstant)) { - return new String_($newValue); + if (! $this->isName($node, $oldConstant)) { + continue; } + + return new String_($newValue); } } return $node; } - - private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): ?string - { - /** @var FullyQualified|null $fqnName */ - $fqnName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME); - - if ($fqnName === null && $classConstFetchNode->class instanceof Variable) { - return $this->getName($classConstFetchNode->class); - } - - if ($fqnName !== null) { - return $fqnName->toString(); - } - } } diff --git a/src/Rector/Namespace_/NamespaceReplacerRector.php b/src/Rector/Namespace_/NamespaceReplacerRector.php index 3af6197ceb1..8b1c70b8e9b 100644 --- a/src/Rector/Namespace_/NamespaceReplacerRector.php +++ b/src/Rector/Namespace_/NamespaceReplacerRector.php @@ -166,9 +166,8 @@ final class NamespaceReplacerRector extends AbstractRector return false; } - $nodeName = $nameNode->toString(); if ($resolvedName instanceof FullyQualified) { - return $nodeName !== $resolvedName->toString(); + return ! $this->isName($nameNode, $resolvedName->toString()); } return false;