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; }