From 4b5511a40449468c3376f07b82ba4fa1c676b26a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 13 Oct 2026 22:49:02 +0200 Subject: [PATCH] [CodeQuality] Skip trailing comment after return in WillReturnCallbackFallbackToReturnFalseRector --- ...skip_trailing_comment_after_return.php.inc | 20 +++++++++++++++++++ ...urnCallbackFallbackToReturnFalseRector.php | 7 +++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector/Fixture/skip_trailing_comment_after_return.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector/Fixture/skip_trailing_comment_after_return.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector/Fixture/skip_trailing_comment_after_return.php.inc new file mode 100644 index 00000000..5332d6e5 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector/Fixture/skip_trailing_comment_after_return.php.inc @@ -0,0 +1,20 @@ +createMock(\stdClass::class); + $queueMock->expects($this->atLeastOnce())->method('isEmpty')->willReturnCallback(static function () use (&$callSequence): bool { + $callSequence[] = 'isEmpty'; + + return true; // Exit loop immediately + }); + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector.php b/rules/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector.php index 820fcc98..6b444601 100644 --- a/rules/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector.php +++ b/rules/CodeQuality/Rector/MethodCall/WillReturnCallbackFallbackToReturnFalseRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Stmt\Nop; use PhpParser\Node\Stmt\Return_; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; @@ -123,8 +124,10 @@ public function refactor(Node $node): ?MethodCall return null; } - // already ends with an explicit return, no fallback needed - $lastStmt = $closure->stmts[count($closure->stmts) - 1]; + // already ends with an explicit return, no fallback needed; + // skip trailing comment-only Nop statements added by a trailing comment + $stmts = array_filter($closure->stmts, static fn (Node $stmt): bool => ! $stmt instanceof Nop); + $lastStmt = end($stmts); if ($lastStmt instanceof Return_) { return null; }