From 0c028c6ad1b5abb64e041266f3d12c93d647d065 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 23 Oct 2025 16:15:45 +0200 Subject: [PATCH 1/2] simplify code --- ...eplaceAtMethodWithDesiredMatcherRector.php | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php b/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php index 74a2bab1..492a5f54 100644 --- a/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php +++ b/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php @@ -19,11 +19,10 @@ */ final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector { - private bool $hasChanged = false; - public function __construct( private readonly TestsNodeAnalyzer $testsNodeAnalyzer - ) { + ) + { } public function getRuleDefinition(): RuleDefinition @@ -57,21 +56,49 @@ public function getNodeTypes(): array } /** - * @param MethodCall $node + * @param MethodCall $node */ public function refactor(Node $node): null|MethodCall { - $this->hasChanged = false; + if (!$this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + if (!$node->var instanceof MethodCall) { + return null; + } + + $arg = $this->findAtMethodCall($node->var); + if (! $arg instanceof Arg) { + return null; + } + + if (!$arg->value instanceof MethodCall) { + return null; + } - if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + $count = null; + foreach ($arg->value->getArgs() as $item) { + if ($item->value instanceof Int_) { + $count = $item->value->value; + } + } + + if (!isset($count)) { return null; } - if ($node->var instanceof MethodCall && $arg = $this->findAtMethodCall($node->var)) { - $this->replaceWithDesiredMatcher($arg); + if ($count === 0) { + $arg->value = new MethodCall($arg->value->var, 'never'); + return $node; + } + if ($count === 1) { + $arg->value = new MethodCall($arg->value->var, 'once'); + return $node; } - if ($this->hasChanged) { + if ($count > 1) { + $arg->value = new MethodCall($arg->value->var, 'exactly', [new Arg(new Int_($count))]); return $node; } @@ -81,10 +108,8 @@ public function refactor(Node $node): null|MethodCall private function findAtMethodCall(MethodCall $methodCall): ?Arg { foreach ($methodCall->getArgs() as $arg) { - if ($arg->value instanceof MethodCall && - $arg->value->name instanceof Identifier && - $arg->value->name->toString() === 'at' - ) { + $argExpr = $arg->value; + if ($argExpr instanceof MethodCall && $this->isName($argExpr->name, 'at')) { return $arg; } } @@ -95,32 +120,4 @@ private function findAtMethodCall(MethodCall $methodCall): ?Arg return null; } - - private function replaceWithDesiredMatcher(Arg $arg): void - { - if (! $arg->value instanceof MethodCall) { - return; - } - - foreach ($arg->value->getArgs() as $item) { - if ($item->value instanceof Int_) { - $count = $item->value->value; - } - } - - if (! isset($count)) { - return; - } - - 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; - } - } } From c71728891840ede644b3f809f7c12db6d002dfda Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 23 Oct 2025 16:16:27 +0200 Subject: [PATCH 2/2] register ReplaceAtMethodWithDesiredMatcherRector to code-quality --- config/sets/phpunit-code-quality.php | 2 ++ .../ReplaceAtMethodWithDesiredMatcherRector.php | 13 ++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index 18d54bd8..2c34f7fa 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -46,6 +46,7 @@ use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector; use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector; +use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ @@ -119,5 +120,6 @@ // prefer simple mocking GetMockBuilderGetMockToCreateMockRector::class, EntityDocumentCreateMockToDirectNewRector::class, + ReplaceAtMethodWithDesiredMatcherRector::class, ]); }; diff --git a/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php b/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php index 492a5f54..7dac1f03 100644 --- a/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php +++ b/rules/PHPUnit90/Rector/MethodCall/ReplaceAtMethodWithDesiredMatcherRector.php @@ -7,7 +7,6 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Identifier; use PhpParser\Node\Scalar\Int_; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; @@ -21,8 +20,7 @@ final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector { public function __construct( private readonly TestsNodeAnalyzer $testsNodeAnalyzer - ) - { + ) { } public function getRuleDefinition(): RuleDefinition @@ -60,11 +58,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): null|MethodCall { - if (!$this->testsNodeAnalyzer->isInTestClass($node)) { + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { return null; } - if (!$node->var instanceof MethodCall) { + if (! $node->var instanceof MethodCall) { return null; } @@ -73,7 +71,7 @@ public function refactor(Node $node): null|MethodCall return null; } - if (!$arg->value instanceof MethodCall) { + if (! $arg->value instanceof MethodCall) { return null; } @@ -84,7 +82,7 @@ public function refactor(Node $node): null|MethodCall } } - if (!isset($count)) { + if (! isset($count)) { return null; } @@ -92,6 +90,7 @@ public function refactor(Node $node): null|MethodCall $arg->value = new MethodCall($arg->value->var, 'never'); return $node; } + if ($count === 1) { $arg->value = new MethodCall($arg->value->var, 'once'); return $node;