-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathLivingCodeManipulator.php
More file actions
149 lines (126 loc) · 4.38 KB
/
LivingCodeManipulator.php
File metadata and controls
149 lines (126 loc) · 4.38 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
<?php
declare(strict_types=1);
namespace Rector\DeadCode\NodeManipulator;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
use PhpParser\Node\Expr\BinaryOp\Pipe;
use PhpParser\Node\Expr\BitwiseNot;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\NodeTypeResolver;
final readonly class LivingCodeManipulator
{
public function __construct(
private NodeTypeResolver $nodeTypeResolver
) {
}
/**
* @return Expr[]|mixed[]
*/
public function keepLivingCodeFromExpr(Node | int | string | null $expr): array
{
if (! $expr instanceof Expr) {
return [];
}
if ($expr instanceof Closure || $expr instanceof Scalar || $expr instanceof ConstFetch) {
return [];
}
if ($this->isNestedExpr($expr)) {
return $this->keepLivingCodeFromExpr($expr->expr);
}
if ($expr instanceof Variable) {
return $this->keepLivingCodeFromExpr($expr->name);
}
if ($expr instanceof PropertyFetch) {
return [...$this->keepLivingCodeFromExpr($expr->var), ...$this->keepLivingCodeFromExpr($expr->name)];
}
if ($expr instanceof ArrayDimFetch) {
$type = $this->nodeTypeResolver->getType($expr->var);
if ($type instanceof ObjectType) {
$objectType = new ObjectType('ArrayAccess');
if ($objectType->isSuperTypeOf($type)->yes()) {
return [$expr];
}
}
return [...$this->keepLivingCodeFromExpr($expr->var), ...$this->keepLivingCodeFromExpr($expr->dim)];
}
if ($expr instanceof ClassConstFetch || $expr instanceof StaticPropertyFetch) {
return [...$this->keepLivingCodeFromExpr($expr->class), ...$this->keepLivingCodeFromExpr($expr->name)];
}
if ($this->isBinaryOpWithoutChange($expr)) {
/** @var BinaryOp $binaryOp */
$binaryOp = $expr;
return $this->processBinary($binaryOp);
}
if ($expr instanceof Instanceof_) {
return [...$this->keepLivingCodeFromExpr($expr->expr), ...$this->keepLivingCodeFromExpr($expr->class)];
}
if ($expr instanceof Isset_) {
return $this->processIsset($expr);
}
return [$expr];
}
private function isNestedExpr(Expr $expr): bool
{
return $expr instanceof Cast ||
$expr instanceof Empty_ ||
$expr instanceof UnaryMinus ||
$expr instanceof UnaryPlus ||
$expr instanceof BitwiseNot ||
$expr instanceof BooleanNot ||
$expr instanceof Clone_;
}
private function isBinaryOpWithoutChange(Expr $expr): bool
{
if (! $expr instanceof BinaryOp) {
return false;
}
return ! (
$expr instanceof LogicalAnd ||
$expr instanceof BooleanAnd ||
$expr instanceof LogicalOr ||
$expr instanceof BooleanOr ||
$expr instanceof Coalesce ||
$expr instanceof Pipe
);
}
/**
* @return Expr[]
*/
private function processBinary(BinaryOp $binaryOp): array
{
return [...$this->keepLivingCodeFromExpr($binaryOp->left), ...$this->keepLivingCodeFromExpr($binaryOp->right)];
}
/**
* @return mixed[]
*/
private function processIsset(Isset_ $isset): array
{
$livingExprs = [];
foreach ($isset->vars as $expr) {
$livingExprs = [...$livingExprs, ...$this->keepLivingCodeFromExpr($expr)];
}
return $livingExprs;
}
}