From 4e2250fef4d4b5339fca60a52317262b0b1a742d Mon Sep 17 00:00:00 2001 From: Mat Lipe Date: Fri, 7 Jun 2024 17:39:03 +0000 Subject: [PATCH 1/3] feat: Add support for dynamic return type in `WP_List_Table` methods The code changes introduce support for dynamic return types in several methods of the `WP_List_Table` class. This allows for more flexibility in the return values based on the generic type `T` defined in the class. --- functionMap.php | 11 +++++++++++ wordpress-stubs.php | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/functionMap.php b/functionMap.php index 93c6e96..25431f8 100644 --- a/functionMap.php +++ b/functionMap.php @@ -41,6 +41,17 @@ 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], 'WP_Http::request' => [$httpReturnType], + 'WP_List_Table' => [null, '@phpstan-template' => 'T of array|object'], + 'WP_List_Table::get_columns' =>[ 'array, string>' ], + 'WP_List_Table::get_sortable_columns' =>[ 'array, string>' ], + 'WP_List_Table::get_default_primary_column_name' =>[ 'key-of' ], + 'WP_List_Table::get_primary_column' =>[ 'key-of' ], + 'WP_List_Table::get_primary_column_name' =>[ 'key-of' ], + 'WP_List_Table::single_row' =>[ null, 'item' => 'T' ], + 'WP_List_Table::column_default' =>[ null, 'item' => 'T', 'column_name' => 'key-of' ], + 'WP_List_Table::column_cb' =>[ null, 'item' => 'T' ], + 'WP_List_Table::single_row_columns' =>[ null, 'item' => 'T' ], + 'WP_List_Table::handle_row_actions' =>[ null, 'item' => 'T', 'column_name' => 'key-of', 'primary' => 'key-of' ], 'WP_List_Table::set_pagination_args' => ['void', 'args' => 'array{total_items?: int, total_pages?: int, per_page?: int}'], 'wp_next_scheduled' => [null, 'args' => $cronArgsType], 'WP_Query::have_posts' => [null, '@phpstan-impure' => ''], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 95f52b6..71d1151 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -4162,6 +4162,7 @@ public function feedback($feedback, ...$args) * Base class for displaying a list of items in an ajaxified HTML table. * * @since 3.1.0 + * @phpstan-template T of array|object */ #[\AllowDynamicProperties] class WP_List_Table @@ -4577,6 +4578,7 @@ protected function pagination($which) * @abstract * * @return array + * @phpstan-return array, string> */ public function get_columns() { @@ -4599,6 +4601,7 @@ public function get_columns() * @since 6.3.0 Added 'abbr', 'orderby-text' and 'initially-sorted-column-order'. * * @return array + * @phpstan-return array, string> */ protected function get_sortable_columns() { @@ -4609,6 +4612,7 @@ protected function get_sortable_columns() * @since 4.3.0 * * @return string Name of the default primary column, in this case, an empty string. + * @phpstan-return key-of */ protected function get_default_primary_column_name() { @@ -4621,6 +4625,7 @@ protected function get_default_primary_column_name() * @since 4.4.0 * * @return string Name of the default primary column. + * @phpstan-return key-of */ public function get_primary_column() { @@ -4631,6 +4636,7 @@ public function get_primary_column() * @since 4.3.0 * * @return string The name of the primary column. + * @phpstan-return key-of */ protected function get_primary_column_name() { @@ -4738,6 +4744,7 @@ public function display_rows() * @since 3.1.0 * * @param object|array $item The current item + * @phpstan-param T $item */ public function single_row($item) { @@ -4745,12 +4752,15 @@ public function single_row($item) /** * @param object|array $item * @param string $column_name + * @phpstan-param T $item + * @phpstan-param key-of $column_name */ protected function column_default($item, $column_name) { } /** * @param object|array $item + * @phpstan-param T $item */ protected function column_cb($item) { @@ -4761,6 +4771,7 @@ protected function column_cb($item) * @since 3.1.0 * * @param object|array $item The current item. + * @phpstan-param T $item */ protected function single_row_columns($item) { @@ -4775,6 +4786,9 @@ protected function single_row_columns($item) * @param string $primary Primary column name. * @return string The row actions HTML, or an empty string * if the current column is not the primary column. + * @phpstan-param T $item + * @phpstan-param key-of $column_name + * @phpstan-param key-of $primary */ protected function handle_row_actions($item, $column_name, $primary) { From ad4abb0444838fd26b2088f35b9bb278354381a5 Mon Sep 17 00:00:00 2001 From: Mat Lipe Date: Sun, 9 Jun 2024 15:49:48 +0000 Subject: [PATCH 2/3] Split the column name of WP_List_Table into its own @template The column names could be anything as they are provided via `get_columns`. We also must support `object` as T which does not work with `key-of`. https://phpstan.org/r/a4104203-bd28-459c-aff5-27715708acbe --- functionMap.php | 22 +++++++++++----------- wordpress-stubs.php | 15 ++++++++------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/functionMap.php b/functionMap.php index 25431f8..e7dfd89 100644 --- a/functionMap.php +++ b/functionMap.php @@ -41,17 +41,17 @@ 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], 'WP_Http::request' => [$httpReturnType], - 'WP_List_Table' => [null, '@phpstan-template' => 'T of array|object'], - 'WP_List_Table::get_columns' =>[ 'array, string>' ], - 'WP_List_Table::get_sortable_columns' =>[ 'array, string>' ], - 'WP_List_Table::get_default_primary_column_name' =>[ 'key-of' ], - 'WP_List_Table::get_primary_column' =>[ 'key-of' ], - 'WP_List_Table::get_primary_column_name' =>[ 'key-of' ], - 'WP_List_Table::single_row' =>[ null, 'item' => 'T' ], - 'WP_List_Table::column_default' =>[ null, 'item' => 'T', 'column_name' => 'key-of' ], - 'WP_List_Table::column_cb' =>[ null, 'item' => 'T' ], - 'WP_List_Table::single_row_columns' =>[ null, 'item' => 'T' ], - 'WP_List_Table::handle_row_actions' =>[ null, 'item' => 'T', 'column_name' => 'key-of', 'primary' => 'key-of' ], + 'WP_List_Table' => [null, '@phpstan-template' => 'T of array|object', '@template' => 'K of string'], + 'WP_List_Table::get_columns' =>['array'], + 'WP_List_Table::get_sortable_columns' =>['array'], + 'WP_List_Table::get_default_primary_column_name' =>['K'], + 'WP_List_Table::get_primary_column' =>[ 'K'], + 'WP_List_Table::get_primary_column_name' =>[ 'K'], + 'WP_List_Table::single_row' =>[null, 'item' => 'T'], + 'WP_List_Table::column_default' =>[null, 'item' => 'T', 'column_name' => 'K'], + 'WP_List_Table::column_cb' =>[null, 'item' => 'T'], + 'WP_List_Table::single_row_columns' =>[null, 'item' => 'T'], + 'WP_List_Table::handle_row_actions' =>[null, 'item' => 'T', 'column_name' => 'K', 'primary' => 'key-of'], 'WP_List_Table::set_pagination_args' => ['void', 'args' => 'array{total_items?: int, total_pages?: int, per_page?: int}'], 'wp_next_scheduled' => [null, 'args' => $cronArgsType], 'WP_Query::have_posts' => [null, '@phpstan-impure' => ''], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 71d1151..2ad44c7 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -4163,6 +4163,7 @@ public function feedback($feedback, ...$args) * * @since 3.1.0 * @phpstan-template T of array|object + * @template K of string */ #[\AllowDynamicProperties] class WP_List_Table @@ -4578,7 +4579,7 @@ protected function pagination($which) * @abstract * * @return array - * @phpstan-return array, string> + * @phpstan-return array */ public function get_columns() { @@ -4601,7 +4602,7 @@ public function get_columns() * @since 6.3.0 Added 'abbr', 'orderby-text' and 'initially-sorted-column-order'. * * @return array - * @phpstan-return array, string> + * @phpstan-return array */ protected function get_sortable_columns() { @@ -4612,7 +4613,7 @@ protected function get_sortable_columns() * @since 4.3.0 * * @return string Name of the default primary column, in this case, an empty string. - * @phpstan-return key-of + * @phpstan-return K */ protected function get_default_primary_column_name() { @@ -4625,7 +4626,7 @@ protected function get_default_primary_column_name() * @since 4.4.0 * * @return string Name of the default primary column. - * @phpstan-return key-of + * @phpstan-return K */ public function get_primary_column() { @@ -4636,7 +4637,7 @@ public function get_primary_column() * @since 4.3.0 * * @return string The name of the primary column. - * @phpstan-return key-of + * @phpstan-return K */ protected function get_primary_column_name() { @@ -4753,7 +4754,7 @@ public function single_row($item) * @param object|array $item * @param string $column_name * @phpstan-param T $item - * @phpstan-param key-of $column_name + * @phpstan-param K $column_name */ protected function column_default($item, $column_name) { @@ -4787,7 +4788,7 @@ protected function single_row_columns($item) * @return string The row actions HTML, or an empty string * if the current column is not the primary column. * @phpstan-param T $item - * @phpstan-param key-of $column_name + * @phpstan-param K $column_name * @phpstan-param key-of $primary */ protected function handle_row_actions($item, $column_name, $primary) From 3e0f085b9cfb3d6badd6e613b7ec7b50fbc6ecc5 Mon Sep 17 00:00:00 2001 From: Mat Lipe Date: Sun, 9 Jun 2024 15:53:20 +0000 Subject: [PATCH 3/3] Fix spacing in functionMap WP_List_Table --- functionMap.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/functionMap.php b/functionMap.php index e7dfd89..a94d29c 100644 --- a/functionMap.php +++ b/functionMap.php @@ -42,16 +42,16 @@ 'WP_Http::post' => [$httpReturnType], 'WP_Http::request' => [$httpReturnType], 'WP_List_Table' => [null, '@phpstan-template' => 'T of array|object', '@template' => 'K of string'], - 'WP_List_Table::get_columns' =>['array'], - 'WP_List_Table::get_sortable_columns' =>['array'], - 'WP_List_Table::get_default_primary_column_name' =>['K'], - 'WP_List_Table::get_primary_column' =>[ 'K'], - 'WP_List_Table::get_primary_column_name' =>[ 'K'], - 'WP_List_Table::single_row' =>[null, 'item' => 'T'], - 'WP_List_Table::column_default' =>[null, 'item' => 'T', 'column_name' => 'K'], - 'WP_List_Table::column_cb' =>[null, 'item' => 'T'], - 'WP_List_Table::single_row_columns' =>[null, 'item' => 'T'], - 'WP_List_Table::handle_row_actions' =>[null, 'item' => 'T', 'column_name' => 'K', 'primary' => 'key-of'], + 'WP_List_Table::get_columns' => ['array'], + 'WP_List_Table::get_sortable_columns' => ['array'], + 'WP_List_Table::get_default_primary_column_name' => ['K'], + 'WP_List_Table::get_primary_column' => [ 'K'], + 'WP_List_Table::get_primary_column_name' => [ 'K'], + 'WP_List_Table::single_row' => [null, 'item' => 'T'], + 'WP_List_Table::column_default' => [null, 'item' => 'T', 'column_name' => 'K'], + 'WP_List_Table::column_cb' => [null, 'item' => 'T'], + 'WP_List_Table::single_row_columns' => [null, 'item' => 'T'], + 'WP_List_Table::handle_row_actions' => [null, 'item' => 'T', 'column_name' => 'K', 'primary' => 'key-of'], 'WP_List_Table::set_pagination_args' => ['void', 'args' => 'array{total_items?: int, total_pages?: int, per_page?: int}'], 'wp_next_scheduled' => [null, 'args' => $cronArgsType], 'WP_Query::have_posts' => [null, '@phpstan-impure' => ''],