[Php85] Skip nested compound assignment in ArrayFirstLastRector - #8425
Merged
TomasVotruba merged 1 commit intoSep 1, 2026
Merged
Conversation
$array[array_key_last($array)][0] += 1 was rewritten to array_last($array)[0] += 1. That reads the last element into a temporary and writes to the copy, so the assignment is lost. PHP reports nothing, which makes the change silent. IS_ASSIGN_OP_VAR was only set on the outermost node of the assignment target, so the rule saw the inner dim fetch as an ordinary read. Marking the whole chain reflects what happens: $array[$key][0] += 1 writes to $array[$key] too. The plain assignment form was already skipped through PHPStan's Scope::isInExpressionAssign(); a fixture now covers it so that stays true.
Member
|
Thank you |
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.
Description
ArrayFirstLastRectorrewrites$array[array_key_last($array)][0] += 1toarray_last($array)[0] += 1.array_last()returns a value rather than a reference, so the compound assignment writes into a temporary and is discarded, silently:shouldSkip()checksIS_ASSIGN_OP_VAR, butAssignedToNodeVisitorsets that attribute only on the outermost node of the assignment target. For$array[$key][0] += 1that is the outer dim fetch, while the rule matches the inner one, so the guard does not apply. The plain assignment form was already skipped through PHPStan'sScope::isInExpressionAssign(), which is why the existingskip_as_assign_opfixture did not catch this.Fix
AssignedToNodeVisitornow marks the whole dim fetch chain, which reflects what the assignment does:$array[$key][0] += 1writes to$array[$key]as well.ArrayDimFetchToMethodCallRectorreads the same attribute and benefits too.Fixtures for both the compound and the plain nested form are added.