-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathExprInTopStmtMatcher.php
More file actions
153 lines (127 loc) · 4 KB
/
ExprInTopStmtMatcher.php
File metadata and controls
153 lines (127 loc) · 4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;
use PHPStan\Analyser\Scope;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
/**
* To resolve Expr in top Stmt from early Expr attribute
* so the usage can append code before the Stmt
*/
final readonly class ExprInTopStmtMatcher
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
) {
}
/**
* @param callable(Node $node): bool $filter
*/
public function match(Node|Switch_|Return_|Expression|Echo_ $stmt, callable $filter): null|Expr
{
if ($stmt instanceof Closure) {
return null;
}
$nodes = [];
if ($stmt instanceof Foreach_) {
// keyVar can be null, so need to be filtered only Expr
$nodes = array_filter([$stmt->expr, $stmt->keyVar, $stmt->valueVar]);
}
if ($stmt instanceof For_) {
$nodes = $stmt->init;
$nodes = array_merge($nodes, $stmt->cond);
$nodes = array_merge($nodes, $stmt->loop);
}
if ($stmt instanceof If_ || $stmt instanceof While_ || $stmt instanceof Do_ || $stmt instanceof Switch_) {
$nodes = [$stmt->cond];
}
if ($stmt instanceof Echo_) {
$nodes = $stmt->exprs;
}
foreach ($nodes as $node) {
$expr = $this->resolveExpr($stmt, $node, $filter);
if ($expr instanceof Expr) {
return $expr;
}
}
$expr = $this->resolveFromChildCond($stmt, $filter);
if ($expr instanceof Expr) {
return $expr;
}
return $this->resolveOnReturnOrExpression($stmt, $filter);
}
/**
* @param callable(Node $node): bool $filter
*/
private function resolveOnReturnOrExpression(
Node|Switch_|Return_|Expression|Echo_ $stmt,
callable $filter
): ?Expr {
if (! $stmt instanceof Return_ && ! $stmt instanceof Expression) {
return null;
}
if (! $stmt->expr instanceof Expr) {
return null;
}
return $this->resolveExpr($stmt, $stmt->expr, $filter);
}
/**
* @param Expr[]|Expr $exprs
* @param callable(Node $node): bool $filter
*/
private function resolveExpr(
Node|Switch_|Return_|Expression|Echo_ $stmt,
array|Expr $exprs,
callable $filter
): ?Expr {
$node = $this->betterNodeFinder->findFirst($exprs, $filter);
if (! $node instanceof Expr) {
return null;
}
$stmtScope = $stmt->getAttribute(AttributeKey::SCOPE);
$exprScope = $node->getAttribute(AttributeKey::SCOPE);
if (! $stmtScope instanceof Scope || ! $exprScope instanceof Scope) {
return null;
}
if ($stmtScope->getParentScope() === $exprScope->getParentScope()) {
return $node;
}
return null;
}
/**
* @param callable(Node $node): bool $filter
*/
private function resolveFromChildCond(
Node|Switch_|Return_|Expression|Echo_ $stmt,
callable $filter
): null|Expr {
if (! $stmt instanceof If_ && ! $stmt instanceof Switch_) {
return null;
}
$stmts = $stmt instanceof If_
? $stmt->elseifs
: $stmt->cases;
foreach ($stmts as $stmt) {
if (! $stmt->cond instanceof Expr) {
continue;
}
$expr = $this->resolveExpr($stmt, $stmt->cond, $filter);
if ($expr instanceof Expr) {
return $expr;
}
}
return null;
}
}