Skip to content

[Php73] Add ArrayKeysToArrayKeyFirstLastRector - #8238

Merged
TomasVotruba merged 2 commits into
rectorphp:mainfrom
Soean:php73-array-keys-to-array-key-first-last
Aug 24, 2026
Merged

[Php73] Add ArrayKeysToArrayKeyFirstLastRector#8238
TomasVotruba merged 2 commits into
rectorphp:mainfrom
Soean:php73-array-keys-to-array-key-first-last

Conversation

@Soean

Copy link
Copy Markdown
Contributor

Adds ArrayKeysToArrayKeyFirstLastRector, which replaces reading a single key off array_keys() with the dedicated PHP 7.3 function:

$firstKey = current(array_keys($items));
$alsoFirst = reset(array_keys($items));
$lastKey = end(array_keys($items));
$firstKey = array_key_first($items);
$alsoFirst = array_key_first($items);
$lastKey = array_key_last($items);

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 is STMTS_AWARE and only matches the two-statement reset($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.
  • Named arguments, spread, and first-class callables on either call.
  • key(array_keys($items)), which always evaluates to 0 rather than the first key.

Note on empty arrays

current(array_keys([])) returns false, while array_key_first([]) returns null. The rule transforms regardless, matching how ArrayFirstLastRector handles the equivalent array_values($array)[0]array_first($array) case. Gating on a proven non-empty-array type would leave nearly nothing to fix. Happy to add such a guard if you would rather have it.

@samsonasiksamsonasik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downgrade rule seems needed first at

https://github.com/rectorphp/rector-downgrade-php/

If not in existing downgrade rule functionality about it.

Soeanand others added 2 commits August 25, 2026 00:27
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.
@TomasVotruba
TomasVotrubaforce-pushed the php73-array-keys-to-array-key-first-last branch from f21aaae to 30e67bbCompareAugust 24, 2026 23:33
@TomasVotruba
TomasVotruba marked this pull request as ready for review August 24, 2026 23:37
@TomasVotruba

Copy link
Copy Markdown
Member

Thanks for the feature 👍

We already have a downgrade rule that handles this.
https://github.com/rectorphp/rector-downgrade-php/blob/main/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php

@TomasVotruba
TomasVotruba merged commit 5b41311 into rectorphp:mainAug 24, 2026
47 of 48 checks passed
@Soean
Soean deleted the php73-array-keys-to-array-key-first-last branch August 25, 2026 08:00
Sign up for freeto 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.

3 participants

@Soean@TomasVotruba@samsonasik