Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Performance][TypeDeclaration] Avoid double loop on SilentVoidResolver use NeverFuncCallAnalyzer #5809

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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