Skip to content

Commit cf0ad1e

Browse files
committed
Code Quality: Add the missing types around _get_list_table().
- The return type promised `WP_List_Table|false` whatever class was asked for. A `@phpstan-template` now ties it to the `class-string` passed in. - The `$args` param was described as accepting only `screen`. It now carries the hash notation from `WP_List_Table::__construct()`. - That `screen` argument was typed as a string. It is documented as the `string|WP_Screen|null` it has always accepted. This type union is also applied to `convert_to_screen()` and `WP_Screen::get()`. Nothing changes at runtime. The function is left free of PHPStan errors at every rule level. Developed in WordPress#13553. Follow-up to r29459, r54378, r63020, r63420. Props soean, westonruter. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@63648 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8ff90e3 commit cf0ad1e

8 files changed

Lines changed: 45 additions & 96 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ includes:
5454
- tests/phpstan/baselines/isset.offset.neon
5555
- tests/phpstan/baselines/isset.property.neon
5656
- tests/phpstan/baselines/method.childParameterType.neon
57-
- tests/phpstan/baselines/method.nonObject.neon
5857
- tests/phpstan/baselines/method.unused.neon
5958
- tests/phpstan/baselines/notIdentical.alwaysTrue.neon
6059
- tests/phpstan/baselines/nullCoalesce.offset.neon

