#62558 closed feature request (fixed)
Add PHP 8.4 compat array functions: `array_find`, `array_find_key`, `array_any` and `array_all`
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.8 | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch php84 add-to-field-guide commit |
Focuses: | php-compatibility | Cc: |
Description
Add polyfill functions for PHP 8.4 array utilities
- Added
array_find
function to search an array for the first element that passes a given callback. - Added
array_find_key
function to search an array for the first key that passes a given callback. - Added
array_any
function to check if any element of an array passes a given callback. - Added
array_all
function to check if all elements of an array pass a given callback.
Change History (9)
This ticket was mentioned in PR #7879 on WordPress/wordpress-develop by @Soean.
6 months ago
#1
- Keywords has-patch added
#3
@
6 months ago
- Keywords php84 added
- Type changed from defect (bug) to feature request
Related: #62061, #62277
Previously added polyfills: [57337], [52038]
One Inline Documentation request, if I may: There should not be a blank line before the @return
, see https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#1-functions-class-methods
#5
follow-up:
↓ 6
@
6 months ago
Do we have a guideline on which functions we polyfill, and which we don't?
PHP 8.4 has more new functions, and and the likelihood of functions like mb_*trim
functions being useful enough to polyfill is as good as the new array_*
functions.
I'm not objecting this ticket in particular, just wanted to put my two cents forward that we probably should follow a guideline (of which I don't know if exists) so we can maintain the polyfills properly.
#6
in reply to:
↑ 5
@
3 months ago
- Keywords changes-requested removed
Replying to ayeshrajans:
Do we have a guideline on which functions we polyfill, and which we don't?
We don't currently have any official guidance in the handbook. Perhaps that's a good thing to add. If I had to write up that section today, this is what I would list as the criteria used to weigh when evaluating whether to include a PHP language polyfill:
- Polyfill code size
- How self contained is the polyfill? It should serve a single-responsibility unless absolutely necessary (we want to avoid adding entire polyfill classes and libraries as much as possible).
- How many old code patterns does the polyfill replace in favor of consistency?
- Performance improvement compared to these older patterns. Large improvements lend to the polyfill being a strong progressive enhancement that plugin and theme developers can count on being available.
- Does the new function protect against common mistakes? For example,
str_contains()
guarding against scenarios whereif ( strpos( 'This is a haystack', 'This' ) )
is evaluated as falsey. - Are these old patterns prevalent throughout Core? Would the polyfill help remove a good amount of duplicate code?
- Maintenance burden of adding a polyfill. It should be simple to remove later.
- The range of PHP versions currently supported. For example, if something is added in 8.4, a polyfill would currently allow for supporting the new function in 7 additional PHP versions.
In the case of these functions, it's difficult to find occurrence in Core where this would be useful, but I think it's a reasonable assumption there are a good number of instances where this would be useful. But it will require a more manual effort.
The PR looks good to me, I just approved it. I would love to give the opportunity for @joemcgill and @Mamaduka to weigh in as my fellow Core Tech Leads for 6.8 in case they have any opposing opinions.
array_find
function to search an array for the first element that passes a given callback.array_find_key
function to search an array for the first key that passes a given callback.array_any
function to check if any element of an array passes a given callback.array_all
function to check if all elements of an array pass a given callback.Trac ticket: https://core.trac.wordpress.org/ticket/62558