Make WordPress Core

Opened 21 months ago

Last modified 13 days ago

#62762 reviewing enhancement

WP_Widget_Categories::widget(): use int or bool for $cat_args values instead of numeric strings

Reported by: marian1 Owned by: westonruter
Priority: normal Milestone: 7.2
Component: Widgets Version:
Severity: trivial Keywords: has-patch
Cc: Focuses:

Description

Both $cat_args['show_count'] and $cat_args['hierarchical'] are currently set to '0' or '1' (numeric strings). See lines 54 to 68 in class-wp-widget-categories.php. These values are then passed to wp_dropdown_categories() or wp_list_categories(), both of which are documented to expect int|bool for $cat_args['show_count'] and $cat_args['hierarchical']. See the function references for wp_list_categories and wp_dropdown_categories.

As using integers or booleans instead of numeric strings does not alter the behaviour of WP_Widget_Categories::widget(), specifying $cat_args['show_count'] and $cat_args['hierarchical'] either as booleans or integers would increase type consistency.

Also, $dropdown (line 56) is set to '0' or '1' and could be set to true or false instead.

Change History (3)

This ticket was mentioned in PR #8071 on WordPress/wordpress-develop by @marian1.


21 months ago
#1

  • Keywords has-patch added

#2 @westonruter
2 weeks ago

  • Milestone Awaiting Review7.2
  • Owner set to westonruter
  • Status newreviewing

@westonruter commented on PR #8071:


13 days ago
#3

If it weren't for the widget_categories_dropdown_args filter which widened the type of $cat_args to array, the call below to wp_dropdown_categories() would emitting a PHPStan error:

Parameter #1 $args of function wp_dropdown_categories expects array{show_option_all?: string, show_option_none?: string, option_none_value?: string, orderby?: string, pad_counts?: bool, show_count?: bool|int, echo?: bool|int, hierarchical?: bool|int, ..., ...}|string, array{orderby: 'name', show_count: '0'|'1', hierarchical: '0'|'1', show_option_none: string, id: non-falsy-string} given.

With the changes here, $cat_args ends up typed as:

array{
    orderby: 'name', 
    show_count: bool, 
    hierarchical: bool, 
    show_option_none: string, 
    id: non-falsy-string
}

And no PHPStan error is raised.

Separately from the changes here, I want to improve the ApplyFiltersDynamicFunctionReturnTypeExtension for PHPStan so that if the @param of a filter is just a bare array that whatever the type of the value being passed into the filter then is used as the type instead. Alternatively, all the filter docs should be updated to specify the full array shape being passed in, which is clearly better long term. I've done that for the widget_categories_dropdown_args-filtered value here via d399c0b. Furthermore, the extension should flag an error when a value is passed in to such a filter but its analyzed type conflicts the @param.

Note: See TracTickets for help on using tickets.