Skip to content

[Php85] Skip nested and remaining write contexts on ArrayFirstLastRector - #9081

Merged
TomasVotruba merged 2 commits into
mainfrom
fix/array-first-last-nested-write-context
Nov 27, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
fix/array-first-last-nested-write-context

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #9079 and #9080. ArrayFirstLastRector still 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:

-++$items[array_key_last($items)]['k'];
+++array_last($items)['k'];

-unset($items[array_key_last($items)]['k']);
+unset(array_last($items)['k']);

-$list = [&$items[array_key_last($items)]];
+$list = [&array_last($items)];

-$ref = &$items[array_key_last($items)]['k'];
+$ref = &array_last($items)['k'];

-sort($items[array_key_first($items)]['k']);
+sort(array_first($items)['k']);

-foreach ($items[array_key_last($items)]['k'] as &$item) {
+foreach (array_last($items)['k'] as &$item) {

-$fn = fn &() => $items[array_key_last($items)];
+$fn = fn &() => array_last($items);

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. Only ArrayDimFetch nodes are marked, so PropertyFetch/Variable consumers (ContextAnalyzer, PropertyManipulator, VariableAnalyzer) are not affected.

  • ContextNodeVisitor: unset() and ++/-- mark the chain (IS_UNSET_VAR, IS_INCREMENT_OR_DECREMENT).
  • ByRefNodeVisitor: =&, by-ref foreach, by-ref array item, fn &() and return in a by-ref function-like mark the chain IS_BYREF_VAR.
  • AssignedToNodeVisitor: foreach key/value targets, including list items, mark the chain IS_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 before ScopeFetcher::fetch(), which avoids the foreach target crash.

Added skip_nested_write_context.php.inc and nested_read_context.php.inc (nested reads, by-value foreach/arrow fn/return still change).

Performance

bench/array_dim_fetch_write_context_bench.php parses a dim-fetch-heavy class (120 methods, 2 520 array dim fetches, every write context above), then times the 4 decorating visitors alone and ArrayFirstLastRector::refactor() on every ArrayDimFetch. CPU time (user+sys), 200 repeats, warmup excluded, 3 interleaved runs per branch:

main this PR diff
decorating visitors, ms/traverse 50.1 / 52.7 / 52.6 55.9 / 57.7 / 57.9 ~ +10 %
refactor(), us/call 7.1 / 8.1 / 8.3 5.7 / 6.6 / 7.1 ~ -18 %

The 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, and ParametersAcceptorSelectorVariantsWrapper::select() resolves every arg type through the scope. By-ref is declared on the parameter, so the lookup now uses ParametersAcceptorSelector::combineAcceptors($reflection->getVariants()), same as the variadic closure decoration in this visitor. refactor() is faster because the attribute checks now run before ScopeFetcher::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:

bench main this PR
by_ref_node_visitor_bench ms/traverse 8.7 / 8.7 / 9.2 10.0 / 9.2 / 10.0
context_node_visitor_bench ms/traverse 22.2 / 19.7 / 19.5 21.0 / 19.7 / 19.5
call_like_reflection_visitor_array_arg_bench us/call 2.79 / 2.81 / 3.07 2.76 / 3.02 / 2.76
call_like_reflection_visitor_bench us/call 2.41 / 2.45 / 2.33 2.39 / 2.31 / 2.36

@TomasVotruba
TomasVotruba merged commit bdf2987 into main Nov 27, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix/array-first-last-nested-write-context branch November 27, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant