Skip to content

Commit 2326ffe

Browse files
authored
[perf] Speed up DeadCode rules with cheap guards before type checks, reflection and subtree walks (#8666)
1 parent 749f8b3 commit 2326ffe

7 files changed

Lines changed: 43 additions & 27 deletions

‎rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public function refactor(Node $node): ?Node
8989
continue;
9090
}
9191

92+
if (! $stmt->expr->var instanceof Variable && ! $stmt->expr->var instanceof PropertyFetch && ! $stmt->expr->var instanceof StaticPropertyFetch) {
93+
continue;
94+
}
95+
9296
// early check self referencing, ensure that variable not re-used
9397
if ($this->isSelfReferencing($nextAssign)) {
9498
continue;
@@ -116,10 +120,6 @@ public function refactor(Node $node): ?Node
116120
continue;
117121
}
118122

119-
if (! $stmt->expr->var instanceof Variable && ! $stmt->expr->var instanceof PropertyFetch && ! $stmt->expr->var instanceof StaticPropertyFetch) {
120-
continue;
121-
}
122-
123123
// side effect may throw exception, and may be handled by catch block
124124
if ($node instanceof TryCatch && $this->sideEffectNodeDetector->detectCallExpr($nextAssign->expr)) {
125125
continue;

‎rules/DeadCode/Rector/FuncCall/RemoveFilterVarOnExactTypeRector.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function refactor(Node $node): ?Node
7272

7373
$constantFilterName = $secondArgValue->name->toString();
7474

75+
if (! in_array(
76+
$constantFilterName,
77+
['FILTER_VALIDATE_INT', 'FILTER_VALIDATE_FLOAT', 'FILTER_VALIDATE_BOOLEAN', 'FILTER_VALIDATE_BOOL'],
78+
true
79+
)) {
80+
return null;
81+
}
82+
7583
$valueType = $this->nodeTypeResolver->getNativeType($firstArgValue);
7684

7785
if ($constantFilterName === 'FILTER_VALIDATE_INT' && $valueType->isInteger()->yes()) {

‎rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ private function isInstanceofTheSameType(Instanceof_ $instanceof): ?bool
154154
return null;
155155
}
156156

157-
$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
158-
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
157+
$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
158+
if (! $exprType instanceof ObjectType) {
159159
return null;
160160
}
161161

162-
$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
163-
if (! $exprType instanceof ObjectType) {
162+
$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
163+
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
164164
return null;
165165
}
166166

‎rules/DeadCode/Rector/If_/RemoveOverriddenAssignBeforeIfElseRector.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ public function refactor(Node $node): ?Node
9898
$hasChanged = false;
9999

100100
foreach ($node->stmts as $key => $stmt) {
101-
$variableName = $this->matchOverridableAssignVariableName($stmt);
102-
if ($variableName === null) {
103-
continue;
104-
}
105-
106101
$nextStmt = $node->stmts[$key + 1] ?? null;
107102
if (! $nextStmt instanceof If_) {
108103
continue;
@@ -112,6 +107,11 @@ public function refactor(Node $node): ?Node
112107
continue;
113108
}
114109

110+
$variableName = $this->matchOverridableAssignVariableName($stmt);
111+
if ($variableName === null) {
112+
continue;
113+
}
114+
115115
// the initial value is still read when the condition uses it
116116
if ($this->isVariableUsedInNode($nextStmt->cond, $variableName)) {
117117
continue;

‎rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ private function areNumberType(BinaryOp $binaryOp): bool
155155

156156
private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
157157
{
158+
if (! $this->isLiteralZero($binaryOp->left) && ! $this->isLiteralZero($binaryOp->right)) {
159+
return null;
160+
}
161+
158162
if (! $this->areNumberType($binaryOp)) {
159163
return null;
160164
}
@@ -229,6 +233,10 @@ private function processBinaryMulAndDiv(Mul|Div $binaryOp): ?Expr
229233
return null;
230234
}
231235

236+
if (! $this->isLiteralOne($binaryOp->left) && ! $this->isLiteralOne($binaryOp->right)) {
237+
return null;
238+
}
239+
232240
if (! $this->areNumberType($binaryOp)) {
233241
return null;
234242
}

‎rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ private function isParentStaticCall(Expr $expr): bool
141141

142142
private function shouldSkipClass(Class_ $class): bool
143143
{
144-
// skip cases when parent class reflection is not found
145-
if ($class->extends instanceof FullyQualified && ! $this->reflectionProvider->hasClass(
146-
$class->extends->toString()
147-
)) {
144+
// currently the classMethodManipulator isn't able to find usages of anonymous classes
145+
if ($this->classAnalyzer->isAnonymousClass($class)) {
148146
return true;
149147
}
150148

151-
// currently the classMethodManipulator isn't able to find usages of anonymous classes
152-
return $this->classAnalyzer->isAnonymousClass($class);
149+
// skip cases when parent class reflection is not found
150+
return $class->extends instanceof FullyQualified && ! $this->reflectionProvider->hasClass(
151+
$class->extends->toString()
152+
);
153153
}
154154

155155
private function doesCalledMethodExistInParent(StaticCall $staticCall, Class_ $class): bool

‎rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,26 @@ public function refactor(Node $node): ?Node
9595

9696
$identicalOrEqual = $stmt->cond;
9797

98-
if ($this->sideEffectNodeDetector->detect($identicalOrEqual->right)) {
98+
// next if must be return of the same var
99+
$nextStmt = $node->stmts[$key + 1] ?? null;
100+
if (! $nextStmt instanceof Return_) {
99101
continue;
100102
}
101103

102-
if (! $this->nodeComparator->areNodesEqual($identicalOrEqual->right, $soleIfReturn->expr)) {
104+
if (! $nextStmt->expr instanceof Expr) {
103105
continue;
104106
}
105107

106-
$comparedVariable = $identicalOrEqual->left;
107-
108-
// next if must be return of the same var
109-
$nextStmt = $node->stmts[$key + 1] ?? null;
110-
if (! $nextStmt instanceof Return_) {
108+
if ($this->sideEffectNodeDetector->detect($identicalOrEqual->right)) {
111109
continue;
112110
}
113111

114-
if (! $nextStmt->expr instanceof Expr) {
112+
if (! $this->nodeComparator->areNodesEqual($identicalOrEqual->right, $soleIfReturn->expr)) {
115113
continue;
116114
}
117115

116+
$comparedVariable = $identicalOrEqual->left;
117+
118118
if (! $this->nodeComparator->areNodesEqual($nextStmt->expr, $comparedVariable)) {
119119
continue;
120120
}

0 commit comments

Comments
 (0)