Uh oh!
There was an error while loading. Please reload this page.
[Php73] Add ArrayKeysToArrayKeyFirstLastRector - #8238
Merged
TomasVotruba merged 2 commits intoAug 24, 2026
Conversation
samsonasik
requested changes
Jul 31, 2026
samsonasik
left a comment
Member
There was a problem hiding this comment.
The downgrade rule seems needed first at
https://github.com/rectorphp/rector-downgrade-php/
If not in existing downgrade rule functionality about it.
Reading a single key off array_keys() builds the full key array only to discard all but one entry. Turn it into the dedicated function instead: current(array_keys($items)) -> array_key_first($items) reset(array_keys($items)) -> array_key_first($items) end(array_keys($items)) -> array_key_last($items) This complements ArrayKeyFirstLastRector, which only covers the two-statement `reset($items); key($items);` form. array_keys() with a search value is skipped, as it returns just the matching keys, so its first entry is not the array's first key. Named args, spread, and first-class callables are skipped as well.
TomasVotrubaforce-pushed
the
php73-array-keys-to-array-key-first-last
branch
from
August 24, 2026 23:33
f21aaae to
30e67bbCompareTomasVotruba
marked this pull request as ready for review
August 24, 2026 23:37
TomasVotruba
commented
Aug 24, 2026
Member
Thanks for the feature 👍 We already have a downgrade rule that handles this. |
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
Adds
ArrayKeysToArrayKeyFirstLastRector, which replaces reading a single key offarray_keys()with the dedicated PHP 7.3 function:array_keys()materializes every key just to have all but one thrown away. Beyond the wasted allocation, the intent is easier to read once the dedicated function is used.This complements the existing
ArrayKeyFirstLastRector, which isSTMTS_AWAREand only matches the two-statementreset($items); key($items);form, the nested expression is not covered by it, so the two rules do not overlap.Skipped cases
array_keys()with a search value,array_keys($items, 'value')returns only the matching keys, so its first entry is not the array's first key. Transforming this would be a behavior change.key(array_keys($items)), which always evaluates to0rather than the first key.Note on empty arrays
current(array_keys([]))returnsfalse, whilearray_key_first([])returnsnull. The rule transforms regardless, matching howArrayFirstLastRectorhandles the equivalentarray_values($array)[0]→array_first($array)case. Gating on a provennon-empty-arraytype would leave nearly nothing to fix. Happy to add such a guard if you would rather have it.