From 8fba5b95f79bf6767a39a55a0fd4b0f5a99448eb Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 8 Apr 2024 09:44:15 +0000 Subject: [PATCH] Updated Rector to commit b292010edec6f45b5a48d1f001edce95e05162f3 https://github.com/rectorphp/rector-src/commit/b292010edec6f45b5a48d1f001edce95e05162f3 [Performance][TypeDeclaration] Avoid double loop on SilentVoidResolver use NeverFuncCallAnalyzer (#5809) --- .../NodeAnalyzer/NeverFuncCallAnalyzer.php | 31 ++++++++++--------- .../TypeInferer/SilentVoidResolver.php | 14 ++++----- src/Application/VersionResolver.php | 4 +-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php index 0d743211445..59aadaf9000 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php @@ -4,7 +4,6 @@ declare (strict_types=1); 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; @@ -23,24 +22,26 @@ final class NeverFuncCallAnalyzer $this->nodeTypeResolver = $nodeTypeResolver; } /** - * @param ClassMethod|Closure|Function_|Stmt[] $functionLike + * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike */ public function hasNeverFuncCall($functionLike) : bool { - $hasNeverType = \false; - $stmts = $functionLike instanceof FunctionLike ? (array) $functionLike->stmts : $functionLike; - foreach ($stmts as $stmt) { - if ($stmt instanceof Expression) { - $stmt = $stmt->expr; - } - if ($stmt instanceof Stmt) { - continue; - } - $stmtType = $this->nodeTypeResolver->getNativeType($stmt); - if ($stmtType instanceof NeverType) { - $hasNeverType = \true; + foreach ((array) $functionLike->stmts as $stmt) { + if ($this->isWithNeverTypeExpr($stmt)) { + return \true; } } - return $hasNeverType; + return \false; + } + public function isWithNeverTypeExpr(Stmt $stmt) : bool + { + if ($stmt instanceof Expression) { + $stmt = $stmt->expr; + } + if ($stmt instanceof Stmt) { + return \false; + } + $stmtType = $this->nodeTypeResolver->getNativeType($stmt); + return $stmtType instanceof NeverType; } } diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index 20f8fbb9510..5151da9fb22 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -85,8 +85,8 @@ final class SilentVoidResolver 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)) { return \true; @@ -105,7 +105,7 @@ final class SilentVoidResolver return \true; } } - return $this->neverFuncCallAnalyzer->hasNeverFuncCall($stmts); + return \false; } private function isDoWithAlwaysReturnOrExit(Do_ $do) : bool { @@ -137,11 +137,11 @@ final class SilentVoidResolver } return $this->hasStmtsAlwaysReturnOrExit($stmt->else->stmts); } - /** - * @param \PhpParser\Node\Stmt|\PhpParser\Node\Expr $stmt - */ - private function isStopped($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 || $stmt instanceof Yield_ || $stmt instanceof YieldFrom; } private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch) : bool diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index b38bdd5de53..d0698adbec7 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '9939ced45eb3479a1d8b2b9339512398118b92d9'; + public const PACKAGE_VERSION = 'b292010edec6f45b5a48d1f001edce95e05162f3'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-04-08 16:17:37'; + public const RELEASE_DATE = '2024-04-08 16:41:47'; /** * @var int */