Make WordPress Core

Opened 6 weeks ago

Last modified 5 weeks ago

#65598 new enhancement

Use array_first() to get the first array element

Reported by: Soean Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: coding-standards

Description

Now that array_first() (added in PHP 8.5, polyfilled in wp-includes/compat.php via #63853) is available in core, a few spots can be simplified to use it directly instead of the older array_values( ... )[0] / array_slice( ..., 0, 1 )[0] constructs. This improves readability without changing behavior.

Change History (5)

This ticket was mentioned in PR #12418 on WordPress/wordpress-develop by @Soean.


6 weeks ago
#1

Replace array_values( ... )[0] and array_slice( ..., 0, 1 )[0] constructs with the array_first() function (added in PHP 8.5, polyfilled in compat.php) for improved readability.

Trac ticket: https://core.trac.wordpress.org/ticket/65598

This ticket was mentioned in PR #12460 on WordPress/wordpress-develop by @im3dabasia1.


6 weeks ago
#2

  • Keywords has-unit-tests added

Trac link: https://core.trac.wordpress.org/ticket/65598

Replace array_values( ... )[0] and array_slice( ..., 0, 1 )[0] constructs with the array_first() function (added in PHP 8.5, polyfilled in compat.php) for improved readability.

This ticket was mentioned in PR #12460 on WordPress/wordpress-develop by @im3dabasia1.


6 weeks ago
#3

Trac link: https://core.trac.wordpress.org/ticket/65598

Replace array_values( ... )[0] and array_slice( ..., 0, 1 )[0] constructs with the array_first() function (added in PHP 8.5, polyfilled in compat.php) for improved readability.

#4 @SergeyBiryukov
5 weeks ago

In 62695:

Coding Standards: Use array_first() to get the first array element.

This replaces array_values( ... )[0] and array_slice( ..., 0, 1 )[0] constructs with the array_first() function added in PHP 8.5 for improved readability.

WordPress core includes a polyfill for array_first() on PHP < 8.5 as of WordPress 6.9, so the change works on every supported PHP version without raising the minimum requirement.

Follow-up to [9685], [50995], [60672].

Props Soean, mukesh27.
See #65598.

Note: See TracTickets for help on using tickets.