Skip to content

Commit cf8a2eb

Browse files
authored
[DeadCode] Speed up RemoveAlwaysTrueIfConditionRector with single node traversal (#8653)
* [CodeQuality] Speed up ExplicitNullableObjectInstanceRector with cheap bail before native type resolution * [DeadCode] Speed up RemoveAlwaysTrueIfConditionRector with single node traversal
1 parent 5604bcb commit cf8a2eb

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

‎rules/CodeQuality/NodeAnalyzer/ExplicitBoolConditionResolver.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
namespace Rector\CodeQuality\NodeAnalyzer;
66

77
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\BinaryOp;
89
use PhpParser\Node\Expr\BooleanNot;
910
use PhpParser\Node\Expr\Cast\Bool_;
11+
use PhpParser\Node\Expr\Empty_;
12+
use PhpParser\Node\Expr\Instanceof_;
13+
use PhpParser\Node\Expr\Isset_;
1014
use PhpParser\Node\Expr\Ternary;
1115
use PhpParser\Node\Stmt\ElseIf_;
1216
use PhpParser\Node\Stmt\If_;
@@ -40,11 +44,24 @@ public function resolve(If_|ElseIf_|Ternary $node): ?ExplicitBoolCondition
4044
return null;
4145
}
4246

47+
// cheap bail - these shapes can never be a nullable object, skip costly native type resolution
48+
if ($this->isNeverNullableObject($conditionNode)) {
49+
return null;
50+
}
51+
4352
$conditionStaticType = $this->nodeTypeResolver->getNativeType($conditionNode);
4453
if ($conditionStaticType instanceof MixedType || $conditionStaticType->isBoolean()->yes()) {
4554
return null;
4655
}
4756

4857
return new ExplicitBoolCondition($conditionNode, $isNegated);
4958
}
59+
60+
private function isNeverNullableObject(Expr $expr): bool
61+
{
62+
return $expr instanceof BinaryOp
63+
|| $expr instanceof Instanceof_
64+
|| $expr instanceof Empty_
65+
|| $expr instanceof Isset_;
66+
}
5067
}

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,30 +197,17 @@ private function shouldSkipFromVariable(Expr $expr, Scope $scope): bool
197197

198198
private function shouldSkipExpr(Expr $expr): bool
199199
{
200-
$hasNonStaticNode = (bool) $this->betterNodeFinder->findInstancesOf(
201-
$expr,
202-
[
203-
PropertyFetch::class,
204-
StaticPropertyFetch::class,
205-
ArrayDimFetch::class,
206-
MethodCall::class,
207-
StaticCall::class,
208-
]
209-
);
210-
211-
if ($hasNonStaticNode) {
212-
return true;
213-
}
214-
215-
/** @var FuncCall[] $funcCalls */
216-
$funcCalls = $this->betterNodeFinder->findInstancesOf($expr, [FuncCall::class]);
217-
foreach ($funcCalls as $funcCall) {
218-
if ($this->isNames($funcCall, self::RUNTIME_STATE_FUNCTIONS)) {
200+
return $this->betterNodeFinder->findFirst($expr, function (Node $node): bool {
201+
if ($node instanceof PropertyFetch
202+
|| $node instanceof StaticPropertyFetch
203+
|| $node instanceof ArrayDimFetch
204+
|| $node instanceof MethodCall
205+
|| $node instanceof StaticCall) {
219206
return true;
220207
}
221-
}
222208

223-
return false;
209+
return $node instanceof FuncCall && $this->isNames($node, self::RUNTIME_STATE_FUNCTIONS);
210+
}) instanceof Node;
224211
}
225212

226213
private function refactorIfWithBooleanAnd(If_ $if): ?If_

0 commit comments

Comments
 (0)