[wp-trac] [WordPress Trac] #44123: Utilize `is_countable()` and `is_iterable()` Polyfills
WordPress Trac
noreply at wordpress.org
Thu May 17 14:22:37 UTC 2018
#44123: Utilize `is_countable()` and `is_iterable()` Polyfills
----------------------------+---------------------
Reporter: desrosj | Owner: (none)
Type: task (blessed) | Status: new
Priority: normal | Milestone: 4.9.7
Component: General | Version:
Severity: normal | Resolution:
Keywords: needs-patch | Focuses:
----------------------------+---------------------
Changes (by desrosj):
* focuses: coding-standards =>
Old description:
> In 4.9.6, polyfills for `is_countable()` and `is_iterable()` were
> introduced (#43583 and #43619). These should be utilized in core for
> better variable validation and forwards compatibility.
>
> This is a meta ticket to track overall progress making replacements
> throughout core. Because there will be many instances and each requires
> validation, individual tickets should be opened for each replacement.
New description:
In 4.9.6, polyfills for `is_countable()` and `is_iterable()` were
introduced (#43583 and #43619). These should be utilized in core for
better variable validation and forwards compatibility.
This is a meta ticket to track overall progress making replacements
throughout core. Because there will be many instances and each requires
validation, individual tickets should be opened for each patch.
**If you have reviewed any core files, please add a note in this ticket
about which files you have reviewed and reference any tickets with patches
that you have opened as a result of your review.**
Dev note detailing the new polyfills:
https://make.wordpress.org/core/2018/05/17/new-php-polyfills-in-4-9-6/
== Using `is_countable()`
=== Old Way
{{{
if ( count( $var ) > 0 ) {
// Do something.
}
}}}
=== New Way
{{{
if ( is_countable( $var ) && count( $var ) > 0 ) {
// Do something.
}
}}}
== Using `is_iterable()`
=== Old Way
{{{
if ( count( $var ) > 0 ) {
foreach( $var as $key => $value) {
// Do something.
}
}
}}}
=== New Way
{{{
if ( is_iterable( $var ) ) {
foreach( $var as $key => $value) {
// Do something.
}
}
}}}
--
Comment:
Description edits
- Added link to the 4.9.6 dev note.
- Added examples from the dev note.
- Added clarification of the ticket's purpose.
- Remove `coding-standards` focus (this is more "best practices").
--
Ticket URL: <https://core.trac.wordpress.org/ticket/44123#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list