-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
UnreachableStatementNodeVisitor.php
65 lines (53 loc) · 2.04 KB
/
UnreachableStatementNodeVisitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Rector\PHPStan\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
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 PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
private readonly string $filePath,
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;
}
$isPassedUnreachableStmt = false;
$mutatingScope = $this->resolveScope($node->getAttribute(AttributeKey::SCOPE));
foreach ($node->stmts as $stmt) {
if (! $stmt->getAttribute(AttributeKey::SCOPE) instanceof MutatingScope) {
$this->phpStanNodeScopeResolver->processNodes([$stmt], $this->filePath, $mutatingScope);
}
if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) {
$isPassedUnreachableStmt = true;
continue;
}
if ($isPassedUnreachableStmt) {
$stmt->setAttribute(AttributeKey::IS_UNREACHABLE, true);
$stmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}
return null;
}
private function resolveScope(?Scope $mutatingScope): MutatingScope
{
return $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeFactory->createFromFile(
$this->filePath
);
}
}