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 dynamic return type extension for get_post_types #177

Closed
wants to merge 2 commits into from
Closed

Add dynamic return type extension for get_post_types #177

wants to merge 2 commits into from

Conversation

lipemat
Copy link
Contributor

@lipemat lipemat commented Apr 13, 2023

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

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

@herndlm herndlm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

lipemat added a commit to lipemat/phpstan-wordpress that referenced this pull request Apr 13, 2023
Account for different results from `get_post_types` based on the
arguments passed.

Also submitted a pull request upstream and simply keeping this here in
case that does not get merged.

@todo Remove this extension if the pull request gets merged and released upstream

szepeviktor/phpstan-wordpress#177
@szepeviktor
Copy link
Owner

szepeviktor commented Apr 13, 2023

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
Copy link
Contributor

herndlm commented Apr 13, 2023

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

lipemat commented Apr 14, 2023

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
Copy link
Owner

szepeviktor commented Apr 14, 2023

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
Copy link
Contributor

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

@szepeviktor
Copy link
Owner

@lipemat Conditional return types arrived in php-stubs/wordpress-stubs#73

Thank you for your PR!!

@szepeviktor
Copy link
Owner

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

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