Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

lipemat
Copy link
Contributor

@lipemat lipemat commented Jun 7, 2024

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 type T defined 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 type `T` defined in the class.
@szepeviktor
Copy link
Member

Thank you!

I wait for a review.

@herndlm
Copy link
Contributor

herndlm commented Jun 8, 2024

a type inference test case, showing that this works and keeps working, would be great I guess? :)

@IanDelMar
Copy link
Contributor

Please see https://phpstan.org/r/6f50bc44-c7db-41a7-b382-bca65c6332ec

Parameter $item of method WP_Privacy_Requests_Table::column_cb() has invalid type WP_User_Request.

@lipemat
Copy link
Contributor Author

lipemat commented Jun 8, 2024

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.

https://phpstan.org/r/4c23cb5c-566c-4038-8ac6-440955dcd0a2

@szepeviktor
Copy link
Member

I think key-of<> does not work with an object.
https://phpstan.org/writing-php-code/phpdoc-types
What do you think?

@lipemat
Copy link
Contributor Author

lipemat commented Jun 8, 2024

I forget to mention the WP_List_Class also has an $items property which would benefit from a T[] type.

I couldn't figure out how to add a custom type to a property with the existing generate system and could use some help.

@szepeviktor
Copy link
Member

WP_List_Table is an anti-everything tragedy.

@lipemat
Copy link
Contributor Author

lipemat commented Jun 8, 2024

WP_List_Table is an anti-everything tragedy.

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. 🤷‍♂️

@szepeviktor
Copy link
Member

How about dropping |object? Does any decent code uses an object?

@IanDelMar
Copy link
Contributor

I forget to mention the WP_List_Class also has an $items property which would benefit from a T[] type.

I couldn't figure out how to add a custom type to a property with the existing generate system and could use some help.

Could this be achieved with the following addition?

class WP_List_Table {
    /**
     * @phpstan-assert $this->items T[]
     */
    public function prepare_items() {}
}

How about dropping |object? Does any decent code uses an object?

WP_Post_List_Table uses items of type WP_Post.

@szepeviktor
Copy link
Member

How about dropping |object? Does any decent code uses an object?

WP_Post_List_Table uses items of type WP_Post.

I mean from template T only.
Does anyone uses objects there?

@szepeviktor
Copy link
Member

szepeviktor commented Jun 9, 2024

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
@lipemat
Copy link
Contributor Author

lipemat commented Jun 9, 2024

I solved the requirements by making the following changes:

  1. Introduce a second template K to be used for the column names.
  2. Update the $column_name parameters and get_columns return to use K instead of key-of<T>.

See it in action here.
https://phpstan.org/r/a4104203-bd28-459c-aff5-27715708acbe

@szepeviktor
Copy link
Member

szepeviktor commented Jun 9, 2024

Guys! Could you try it in a project that is affected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants