From 20622c2541676b705c268d6b38954b03e0f3f608 Mon Sep 17 00:00:00 2001 From: Leonardo Losoviz Date: Sat, 19 Dec 2020 06:18:17 +0800 Subject: [PATCH] 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 Co-authored-by: rector-bot --- ...iableToMatchMethodCallReturnTypeRector.php | 38 ++++++++++++++++++- .../Fixture/skip_double_method_call.php.inc | 20 ++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 rules/naming/tests/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_double_method_call.php.inc diff --git a/rules/naming/src/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php b/rules/naming/src/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php index 5b6485a511a..793a5358f3f 100644 --- a/rules/naming/src/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php +++ b/rules/naming/src/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php @@ -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( diff --git a/rules/naming/tests/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_double_method_call.php.inc b/rules/naming/tests/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_double_method_call.php.inc new file mode 100644 index 00000000000..9cfe72bc173 --- /dev/null +++ b/rules/naming/tests/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture/skip_double_method_call.php.inc @@ -0,0 +1,20 @@ +createClassConstFetch('SomeClass', 'MAGIC_GET'); + $magicSet = $this->createClassConstFetch('SomeClass', 'MAGIC_SET'); + } + + protected function createClassConstFetch(string $class, string $constant): ClassConstFetch + { + return new ClassConstFetch($class, $constant); + } +} +?>