From eb852691e2c5352cfdd161ab8461648f4475b46d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 26 Jan 2026 22:15:42 +0700 Subject: [PATCH] Fix double assign * [PHPUnit 12] Skip first class callable on CreateStubOverCreateMockArgRector * fix --- .../Fixture/hop_around_in_call.php.inc | 35 +++++++++++++++++++ .../multiple_uses_as_no_connection.php | 26 ++++++++++++++ .../Source/AnotherClass.php | 4 +++ .../BareCreateMockAssignToDirectUseRector.php | 25 ++++++------- 4 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/hop_around_in_call.php.inc create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/multiple_uses_as_no_connection.php diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/hop_around_in_call.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/hop_around_in_call.php.inc new file mode 100644 index 00000000..68c72f1f --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/hop_around_in_call.php.inc @@ -0,0 +1,35 @@ +createMock(AnotherClass::class); + + $anotherClass = new AnotherClass(1, 2, 3, $someMock); + } +} + +?> +----- +createMock(AnotherClass::class)); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/multiple_uses_as_no_connection.php b/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/multiple_uses_as_no_connection.php new file mode 100644 index 00000000..0279019d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Fixture/multiple_uses_as_no_connection.php @@ -0,0 +1,26 @@ +createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class); + + $this->useMock($someMock); + $this->useMockAgain($someMock); + } + + private function useMock($someMock) + { + } + + private function useMockAgain($someMock) + { + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Source/AnotherClass.php b/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Source/AnotherClass.php index ae754107..c62553d7 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Source/AnotherClass.php +++ b/rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Source/AnotherClass.php @@ -6,4 +6,8 @@ final class AnotherClass { + public function __construct(...$various) + { + + } } diff --git a/rules/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector.php b/rules/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector.php index 67cae9a8..6fc36009 100644 --- a/rules/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector.php @@ -128,23 +128,20 @@ public function refactor(Node $node): ?Node $assign = $stmt->expr; $instanceArg = $this->assignedMocksCollector->matchCreateMockArgAssignedToVariable($assign); - if (! $instanceArg instanceof Arg) { + if ($instanceArg instanceof Arg && $assign->var instanceof Variable && $this->isName( + $assign->var, + $variableName + )) { + // 1. remove assign + unset($node->stmts[$key]); + $hasChanged = true; + $variablesToMethodCalls[$variableName] = $assign->expr; continue; } + } - if (! $assign->var instanceof Variable) { - continue; - } - - if (! $this->isName($assign->var, $variableName)) { - continue; - } - - // 1. remove assign - unset($node->stmts[$key]); - $hasChanged = true; - - $variablesToMethodCalls[$variableName] = $assign->expr; + // nothing to processy yet + if ($variablesToMethodCalls === []) { continue; }