Skip to content

Commit

Permalink
[Performance][TypeDeclaration] Avoid double loop on SilentVoidResolve…
Browse files Browse the repository at this point in the history
…r use NeverFuncCallAnalyzer (#5809)

* [Performance][TypeDeclaration] Clean up NeverFuncCallAnalyzer and SilentVoidResolver

* better git diff

* fix phpstan
  • Loading branch information
samsonasik committed Apr 8, 2024
1 parent 9939ced commit b292010
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
38 changes: 17 additions & 21 deletions rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
12 changes: 8 additions & 4 deletions rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -105,7 +105,7 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool
}
}

return $this->neverFuncCallAnalyzer->hasNeverFuncCall($stmts);
return false;
}

private function isDoWithAlwaysReturnOrExit(Do_ $do): bool
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b292010

Please sign in to comment.