Make WordPress Core

Opened 18 months ago

Closed 17 months ago

Last modified 16 months ago

#57358 closed enhancement (fixed)

PHP 8 deprecation in wp-includes/category-template.php uasort() helper

Reported by: webrocker's profile Webrocker Owned by: audrasjb's profile audrasjb
Milestone: 6.2 Priority: normal
Severity: normal Version: 6.1.1
Component: Taxonomy Keywords: php8 has-patch commit
Focuses: Cc:

Description

category-template.php, line 1089 ff:

function _wp_object_count_sort_cb( $a, $b ) {
        return ( $a->count > $b->count );
}

This throws a Deprecation warning with PHP 8: "Deprecated: uasort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in wp-includes/category-template.php

changing the above code to

function _wp_object_count_sort_cb( $a, $b ) {
        return ( $a->count - $b->count );
}

fixes the warning for me, while still sorting as before.

Attachments (1)

57358.patch (509 bytes) - added by jigar bhanushali 18 months ago.

Download all attachments as: .zip

Change History (21)

#1 @SergeyBiryukov
18 months ago

  • Component changed from General to Taxonomy
  • Keywords needs-patch php8 added
  • Milestone changed from Awaiting Review to 6.2

Hi there, welcome to WordPress Trac! Thanks for the ticket, moving to 6.2 for review.

#2 @jigar bhanushali
18 months ago

  • Keywords has-patch added; needs-patch removed

#3 @jrf
18 months ago

  • Keywords needs-patch added; has-patch removed

@jigar-bhanushali Thanks for working on this. Unfortunately, your patch cannot be accepted as you are using PHP 7.0+ syntax, while WordPress still supports PHP 5.6.

This ticket was mentioned in PR #3804 on WordPress/wordpress-develop by iamwebrocker.


18 months ago
#4

  • Keywords has-patch added; needs-patch removed

Change return value from boolean to int less, equal or greater than zero as requited by php 8. Using substraction instead of the spaceship operator (<=>) for php 5.6 compat

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

@mukesh27 commented on PR #3804:


18 months ago
#5

@iamwebrocker Can you please update the PR title to Fix PHP 8 deprecation warning in category-template.php?

#6 @mukesh27
18 months ago

  • Keywords changes-requested added

iamwebrocker commented on PR #3804:


18 months ago
#7

@mukeshpanchal27 thanks for the catch :)

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


17 months ago

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


17 months ago

@audrasjb commented on PR #3804:


17 months ago
#10

Added a quick commit to address a tiny typo in the docblock ;)

#11 @costdev
17 months ago

  • Keywords commit added; changes-requested removed

This ticket was discussed during the bug scrub. Feedback on the PR has been addressed and this is ready for commit consideration. Adding the appropriate keyword.

Additional props: @audrasjb

@jrf commented on PR #3804:


17 months ago
#12

Just wondering if there are tests for this function and if not, if now would be a good time to add those ?

@audrasjb commented on PR #3804:


17 months ago
#13

I'd suggest to secure this bugfix for 6.2 and to open a new ticket for complete test coverage, since test fixes can be committed anytime during the beta cycle. What do you think @costdev ?

@costdev commented on PR #3804:


17 months ago
#14

@audrasjb Yes that sounds good to me!

@jrfnl Do you have any thoughts on datasets, beyond sorting 4, 3, 2, 1 ->1, 2, 3, 4 and 3, 1, 4, 2 -> 1, 2, 3, 4?

#15 @audrasjb
17 months ago

  • Owner set to audrasjb
  • Resolution set to fixed
  • Status changed from new to closed

In 55214:

Fix a PHP 8 deprecation warning in category-template.php uasort() helper.

Props Webrocker, jigar-bhanushali, jrf, audrasjb, costdev.
Fixes #57358.
See #56790.

@jrf commented on PR #3804:


17 months ago
#17

@jrfnl Do you have any thoughts on datasets, beyond sorting 4, 3, 2, 1 -> 1, 2, 3, 4 and 3, 1, 4, 2 -> 1, 2, 3, 4?

Well, what about 2, 1, 2, 1 ? Or -2, -1, -3 ,-2 ?

Also: the function contains a lot of assumptions....

  • The function doesn't have any type declarations, but does include a presumption that an object will be passed, though it doesn't specify which type of object _(IIRC I know why, and that's why an interface should be added)_...
  • ... and that the passed object will have a count property.
  • ... and that this count property is publicly accessible.
  • ... and that this count property will actually be set.
  • ... and that the data type stored in the count property will always be numeric or at least a scalar (juggleable to int).

Want me to go on ? I can think of plenty of other ways to break this function.... 😈

@costdev Does this give you enough inspiration ?

#18 @SergeyBiryukov
17 months ago

In 55222:

Docs: Use consistent format for the @return tags in _wp_object_name_sort_cb() and _wp_object_count_sort_cb().

Both functions are used as a callback for uasort().

Follow-up to [36013], [55214].

See #57358, #56792.

#19 @SergeyBiryukov
16 months ago

#57727 was marked as a duplicate.

#20 @costdev
16 months ago

#56838 was marked as a duplicate.

Note: See TracTickets for help on using tickets.