Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#44124 closed defect (bug) (wontfix)

Adding is_countable() check to custom-header.php

Reported by: ianbelanger's profile ianbelanger Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

Updating a single instance of count() in show_header_selector()

if ( count( $var ) > 0 ) {
        // Do something.
}

with is_countable()

if ( is_countable( $var ) && count( $var ) > 0 ) {
        // Do something.
}

in the custom-header.php file, in order to avoid warnings in PHP 7.2

Attachments (2)

44124.diff (635 bytes) - added by ianbelanger 7 years ago.
Updated custom-header.php with new is_countable() function
44124.1.diff (2.2 KB) - added by ianbelanger 7 years ago.
Updated diff to move foreach inside is_countable() condition

Download all attachments as: .zip

Change History (11)

@ianbelanger
7 years ago

Updated custom-header.php with new is_countable() function

#1 @ianbelanger
7 years ago

  • Keywords has-patch needs-testing added

#2 @ianbelanger
7 years ago

  • Focuses coding-standards added
  • Version set to trunk

#3 @jrf
7 years ago

  • Focuses coding-standards removed

LGTM

#4 @jrf
7 years ago

You may also want to add a is_iterable() wrapper around the foreach() just below within the same function or move the whole block of code below within the if ( is_countable() & count() > 1 ) {} wrapper.

https://core.trac.wordpress.org/browser/trunk/src/wp-admin/custom-header.php#L295

Last edited 7 years ago by jrf (previous) (diff)

#5 @ianbelanger
7 years ago

@jrf I was going to work on the is_countable() instances first, but I think it does make sense to do them both in the same diff. I will update the diff and re-upload. Thanks for the input!

@ianbelanger
7 years ago

Updated diff to move foreach inside is_countable() condition

#6 @netweb
7 years ago

  • Milestone changed from Awaiting Review to 4.9.7

Related: #44123 - Utilize is_countable() and is_iterable() Polyfills

#7 @johnbillion
7 years ago

  • Keywords close added
  • Version trunk deleted

Thanks for your patch @ianbelanger.

I don't believe this is appropriate use of is_countable(). If a bug is introduced elsewhere which results in $headers being set to a non-countable value (for example via the return value of get_uploaded_header_images()), it's not a good idea to mask it via logic such as this. This is the reason that PHP now triggers warnings when attempting to count un-countable values since PHP 7.2.

See 44123#comment:11.

#8 @desrosj
7 years ago

  • Milestone changed from 4.9.7 to 4.9.8

Moving all tickets in 4.9.7 to 4.9.8.

#9 @desrosj
7 years ago

  • Keywords has-patch needs-testing close removed
  • Milestone 4.9.8 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Looked this over. Agree that this instance of count() should not be updated.

Breaking it down, $headers could be of a type that is not countable, but only if the $_wp_default_headers global is overridden. In this scenario, an error should be thrown to indicate improper usage of $_wp_default_headers.

Note: See TracTickets for help on using tickets.