Skip to content

Commit

Permalink
[Php56] Remove parent lookup on UndefinedVariableResolver (#4155)
Browse files Browse the repository at this point in the history
* [Php56] Remove parent lookup on UndefinedVariableResolver

* increase rector kernel cache key
  • Loading branch information
samsonasik committed Jun 10, 2023
1 parent f8c6c5c commit c3189b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
Expand All @@ -39,7 +38,6 @@ public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly NodeNameResolver $nodeNameResolver,
private readonly NodeComparator $nodeComparator,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly VariableAnalyzer $variableAnalyzer
) {
}
Expand All @@ -51,10 +49,12 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
{
$undefinedVariables = [];
$checkedVariables = [];
$currentStmt = null;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (
&$undefinedVariables,
&$checkedVariables
&$checkedVariables,
&$currentStmt
): ?int {
// entering new scope - break!
if ($node instanceof FunctionLike && ! $node instanceof ArrowFunction) {
Expand All @@ -71,6 +71,10 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if ($node instanceof Stmt) {
$currentStmt = $node;
}

if (! $node instanceof Variable) {
return null;
}
Expand All @@ -89,7 +93,7 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
return null;
}

if ($this->hasVariableTypeOrCurrentStmtUnreachable($node, $variableName)) {
if ($this->hasVariableTypeOrCurrentStmtUnreachable($node, $variableName, $currentStmt)) {
return null;
}

Expand Down Expand Up @@ -179,8 +183,11 @@ private function resolveCheckedVariablesFromArrayOrList(Node $node, array $check
return $checkedVariables;
}

private function hasVariableTypeOrCurrentStmtUnreachable(Variable $variable, ?string $variableName): bool
{
private function hasVariableTypeOrCurrentStmtUnreachable(
Variable $variable,
?string $variableName,
?Stmt $currentStmt
): bool {
if (! is_string($variableName)) {
return true;
}
Expand All @@ -192,7 +199,6 @@ private function hasVariableTypeOrCurrentStmtUnreachable(Variable $variable, ?st
return true;
}

$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($variable);
return $currentStmt instanceof Stmt && $currentStmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v85';
private const CACHE_KEY = 'v86';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit c3189b1

Please sign in to comment.