Skip to content

Commit 4f2f67f

Browse files
authored
[perf] Speed up ArrayExplicitBoolCompareRector with cheap variable guard (#8603)
1 parent bcc119d commit 4f2f67f

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

‎rules/CodeQuality/Rector/If_/ArrayExplicitBoolCompareRector.php‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
use PhpParser\Node\Expr\Array_;
1010
use PhpParser\Node\Expr\BinaryOp\Identical;
1111
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
12+
use PhpParser\Node\Expr\BooleanNot;
1213
use PhpParser\Node\Expr\Ternary;
1314
use PhpParser\Node\Expr\Variable;
1415
use PhpParser\Node\Stmt\ElseIf_;
1516
use PhpParser\Node\Stmt\If_;
16-
use Rector\CodeQuality\NodeAnalyzer\ExplicitBoolConditionResolver;
17-
use Rector\CodeQuality\ValueObject\ExplicitBoolCondition;
1817
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
1918
use Rector\Rector\AbstractRector;
2019
use Rector\RuleDoc\CodeSample\CodeSample;
@@ -27,7 +26,6 @@ final class ArrayExplicitBoolCompareRector extends AbstractRector
2726
{
2827
public function __construct(
2928
private readonly ArrayTypeAnalyzer $arrayTypeAnalyzer,
30-
private readonly ExplicitBoolConditionResolver $explicitBoolConditionResolver,
3129
) {
3230
}
3331

@@ -75,39 +73,36 @@ public function getNodeTypes(): array
7573
*/
7674
public function refactor(Node $node): ?Node
7775
{
78-
$explicitBoolCondition = $this->explicitBoolConditionResolver->resolve($node);
79-
if (! $explicitBoolCondition instanceof ExplicitBoolCondition) {
76+
// skip short ternary
77+
if ($node instanceof Ternary && ! $node->if instanceof Expr) {
8078
return null;
8179
}
8280

83-
$expr = $explicitBoolCondition->getConditionNode();
84-
if (! $this->arrayTypeAnalyzer->isArrayType($expr)) {
85-
return null;
81+
if ($node->cond instanceof BooleanNot) {
82+
$conditionNode = $node->cond->expr;
83+
$isNegated = true;
84+
} else {
85+
$conditionNode = $node->cond;
86+
$isNegated = false;
8687
}
8788

88-
$binaryOp = $this->resolveArray($explicitBoolCondition->isNegated(), $expr);
89-
if (! $binaryOp instanceof Expr) {
89+
// cheap bail - rule only ever rewrites a bare variable, skip costly type resolution otherwise
90+
if (! $conditionNode instanceof Variable) {
9091
return null;
9192
}
9293

93-
$node->cond = $binaryOp;
94-
95-
return $node;
96-
}
97-
98-
private function resolveArray(bool $isNegated, Expr $expr): Identical|NotIdentical|null
99-
{
100-
if (! $expr instanceof Variable) {
94+
// array type already excludes mixed and bool, so a single native type check is enough
95+
if (! $this->arrayTypeAnalyzer->isArrayType($conditionNode)) {
10196
return null;
10297
}
10398

10499
$array = new Array_([]);
105100

106101
// compare === []
107-
if ($isNegated) {
108-
return new Identical($expr, $array);
109-
}
102+
$node->cond = $isNegated
103+
? new Identical($conditionNode, $array)
104+
: new NotIdentical($conditionNode, $array);
110105

111-
return new NotIdentical($expr, $array);
106+
return $node;
112107
}
113108
}

0 commit comments

Comments
 (0)