Repository navigation
[scope] Swap instanceof chain for property_exists guard in scope expr dispatch - #8789
Closed
TomasVotruba wants to merge 2 commits into
Closed
TomasVotruba wants to merge 2 commits into
TomasVotruba wants to merge 2 commits into
Conversation
TomasVotruba
marked this pull request as draft
October 23, 2026 19:07
Member
Author
|
Folded the bench into #8790 together with the NoLongInstanceofChainRule. Note: the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces the 17-branch
instanceofchain inPHPStanNodeScopeResolver(the guard that propagates scope to$node->expr) withproperty_exists($node, 'expr')plus 5 explicit exclusions.Equivalence
A raw
property_existsswap is NOT equivalent - it also matchesAssign,AssignOp,AssignRef,ArrowFunctionandForeach_. The first three andForeach_have dedicated branches right below and must fall through to them;ArrowFunction->exprmust not inherit the scope here.Excluding those 5 makes it match the chain exactly. Verified against all 42 expr-bearing php-parser node classes (
AssignOpis abstract and covers its 13 subclasses): the guarded predicate matches the same 25 node types the chain lists, no more, no less.Speed
The bare property lookup is ~3.6x cheaper than the chain (~184 ns vs ~668 ns/call). The shipped guarded variant adds 5
instanceofback for the expr-bearing case, but the common path (non-expr nodes, the majority of visited nodes) short-circuits on the singleproperty_existsmiss instead of walking 17instanceof.Bench
bench/scope_expr_dispatch_bench.phpprints the divergent set for the raw swap, confirms the guarded variant is identical to the chain, and the CPU ratio:Full
vendor/bin/phpunitgreen (4869 tests).property_existsis whitelisted for this one file inphpstan.neon.