From 879feb8931f1465fa463d539e9ce99ebfae4e948 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 24 Mar 2024 03:30:55 +0700 Subject: [PATCH] [CodeQuality] Handle do { } while maybe returned on ExplicitReturnNullRector (#5766) * [CodeQuality] Handle do {} while maybe returned on ExplicitReturnNullRector * handle possibly returned * debug * debug --- .../Fixture/do_while_maybe_returned.php.inc | 40 +++++++++++++++++++ .../Fixture/do_while_maybe_returned2.php.inc | 40 +++++++++++++++++++ .../TypeInferer/SilentVoidResolver.php | 16 +++++++- 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned.php.inc create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/do_while_maybe_returned2.php.inc 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_) {