-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NodeTypeResolver] Remove parent attribute on PHPStanNodeScopeResolve…
…r for after UnreachableStatementNode detector (#4415) * [NodeTypeResolver] Remove parent attribute on PHPStanNodeScopeResolver for after UnreachableStatementNodeVisitor detector * inject the visitor * [ci-review] Rector Rectify * clean up set child * remove manual set parent * remove manual set parent * property fix * stop on unreachable * fix phpstan * scope fix * fix * Update UnreachableStatementNodeVisitor.php * [ci-review] Rector Rectify * clean up * clean up --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
708cc4f
commit 93a4b2b
Showing
6 changed files
with
91 additions
and
71 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
66 changes: 66 additions & 0 deletions
66
src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\PHPStan\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\Declare_; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PHPStan\Analyser\MutatingScope; | ||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; | ||
use Rector\Core\Provider\CurrentFileProvider; | ||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver; | ||
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory; | ||
|
||
final class UnreachableStatementNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
public function __construct( | ||
private readonly CurrentFileProvider $currentFileProvider, | ||
private readonly PHPStanNodeScopeResolver $phpStanNodeScopeResolver, | ||
private readonly ScopeFactory $scopeFactory | ||
) { | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if (! $node instanceof StmtsAwareInterface && ! $node instanceof ClassLike && ! $node instanceof Declare_) { | ||
return null; | ||
} | ||
|
||
if ($node->stmts === null) { | ||
return null; | ||
} | ||
|
||
$file = $this->currentFileProvider->getFile(); | ||
if (! $file instanceof File) { | ||
return null; | ||
} | ||
|
||
$filePath = $file->getFilePath(); | ||
$isPassedUnreachableStmt = false; | ||
|
||
$mutatingScope = $node->getAttribute(AttributeKey::SCOPE); | ||
$mutatingScope = $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeFactory->createFromFile( | ||
$filePath | ||
); | ||
|
||
foreach ($node->stmts as $stmt) { | ||
if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) { | ||
$isPassedUnreachableStmt = true; | ||
continue; | ||
} | ||
|
||
if ($isPassedUnreachableStmt) { | ||
$stmt->setAttribute(AttributeKey::IS_UNREACHABLE, true); | ||
$stmt->setAttribute(AttributeKey::SCOPE, $mutatingScope); | ||
$this->phpStanNodeScopeResolver->processNodes([$stmt], $filePath, $mutatingScope); | ||
} | ||
} | ||
|
||
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