From 3ade1f7c7f38e42e4ecb7dee2586aad1a35c67fe Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 8 Apr 2024 16:32:55 +0700 Subject: [PATCH 1/3] [Performance][TypeDeclaration] Clean up NeverFuncCallAnalyzer and SilentVoidResolver --- .../NodeAnalyzer/NeverFuncCallAnalyzer.php | 38 +++++++++---------- .../TypeInferer/SilentVoidResolver.php | 6 ++- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php index c20fe589ef6..e54f98be0df 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 isWithNeverTypeExpr(Stmt $stmt): 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 Expression) { + $stmt = $stmt->expr; + } - if ($stmt instanceof Stmt) { - continue; - } + if ($stmt instanceof Stmt) { + return false; + } - $stmtType = $this->nodeTypeResolver->getNativeType($stmt); - if ($stmtType instanceof NeverType) { - $hasNeverType = true; + $stmtType = $this->nodeTypeResolver->getNativeType($stmt); + return $stmtType instanceof NeverType; + } + + public function hasNeverFuncCall(ClassMethod | Closure | Function_ | Stmt $functionLike): bool + { + foreach ((array) $functionLike->stmts as $stmt) { + if ($this->isWithNeverTypeExpr($stmt)) { + return true; } } - return $hasNeverType; + return false; } } diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index 1dcbe97547d..d14c33cf30e 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -79,6 +79,10 @@ public function hasSilentVoid(FunctionLike $functionLike): bool private function hasStmtsAlwaysReturnOrExit(array $stmts): bool { foreach ($stmts as $stmt) { + if ($this->neverFuncCallAnalyzer->isWithNeverTypeExpr($stmt)) { + return true; + } + if ($stmt instanceof Expression) { $stmt = $stmt->expr; } @@ -105,7 +109,7 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool } } - return $this->neverFuncCallAnalyzer->hasNeverFuncCall($stmts); + return false; } private function isDoWithAlwaysReturnOrExit(Do_ $do): bool From c76b854b444fb5e4bb971ce1edfaaf4d56f04b38 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 8 Apr 2024 16:36:42 +0700 Subject: [PATCH 2/3] better git diff --- .../NodeAnalyzer/NeverFuncCallAnalyzer.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php index e54f98be0df..c6aaa0b2e48 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php @@ -19,6 +19,17 @@ public function __construct( ) { } + public function hasNeverFuncCall(ClassMethod | Closure | Function_ | Stmt $functionLike): bool + { + foreach ((array) $functionLike->stmts as $stmt) { + if ($this->isWithNeverTypeExpr($stmt)) { + return true; + } + } + + return false; + } + public function isWithNeverTypeExpr(Stmt $stmt): bool { if ($stmt instanceof Expression) { @@ -32,15 +43,4 @@ public function isWithNeverTypeExpr(Stmt $stmt): bool $stmtType = $this->nodeTypeResolver->getNativeType($stmt); return $stmtType instanceof NeverType; } - - public function hasNeverFuncCall(ClassMethod | Closure | Function_ | Stmt $functionLike): bool - { - foreach ((array) $functionLike->stmts as $stmt) { - if ($this->isWithNeverTypeExpr($stmt)) { - return true; - } - } - - return false; - } } From 43841fb7cf5ca221c4a9917b4fbfc80963ac3b54 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 8 Apr 2024 16:39:51 +0700 Subject: [PATCH 3/3] fix phpstan --- .../NodeAnalyzer/NeverFuncCallAnalyzer.php | 2 +- .../TypeDeclaration/TypeInferer/SilentVoidResolver.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php index c6aaa0b2e48..582d112fd86 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php @@ -19,7 +19,7 @@ public function __construct( ) { } - public function hasNeverFuncCall(ClassMethod | Closure | Function_ | Stmt $functionLike): bool + public function hasNeverFuncCall(ClassMethod | Closure | Function_ $functionLike): bool { foreach ((array) $functionLike->stmts as $stmt) { if ($this->isWithNeverTypeExpr($stmt)) { diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index d14c33cf30e..fb0a0bcee17 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -83,10 +83,6 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool return true; } - if ($stmt instanceof Expression) { - $stmt = $stmt->expr; - } - if ($this->isStopped($stmt)) { return true; } @@ -147,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)