From a1ebd0b33a91b07eebbef48822c9545f06379998 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 13 Jul 2026 13:21:53 +0200 Subject: [PATCH] [CodeQuality] Skip with() multiple arguments in VoidMethodWithCallbackToWillReturnCallbackRector --- .../skip_multiple_with_arguments.php.inc | 23 +++++++++++++++++++ ...WithCallbackToWillReturnCallbackRector.php | 9 +++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/skip_multiple_with_arguments.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/skip_multiple_with_arguments.php.inc b/rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/skip_multiple_with_arguments.php.inc new file mode 100644 index 00000000..6f440fc9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/skip_multiple_with_arguments.php.inc @@ -0,0 +1,23 @@ +createMock(SomeEntityManager::class) + ->method('persist') + ->with( + $this->callback(function ($entity): bool { + $this->assertInstanceOf(\stdClass::class, $entity); + + return true; + }), + false + ); + } +} diff --git a/rules/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector.php b/rules/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector.php index bfb939bd..cfdef383 100644 --- a/rules/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector.php +++ b/rules/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector.php @@ -180,7 +180,14 @@ public function refactor(Node $node): ?Class_ private function matchCallbackClosure(MethodCall $withMethodCall): ?Closure { - $withFirstArg = $withMethodCall->getArgs()[0] ?? null; + $withArgs = $withMethodCall->getArgs(); + + // a 2nd+ ->with() argument matches a further parameter, keep the callback matcher as is + if (count($withArgs) > 1) { + return null; + } + + $withFirstArg = $withArgs[0] ?? null; if (! $withFirstArg instanceof Arg) { return null; }