-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for dynamic return type in WP_List_Table
methods
#181
base: master
Are you sure you want to change the base?
Conversation
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.
Thank you! I wait for a review. |
a type inference test case, showing that this works and keeps working, would be great I guess? :) |
Please see https://phpstan.org/r/6f50bc44-c7db-41a7-b382-bca65c6332ec
|
I finished your example which passes at level 9. Just needed the class to exist and the generic to be passed. Technically works without the generic passed but not at level 9. |
I think |
I forget to mention the WP_List_Class also has an $items property which would benefit from a I couldn't figure out how to add a custom type to a property with the existing generate system and could use some help. |
|
Agreed. The more I think about this the more it seems like I may be trying to narrow something down beyond how something is actually used. In projects under our control we can determine the column names will match a key in the item, but in real world the column name could be anything. We may have to let this one go in favor of project specific overrides. 🤷♂️ |
How about dropping |
Could this be achieved with the following addition? class WP_List_Table {
/**
* @phpstan-assert $this->items T[]
*/
public function prepare_items() {}
}
WP_Post_List_Table uses items of type WP_Post. |
I mean from template |
If I would be a programmer I would definitely use a straight-forward OOP wrapper. |
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
I solved the requirements by making the following changes:
See it in action here. |
Guys! Could you try it in a project that is affected? |
Introduce a T
@template
in the WP_List_Table class to allow reducing the return type of many methods to the actual array or object shape used in the class.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 typeT
defined in the class.