-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathStmtsManipulator.php
More file actions
114 lines (94 loc) · 3.25 KB
/
StmtsManipulator.php
File metadata and controls
114 lines (94 loc) · 3.25 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
<?php
declare(strict_types=1);
namespace Rector\NodeManipulator;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Webmozart\Assert\Assert;
final readonly class StmtsManipulator
{
public function __construct(
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private BetterNodeFinder $betterNodeFinder,
private NodeComparator $nodeComparator,
private ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer
) {
}
/**
* @param Stmt[] $stmts
*/
public function getUnwrappedLastStmt(array $stmts): null|Expr|Stmt
{
if ($stmts === []) {
return null;
}
$lastStmtKey = array_key_last($stmts);
$lastStmt = $stmts[$lastStmtKey];
if ($lastStmt instanceof Expression) {
$lastStmt->expr->setAttribute(AttributeKey::COMMENTS, $lastStmt->getAttribute(AttributeKey::COMMENTS));
return $lastStmt->expr;
}
return $lastStmt;
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
public function filterOutExistingStmts(ClassMethod $classMethod, array $stmts): array
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $node) use (&$stmts): null {
foreach ($stmts as $key => $assign) {
if (! $this->nodeComparator->areNodesEqual($node, $assign)) {
continue;
}
unset($stmts[$key]);
}
return null;
}
);
return $stmts;
}
/**
* @param StmtsAwareInterface $stmtsAware
*/
public function isVariableUsedInNextStmt(Node $stmtsAware, int $jumpToKey, string $variableName): bool
{
Assert::propertyExists($stmtsAware, 'stmts');
if ($stmtsAware->stmts === null) {
return false;
}
$lastKey = array_key_last($stmtsAware->stmts);
$stmts = [];
for ($key = $jumpToKey; $key <= $lastKey; ++$key) {
if (! isset($stmtsAware->stmts[$key])) {
// can be just removed
continue;
}
$stmts[] = $stmtsAware->stmts[$key];
}
if ($stmtsAware instanceof TryCatch) {
$stmts = array_merge($stmts, $stmtsAware->catches);
if ($stmtsAware->finally instanceof Finally_) {
$stmts = array_merge($stmts, $stmtsAware->finally->stmts);
}
}
$variable = new Variable($variableName);
return (bool) $this->betterNodeFinder->findFirst(
$stmts,
fn (Node $subNode): bool => $this->exprUsedInNodeAnalyzer->isUsed($subNode, $variable)
);
}
}