mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 09:42:45 +01:00
Added tests for RenameVariableToMatchNewTypeRector (#4898)
* Added tests to fix issue * Removed tests placed with wrong rule * Added test with right rule * Simplified className * fixed * [ci-review] Rector Rectify Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
6c813e5e4d
commit
20622c2541
@ -21,6 +21,7 @@ use Rector\Naming\NamingConvention\NamingConventionAnalyzer;
|
||||
use Rector\Naming\PhpDoc\VarTagValueNodeRenamer;
|
||||
use Rector\Naming\ValueObject\VariableAndCallAssign;
|
||||
use Rector\Naming\VariableRenamer;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
@ -146,7 +147,12 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
$expectedName = $this->expectedNameResolver->resolveForCall($variableAndCallAssign->getCall());
|
||||
$call = $variableAndCallAssign->getCall();
|
||||
if ($this->isMultipleCall($call)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$expectedName = $this->expectedNameResolver->resolveForCall($call);
|
||||
if ($expectedName === null || $this->isName($node->var, $expectedName)) {
|
||||
return null;
|
||||
}
|
||||
@ -160,6 +166,36 @@ CODE_SAMPLE
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall|StaticCall|MethodCall $node
|
||||
*/
|
||||
private function isMultipleCall(Node $node): bool
|
||||
{
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
while ($parentNode) {
|
||||
$countUsed = count($this->betterNodeFinder->find($parentNode, function (Node $n) use ($node): bool {
|
||||
if (get_class($node) !== get_class($n)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$passedNode = clone $n;
|
||||
$usedNode = clone $node;
|
||||
$passedNode->args = [];
|
||||
$usedNode->args = [];
|
||||
|
||||
return $this->areNodesEqual($passedNode, $usedNode);
|
||||
}));
|
||||
|
||||
if ($countUsed > 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function shouldSkip(VariableAndCallAssign $variableAndCallAssign, string $expectedName): bool
|
||||
{
|
||||
if ($this->namingConventionAnalyzer->isCallMatchingVariableName(
|
||||
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Naming\Tests\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\Fixture;
|
||||
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
|
||||
final class SkipDoubleMethodCall
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$magicGet = $this->createClassConstFetch('SomeClass', 'MAGIC_GET');
|
||||
$magicSet = $this->createClassConstFetch('SomeClass', 'MAGIC_SET');
|
||||
}
|
||||
|
||||
protected function createClassConstFetch(string $class, string $constant): ClassConstFetch
|
||||
{
|
||||
return new ClassConstFetch($class, $constant);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user