Make WordPress Core

Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#45055 closed feature request (fixed)

Introduce new PHP cross-version compatibility functions, `array_key_first()`, `array_key_last()`

Reported by: desrosj's profile desrosj Owned by: hellofromtonya's profile hellofromTonya
Milestone: 5.9 Priority: normal
Severity: normal Version: 5.1
Component: General Keywords: php73 has-patch has-unit-tests commit
Focuses: Cc:

Description

PHP 7.3 will introduce two new functions, array_key_first(), and array_key_last(). The two functions return the first and last key of each array respectively, without affecting the internal state of the array (reset(), end() and key() do). The full RFC can be read here: https://wiki.php.net/rfc/array_key_first_last

Including a polyfill would allow developers to take advantage of the new functions and write consistent code regardless of a site's PHP version. While polyfills for these two functions are not required for WordPress to be PHP 7.3 compatible, it could be added in the version after 7.3 compatibility is declared.

Attachments (3)

45055.diff (1.3 KB) - added by manzoorwani.jk 6 years ago.
45055-2-optimized.patch (5.0 KB) - added by ayeshrajans 6 years ago.
45055.3.diff (1.3 KB) - added by audrasjb 3 years ago.
Refreshed patch

Download all attachments as: .zip

Change History (24)

#2 @desrosj
6 years ago

  • Keywords php73 added

@manzoorwani.jk
6 years ago

#3 @manzoorwani.jk
6 years ago

For array_key_first, foreach is faster than reset while as for array_key_last, end is faster than other approaches like array_slice or array_keys with end.

#4 @ayeshrajans
6 years ago

  • Keywords has-patch added; needs-patch removed
  • Version set to trunk

I also think it's a good idea to add the polyfills, but also to search existing current(), reset(), and key() calls to make use of this function. I know PHP 7.3 users will appreciate it too.

Patch from @manzoorwanijk is a good start, but now that PHP 7.3 is released, we can make it more closure to the native function. It looks like this patch was taken from the first example in this function page. Symfony/polyfills has a better one that mimics the original functionality with parameter types. I will attach a patch with more optimized polyfills. To make it complete, this patch also contains the original tests from PHP source, so we can make sure it really gives same results as PHP's own tests.

#6 @desrosj
3 years ago

  • Milestone changed from Future Release to 5.9
  • Owner set to desrosj
  • Status changed from new to assigned

@audrasjb
3 years ago

Refreshed patch

#7 @audrasjb
3 years ago

Damn I forgot the phpunit tests

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 years ago

#9 @audrasjb
3 years ago

  • Keywords needs-refresh needs-unit-tests added

This ticket was mentioned in Slack in #core-php by hellofromtonya. View the logs.


3 years ago

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 years ago

#12 @hellofromTonya
3 years ago

  • Owner changed from desrosj to hellofromTonya
  • Status changed from assigned to accepted

Reassigning this ticket and other PHP polyfill tickets to me to get them into 5.9 release.

This ticket was mentioned in PR #1809 on WordPress/wordpress-develop by pbearne.


3 years ago
#13

  • Keywords has-unit-tests added; needs-refresh needs-unit-tests removed

This ticket was mentioned in Slack in #core-php by pbearne. View the logs.


3 years ago

hellofromtonya commented on PR #1809:


3 years ago
#15

Well darn. Working in the GH UI to edit the file is not capturing the tabs. Weird. Can be fixed during commit.

hellofromtonya commented on PR #1809:


3 years ago
#16

Well darn. Working in the GH UI to edit the file is not capturing the tabs. Weird. Can be fixed during commit.

#17 @hellofromTonya
3 years ago

  • Keywords commit added

PR 1809 is thoroughly reviewed and ready for commit.

#18 @hellofromTonya
3 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 52038:

General: Introduce polyfills for array_key_first() and array_key_last() added in PHP 7.3.0.

PHP 7.3.0 introduced two new functions: array_key_first() and array_key_last(). These two functions return the first and last key of each array respectively, without affecting the internal state of the array.

The polyfills make these two functions available for use in Core on PHP versions less than 7.3.0.

Ref:

Props desrosj, pbearne, costdev, hellofromTonya, ayeshrajans, manzoorwanijk, audrasjb, sergeybiryukov.
Fixes #45055.

#20 @hellofromTonya
3 years ago

Thank you everyone for your contributions! The polyfills and their tests are now committed and will ship in 5.9.

This ticket was mentioned in Slack in #core by sergey. View the logs.


3 years ago

Note: See TracTickets for help on using tickets.