diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc new file mode 100644 index 00000000000..060b7831fb0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc @@ -0,0 +1,40 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc new file mode 100644 index 00000000000..018241763b0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc @@ -0,0 +1,40 @@ + +----- + diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index 4226044584e..55c02a0e3a7 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -13,7 +13,9 @@ use PhpParser\Node\Expr\YieldFrom; use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Continue_; use PhpParser\Node\Stmt\Do_; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\Expression; @@ -94,7 +96,7 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool return true; } - if ($stmt instanceof Do_ && $this->hasStmtsAlwaysReturnOrExit($stmt->stmts)) { + if ($stmt instanceof Do_ && $this->isDoWithAlwaysReturnOrExit($stmt)) { return true; } } @@ -102,6 +104,18 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool return false; } + private function isDoWithAlwaysReturnOrExit(Do_ $do): bool + { + if (! $this->hasStmtsAlwaysReturnOrExit($do->stmts)) { + return false; + } + + return ! (bool) $this->betterNodeFinder->findFirst( + $do->stmts, + static fn (Node $node): bool => $node instanceof Break_ || $node instanceof Continue_ + ); + } + private function isIfReturn(Stmt|Expr $stmt): bool { if (! $stmt instanceof If_) {