szepeviktor / phpstan-wordpress

WordPress extensions for PHPStan ⛏️
https://packagist.org/packages/szepeviktor/phpstan-wordpress
MIT License
267 stars 25 forks source link

Add dynamic return type extension for `get_post_types` #177

Closed lipemat closed 1 year ago

lipemat commented 1 year ago

Introduce GetPostTypesDynamicFunctionReturnTypeExtension service

The get_post_types function return either and array of string or an array of WP_Post_Type depending on the passed arguments.

https://developer.wordpress.org/reference/functions/get_post_types/#return

szepeviktor commented 1 year ago

Could it be done with the new conditional type in WP stubs? @phpstan-assert-if... https://phpstan.org/writing-php-code/narrowing-types#custom-type-checking-functions-and-methods

herndlm commented 1 year ago

Could it be done with the new conditional type in WP stubs? @phpstan-assert-if... https://phpstan.org/writing-php-code/narrowing-types#custom-type-checking-functions-and-methods

good point, looks like this would work: https://phpstan.org/r/6e188841-6faf-4de8-9c0f-57ebabc3e626

lipemat commented 1 year ago

can you add a new file with assertion tests, please? an example would be e.g. #144

Tests are now included should you decide to use this code. :)

szepeviktor commented 1 year ago

Thank you for your work. I am very sorry. That conditional thing, Martin wrote, makes PHPStan work alone. That should be added to WP stubs.

IanDelMar commented 1 year ago

The conditional thing then must work for get_taxonomies() as well.

szepeviktor commented 1 year ago

@lipemat Conditional return types arrived in https://github.com/php-stubs/wordpress-stubs/pull/73

Thank you for your PR!!

szepeviktor commented 1 year ago

I will use your tests. tests/data/get_post_types.php