Skip to content

Commit d941625

Browse files
staabmondrejmirtes
andauthored
Replace NodeFinder with direct recursion in shouldInvalidateExpression()
Co-authored-by: Ondrej Mirtes <ondrej@mirtes.cz>
1 parent 324cd4e commit d941625

1 file changed

Lines changed: 50 additions & 24 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,30 +3186,7 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr
31863186
return false;
31873187
}
31883188

3189-
$nodeFinder = new NodeFinder();
3190-
$expressionToInvalidateClass = get_class($exprToInvalidate);
3191-
$found = $nodeFinder->findFirst([$expr], function (Node $node) use ($expressionToInvalidateClass, $exprStringToInvalidate): bool {
3192-
if (
3193-
$exprStringToInvalidate === '$this'
3194-
&& $node instanceof Name
3195-
&& (
3196-
in_array($node->toLowerString(), ['self', 'static', 'parent'], true)
3197-
|| ($this->getClassReflection() !== null && $this->getClassReflection()->is($this->resolveName($node)))
3198-
)
3199-
) {
3200-
return true;
3201-
}
3202-
3203-
if (!$node instanceof $expressionToInvalidateClass) {
3204-
return false;
3205-
}
3206-
3207-
$nodeString = $this->getNodeKey($node);
3208-
3209-
return $nodeString === $exprStringToInvalidate;
3210-
});
3211-
3212-
if ($found === null) {
3189+
if (!$this->containsExpressionToInvalidate($expr, get_class($exprToInvalidate), $exprStringToInvalidate)) {
32133190
return false;
32143191
}
32153192

@@ -3232,6 +3209,55 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr
32323209
return true;
32333210
}
32343211

3212+
/**
3213+
* Depth-first pre-order search for the invalidated expression, replacing a
3214+
* NodeFinder::findFirst() call - this runs for every (stored expression,
3215+
* invalidated expression) pair whose keys pass the substring pre-filter,
3216+
* so the traverser/visitor machinery overhead was significant.
3217+
*
3218+
* @param class-string<Expr> $expressionToInvalidateClass
3219+
*/
3220+
private function containsExpressionToInvalidate(Node $node, string $expressionToInvalidateClass, string $exprStringToInvalidate): bool
3221+
{
3222+
if (
3223+
$exprStringToInvalidate === '$this'
3224+
&& $node instanceof Name
3225+
&& (
3226+
in_array($node->toLowerString(), ['self', 'static', 'parent'], true)
3227+
|| ($this->getClassReflection() !== null && $this->getClassReflection()->is($this->resolveName($node)))
3228+
)
3229+
) {
3230+
return true;
3231+
}
3232+
3233+
if (
3234+
$node instanceof $expressionToInvalidateClass
3235+
&& $this->getNodeKey($node) === $exprStringToInvalidate
3236+
) {
3237+
return true;
3238+
}
3239+
3240+
foreach ($node->getSubNodeNames() as $subNodeName) {
3241+
$subNode = $node->$subNodeName;
3242+
if ($subNode instanceof Node) {
3243+
if ($this->containsExpressionToInvalidate($subNode, $expressionToInvalidateClass, $exprStringToInvalidate)) {
3244+
return true;
3245+
}
3246+
} elseif (is_array($subNode)) {
3247+
foreach ($subNode as $subNodeItem) {
3248+
if (
3249+
$subNodeItem instanceof Node
3250+
&& $this->containsExpressionToInvalidate($subNodeItem, $expressionToInvalidateClass, $exprStringToInvalidate)
3251+
) {
3252+
return true;
3253+
}
3254+
}
3255+
}
3256+
}
3257+
3258+
return false;
3259+
}
3260+
32353261
private function isPrivatePropertyOfDifferentClass(Expr $expr, ClassReflection $invalidatingClass): bool
32363262
{
32373263
if ($expr instanceof Expr\StaticPropertyFetch || $expr instanceof PropertyFetch) {

0 commit comments

Comments
 (0)