mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
make use of helper method of AbstractRector
This commit is contained in:
parent
a33074bdf5
commit
b9dd90266a
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user