Skip to content

Code Modernization: Use array_key_first() to read the first key of an array - #12790

Open
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:code-modernization/array-key-first
Open

Code Modernization: Use array_key_first() to read the first key of an array#12790
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:code-modernization/array-key-first

Conversation

@Soean

@Soean Soean commented Jul 31, 2026

Copy link
Copy Markdown
Member

Reading the first key of an array through array_keys() allocates a full array of every key just to keep one entry and discard the rest. array_key_first() reads the first bucket directly.

Follow-up to 65773, which is scoped to the two nested current( array_keys( $array ) ) call sites and is addressed in #12785. This PR covers a second shape of the same idea, which that ticket does not include: the key array is assigned to a variable first, then read on the next line.

$keys    = array_keys( $wp_registered_sidebars );
$sidebar = reset( $keys );
$sidebar = array_key_first( $wp_registered_sidebars );

13 occurrences across 11 files, in two shapes — reset( $keys ) and $keys[0]. In every case the intermediate variable existed only to carry the key array to the next line and is never read again afterwards.

One change that goes further

In spawn_cron() and _wp_cron() the surrounding check is dropped too:

$keys = array_keys( $crons );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
if ( array_key_first( $crons ) > $gmt_time ) {

Both functions return early a few lines above when $crons is empty, so isset( $keys[0] ) can never be false there. Happy to keep an explicit null !== $first guard instead if reviewers prefer the defensive form.

Behavior notes

  • On an empty array reset() returns false while array_key_first() returns null. None of the touched call sites compares the result with ===, and most sit behind an empty() guard.
  • Where the old code used $keys[0], the new code is strictly safer: $keys[0] emitted a notice on an empty array, array_key_first() returns null quietly.

… array.

Reading the first key of an array through `array_keys()` builds a complete array of every key only to keep one entry and throw the rest away. Replace that with `array_key_first()`, which reads the first bucket directly.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props soean.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant