-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Add ExprScopeFromStmtNodeVisitor, move logic deep Expr from Scop…
…eAnalyzer to it (#4815) Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
0477f5b
commit 87c677e
Showing
6 changed files
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\PHPStan\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PHPStan\Analyser\Scope; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory; | ||
|
||
final class ExprScopeFromStmtNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
private ?Stmt $currentStmt = null; | ||
|
||
public function __construct( | ||
private readonly ScopeFactory $scopeFactory, | ||
private string $filePath | ||
) { | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Stmt) { | ||
$this->currentStmt = $node; | ||
return null; | ||
} | ||
|
||
if (! $node instanceof Expr || $node->getAttribute(AttributeKey::EXPRESSION_DEPTH) < 2) { | ||
return null; | ||
} | ||
|
||
$scope = $node->getAttribute(AttributeKey::SCOPE); | ||
if ($scope instanceof Scope) { | ||
return null; | ||
} | ||
|
||
// too deep Expr, eg: $$param = $$bar = self::decodeValue($result->getItem()->getTextContent()); | ||
$filePath = $this->filePath; | ||
$scope = $this->currentStmt instanceof Stmt | ||
? $this->currentStmt->getAttribute(AttributeKey::SCOPE) | ||
: $this->scopeFactory->createFromFile($filePath); | ||
|
||
$scope = $scope instanceof Scope ? $scope : $this->scopeFactory->createFromFile($filePath); | ||
|
||
$node->setAttribute(AttributeKey::SCOPE, $scope); | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters