From 17846bb40c575e2534731b3f14f0edb6f3a7be41 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 2 Feb 2025 08:07:22 +0700 Subject: [PATCH] [PHPUnit90] Ensure return node on has changed on ReplaceAtMethodWithDesiredMatcherRector --- .../ReplaceAtMethodWithDesiredMatcherRector.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php b/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php index 39f74c1a..74a2bab1 100644 --- a/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php +++ b/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php @@ -19,6 +19,8 @@ */ final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector { + private bool $hasChanged = false; + public function __construct( private readonly TestsNodeAnalyzer $testsNodeAnalyzer ) { @@ -59,6 +61,8 @@ public function getNodeTypes(): array */ public function refactor(Node $node): null|MethodCall { + $this->hasChanged = false; + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { return null; } @@ -67,6 +71,10 @@ public function refactor(Node $node): null|MethodCall $this->replaceWithDesiredMatcher($arg); } + if ($this->hasChanged) { + return $node; + } + return null; } @@ -106,10 +114,13 @@ private function replaceWithDesiredMatcher(Arg $arg): void if ($count === 0) { $arg->value = new MethodCall($arg->value->var, 'never'); + $this->hasChanged = true; } elseif ($count === 1) { $arg->value = new MethodCall($arg->value->var, 'once'); + $this->hasChanged = true; } elseif ($count > 1) { $arg->value = new MethodCall($arg->value->var, 'exactly', [new Arg(new Int_($count))]); + $this->hasChanged = true; } } }