Repository navigation
[Php85] Skip nested and remaining write contexts on ArrayFirstLastRector - #9081
Merged
Merged
Conversation
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.
Follow-up to #9079 and #9080.
ArrayFirstLastRectorstill rewrote array dim fetches in write context when the write goes through a nested dim fetch, a by-ref array item, a foreach target or a by-ref return:Most of these are fatal errors ("Can't use function return value in write context"), the rest silently write to a temporary copy.
foreach ($other as $items[array_key_last($items)])(value or key target) crashed with "Scope not available on Variable".Same approach as
AssignedToNodeVisitor::markAssignOpVar(): mark every array dim fetch in the chain, since a write to$array[$key][0]writes to$array[$key]too. OnlyArrayDimFetchnodes are marked, soPropertyFetch/Variableconsumers (ContextAnalyzer,PropertyManipulator,VariableAnalyzer) are not affected.ContextNodeVisitor:unset()and++/--mark the chain (IS_UNSET_VAR,IS_INCREMENT_OR_DECREMENT).ByRefNodeVisitor:=&, by-refforeach, by-ref array item,fn &()andreturnin a by-ref function-like mark the chainIS_BYREF_VAR.AssignedToNodeVisitor: foreach key/value targets, including list items, mark the chainIS_BEING_ASSIGNED.CallLikeReflectionNodeVisitor: the by-ref arg shape matches a func call at any level of the chain and marks the whole chain.ArrayFirstLastRector::shouldSkip(): attribute checks run beforeScopeFetcher::fetch(), which avoids the foreach target crash.Added
skip_nested_write_context.php.incandnested_read_context.php.inc(nested reads, by-value foreach/arrow fn/return still change).Performance
bench/array_dim_fetch_write_context_bench.phpparses a dim-fetch-heavy class (120 methods, 2 520 array dim fetches, every write context above), then times the 4 decorating visitors alone andArrayFirstLastRector::refactor()on everyArrayDimFetch. CPU time (user+sys), 200 repeats, warmup excluded, 3 interleaved runs per branch:refactor(), us/callThe first version of this PR doubled the decoration time (~98 ms/traverse): nested shapes like
count($items[array_key_first($items)]['k'])now reach the by-ref param lookup, andParametersAcceptorSelectorVariantsWrapper::select()resolves every arg type through the scope. By-ref is declared on the parameter, so the lookup now usesParametersAcceptorSelector::combineAcceptors($reflection->getVariants()), same as the variadic closure decoration in this visitor.refactor()is faster because the attribute checks now run beforeScopeFetcher::fetch().The remaining +10 % is on a file where every method has nested by-ref writes, real code has far fewer. Existing visitor benches, main vs this PR, CPU:
by_ref_node_visitor_benchms/traversecontext_node_visitor_benchms/traversecall_like_reflection_visitor_array_arg_benchus/callcall_like_reflection_visitor_benchus/call