diff --git a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php index c20fe589ef6..582d112fd86 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php @@ -5,7 +5,6 @@ namespace Rector\TypeDeclaration\NodeAnalyzer; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; @@ -20,31 +19,28 @@ public function __construct( ) { } - /** - * @param ClassMethod|Closure|Function_|Stmt[] $functionLike - */ - public function hasNeverFuncCall(ClassMethod | Closure | Function_ | array $functionLike): bool + public function hasNeverFuncCall(ClassMethod | Closure | Function_ $functionLike): bool { - $hasNeverType = false; - $stmts = $functionLike instanceof FunctionLike - ? (array) $functionLike->stmts - : $functionLike; - - foreach ($stmts as $stmt) { - if ($stmt instanceof Expression) { - $stmt = $stmt->expr; + foreach ((array) $functionLike->stmts as $stmt) { + if ($this->isWithNeverTypeExpr($stmt)) { + return true; } + } - if ($stmt instanceof Stmt) { - continue; - } + return false; + } - $stmtType = $this->nodeTypeResolver->getNativeType($stmt); - if ($stmtType instanceof NeverType) { - $hasNeverType = true; - } + public function isWithNeverTypeExpr(Stmt $stmt): bool + { + if ($stmt instanceof Expression) { + $stmt = $stmt->expr; + } + + if ($stmt instanceof Stmt) { + return false; } - return $hasNeverType; + $stmtType = $this->nodeTypeResolver->getNativeType($stmt); + return $stmtType instanceof NeverType; } } diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index 1dcbe97547d..fb0a0bcee17 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -79,8 +79,8 @@ public function hasSilentVoid(FunctionLike $functionLike): bool private function hasStmtsAlwaysReturnOrExit(array $stmts): bool { foreach ($stmts as $stmt) { - if ($stmt instanceof Expression) { - $stmt = $stmt->expr; + if ($this->neverFuncCallAnalyzer->isWithNeverTypeExpr($stmt)) { + return true; } if ($this->isStopped($stmt)) { @@ -105,7 +105,7 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool } } - return $this->neverFuncCallAnalyzer->hasNeverFuncCall($stmts); + return false; } private function isDoWithAlwaysReturnOrExit(Do_ $do): bool @@ -143,8 +143,12 @@ private function isIfReturn(Stmt|Expr $stmt): bool return $this->hasStmtsAlwaysReturnOrExit($stmt->else->stmts); } - private function isStopped(Stmt|Expr $stmt): bool + private function isStopped(Stmt $stmt): bool { + if ($stmt instanceof Expression) { + $stmt = $stmt->expr; + } + return $stmt instanceof Throw_ || $stmt instanceof Exit_ || ($stmt instanceof Return_ && $stmt->expr instanceof Expr)