‎src/wp-admin/includes/class-wp-list-table.php‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ class WP_List_Table {
133133
* @param array|string $args {
134134
* Array or string of arguments.
135135
*
136-
* @type string $plural Plural value used for labels and the objects being listed.
137-
* This affects things such as CSS class-names and nonces used
138-
* in the list table, e.g. 'posts'. Default empty.
139-
* @type string $singular Singular label for an object being listed, e.g. 'post'.
140-
* Default empty
141-
* @type bool $ajax Whether the list table supports Ajax. This includes loading
142-
* and sorting data, for example. If true, the class will call
143-
* the _js_vars() method in the footer to provide variables
144-
* to any scripts handling Ajax events. Default false.
145-
* @type string $screen String containing the hook name used to determine the current
146-
* screen. If left null, the current screen will be automatically set.
147-
* Default null.
136+
* @type string $plural Plural value used for labels and the objects being listed.
137+
* This affects things such as CSS class-names and nonces used
138+
* in the list table, e.g. 'posts'. Default empty.
139+
* @type string $singular Singular label for an object being listed, e.g. 'post'.
140+
* Default empty.
141+
* @type bool $ajax Whether the list table supports Ajax. This includes loading
142+
* and sorting data, for example. If true, the class will call
143+
* the _js_vars() method in the footer to provide variables
144+
* to any scripts handling Ajax events. Default false.
145+
* @type string|WP_Screen|null $screen String containing the hook name used to determine the current
146+
* screen, or a `WP_Screen` instance. If left null, the current
147+
* screen will be automatically set. Default null.
148148
* }
149149
*/
150150
public function __construct( $args = array() ) {

‎src/wp-admin/includes/class-wp-screen.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ final class WP_Screen {
205205
*
206206
* @global string $hook_suffix
207207
*
208-
* @param string|WP_Screen $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
209-
* Defaults to the current $hook_suffix global.
208+
* @param string|WP_Screen|null $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
209+
* Defaults to the current $hook_suffix global.
210210
* @return WP_Screen Screen object.
211211
*/
212212
public static function get( $hook_name = '' ) {

‎src/wp-admin/includes/list-table.php‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,28 @@
1515
* @global string $hook_suffix
1616
*
1717
* @param string $class_name The type of the list table, which is the class name.
18-
* @param array $args Optional. Arguments to pass to the class. Accepts 'screen'.
19-
* @return WP_List_Table|false List table object on success, false if the class does not exist.
18+
* @param array $args {
19+
* Optional. Arguments to pass to the class.
20+
*
21+
* @type string $plural Plural value used for labels and the objects being listed.
22+
* This affects things such as CSS class-names and nonces used
23+
* in the list table, e.g. 'posts'. Default empty.
24+
* @type string $singular Singular label for an object being listed, e.g. 'post'.
25+
* Default empty.
26+
* @type bool $ajax Whether the list table supports Ajax. This includes loading
27+
* and sorting data, for example. If true, the class will call
28+
* the _js_vars() method in the footer to provide variables
29+
* to any scripts handling Ajax events. Default false.
30+
* @type string|WP_Screen|null $screen String containing the hook name used to determine the current
31+
* screen, or a `WP_Screen` instance. If left null, the current
32+
* screen will be automatically set. Default null.
33+
* }
34+
* @return WP_List_Table|false List table object of the type given in `$class_name`
35+
* on success, false if the class does not exist.
36+
*
37+
* @phpstan-template T of WP_List_Table
38+
* @phpstan-param class-string<T> $class_name
39+
* @phpstan-return T|false
2040
*/
2141
function _get_list_table( $class_name, $args = array() ) {
2242
$core_classes = array(
@@ -64,6 +84,9 @@ function _get_list_table( $class_name, $args = array() ) {
6484
*
6585
* @param string $class_name The list table class to use.
6686
* @param array $args An array containing _get_list_table() arguments.
87+
*
88+
* @phpstan-template T of WP_List_Table
89+
* @phpstan-param class-string<T> $class_name
6790
*/
6891
$custom_class_name = apply_filters( 'wp_list_table_class_name', $class_name, $args );
6992

‎src/wp-admin/includes/template.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,9 @@ function _wp_admin_html_begin() {
27252725
*
27262726
* @since 3.0.0
27272727
*
2728-
* @param string $hook_name The hook name (also known as the hook suffix) used to determine the screen.
2728+
* @param string|WP_Screen|null $hook_name The hook name (also known as the hook suffix) used to determine the screen.
2729+
* A `WP_Screen` instance is returned as-is. If null, the current
2730+
* $hook_suffix global is used.
27292731
* @return WP_Screen Screen object.
27302732
*/
27312733
function convert_to_screen( $hook_name ) {

‎tests/phpstan/baselines/arguments.count.neon‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@
1818

1919
parameters:
2020
ignoreErrors:
21-
-
22-
message: '#^Method WP_List_Table\:\:display_rows\(\) invoked with 2 parameters, 0 required\.$#'
23-
identifier: arguments.count
24-
count: 1
25-
path: ../../../src/wp-admin/includes/ajax-actions.php
26-
-
27-
message: '#^Method WP_List_Table\:\:single_row\(\) invoked with 2 parameters, 1 required\.$#'
28-
identifier: arguments.count
29-
count: 2
30-
path: ../../../src/wp-admin/includes/ajax-actions.php
31-
-
32-
message: '#^Method WP_List_Table\:\:single_row\(\) invoked with 3 parameters, 1 required\.$#'
33-
identifier: arguments.count
34-
count: 1
35-
path: ../../../src/wp-admin/includes/ajax-actions.php
3621
-
3722
message: '#^Method WP_Upgrader_Skin\:\:before\(\) invoked with 1 parameter, 0 required\.$#'
3823
identifier: arguments.count
@@ -43,8 +28,3 @@ parameters:
4328
identifier: arguments.count
4429
count: 2
4530
path: ../../../src/wp-admin/includes/class-theme-upgrader.php
46-
-
47-
message: '#^Method WP_List_Table\:\:display\(\) invoked with 1 parameter, 0 required\.$#'
48-
identifier: arguments.count
49-
count: 1
50-
path: ../../../src/wp-admin/includes/meta-boxes.php

‎tests/phpstan/baselines/method.nonObject.neon‎

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎tests/phpstan/baselines/property.protected.neon‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
parameters:
2020
ignoreErrors:
2121
-
22-
message: '#^Access to protected property WP_List_Table\:\:\$screen\.$#'
22+
message: '#^Access to protected property WP_Privacy_Data_Removal_Requests_List_Table\:\:\$screen\.$#'
2323
identifier: property.protected
2424
count: 1
2525
path: ../../../src/wp-admin/erase-personal-data.php
2626
-
27-
message: '#^Access to protected property WP_List_Table\:\:\$screen\.$#'
27+
message: '#^Access to protected property WP_Privacy_Data_Export_Requests_List_Table\:\:\$screen\.$#'
2828
identifier: property.protected
2929
count: 1
3030
path: ../../../src/wp-admin/export-personal-data.php
3131
-
32-
message: '#^Access to protected property WP_List_Table\:\:\$screen\.$#'
32+
message: '#^Access to protected property WP_Posts_List_Table\:\:\$screen\.$#'
3333
identifier: property.protected
3434
count: 1
3535
path: ../../../src/wp-admin/includes/ajax-actions.php

0 commit comments

Comments
 (0)