Skip to content

Commit

Permalink
[CodeQuality] Handle crash on variable variable on assign closure on …
Browse files Browse the repository at this point in the history
…OptionalParametersAfterRequiredRector (#5089)

* [CodeQuality] Handle crash on variable variable on assign closure on OptionalParametersAfterRequiredRector

* Fix

* Fix scope check
  • Loading branch information
samsonasik committed Sep 28, 2023
1 parent 2f90125 commit 8e67265
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function processNodes(

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
$nodeTraverser->addVisitor(new ExprScopeFromStmtNodeVisitor($scope));
$nodeTraverser->addVisitor(new ExprScopeFromStmtNodeVisitor($this, $filePath, $scope));
$nodeTraverser->traverse($stmts);

return $stmts;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;

abstract class SkipVariableVariable5
{
public function run()
{
$$get_data = $get_data->Where(function ($query) use ($field, $key) {
$query->where($key, '!=', $field['search_value'][0]['val'])->orWhere($key, 'exists', false);
});
}
}
13 changes: 10 additions & 3 deletions src/PHPStan/NodeVisitor/ExprScopeFromStmtNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Node\VirtualNode;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;

final class ExprScopeFromStmtNodeVisitor extends NodeVisitorAbstract
{
private ?Stmt $currentStmt = null;

public function __construct(
private readonly PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
private readonly string $filePath,
private readonly MutatingScope $mutatingScope
) {
}
Expand Down Expand Up @@ -52,7 +55,7 @@ public function enterNode(Node $node): ?Node
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope) {
if ($scope instanceof MutatingScope) {
return null;
}

Expand All @@ -61,10 +64,14 @@ public function enterNode(Node $node): ?Node
? $this->currentStmt->getAttribute(AttributeKey::SCOPE)
: $this->mutatingScope;

$scope = $scope instanceof Scope ? $scope : $this->mutatingScope;
$scope = $scope instanceof MutatingScope ? $scope : $this->mutatingScope;

$node->setAttribute(AttributeKey::SCOPE, $scope);

if ($node instanceof Closure) {
$this->phpStanNodeScopeResolver->processNodes($node->stmts, $this->filePath, $scope);
}

return null;
}
}

0 comments on commit 8e67265

Please sign in to comment.