Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 50 additions & 24 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3186,30 +3186,7 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr
return false;
}

$nodeFinder = new NodeFinder();
$expressionToInvalidateClass = get_class($exprToInvalidate);
$found = $nodeFinder->findFirst([$expr], function (Node $node) use ($expressionToInvalidateClass, $exprStringToInvalidate): bool {
if (
$exprStringToInvalidate === '$this'
&& $node instanceof Name
&& (
in_array($node->toLowerString(), ['self', 'static', 'parent'], true)
|| ($this->getClassReflection() !== null && $this->getClassReflection()->is($this->resolveName($node)))
)
) {
return true;
}

if (!$node instanceof $expressionToInvalidateClass) {
return false;
}

$nodeString = $this->getNodeKey($node);

return $nodeString === $exprStringToInvalidate;
});

if ($found === null) {
if (!$this->containsExpressionToInvalidate($expr, get_class($exprToInvalidate), $exprStringToInvalidate)) {
return false;
}

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

/**
* Depth-first pre-order search for the invalidated expression, replacing a
* NodeFinder::findFirst() call - this runs for every (stored expression,
* invalidated expression) pair whose keys pass the substring pre-filter,
* so the traverser/visitor machinery overhead was significant.
*
* @param class-string<Expr> $expressionToInvalidateClass
*/
private function containsExpressionToInvalidate(Node $node, string $expressionToInvalidateClass, string $exprStringToInvalidate): bool
{
if (
$exprStringToInvalidate === '$this'
&& $node instanceof Name
&& (
in_array($node->toLowerString(), ['self', 'static', 'parent'], true)
|| ($this->getClassReflection() !== null && $this->getClassReflection()->is($this->resolveName($node)))
)
) {
return true;
}

if (
$node instanceof $expressionToInvalidateClass
&& $this->getNodeKey($node) === $exprStringToInvalidate
) {
return true;
}

foreach ($node->getSubNodeNames() as $subNodeName) {
$subNode = $node->$subNodeName;
if ($subNode instanceof Node) {
if ($this->containsExpressionToInvalidate($subNode, $expressionToInvalidateClass, $exprStringToInvalidate)) {
return true;
}
} elseif (is_array($subNode)) {
foreach ($subNode as $subNodeItem) {
if (
$subNodeItem instanceof Node
&& $this->containsExpressionToInvalidate($subNodeItem, $expressionToInvalidateClass, $exprStringToInvalidate)
) {
return true;
}
}
}
}

return false;
}

private function isPrivatePropertyOfDifferentClass(Expr $expr, ClassReflection $invalidatingClass): bool
{
if ($expr instanceof Expr\StaticPropertyFetch || $expr instanceof PropertyFetch) {
Expand Down
Loading