Skip to content

Add dynamic return type extension for get_post_types#177

Closed
lipemat wants to merge 2 commits intoszepeviktor:masterfrom
lipemat:feature/get-post-types-extension
Closed

Add dynamic return type extension for get_post_types#177
lipemat wants to merge 2 commits intoszepeviktor:masterfrom
lipemat:feature/get-post-types-extension

Conversation

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

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

@szepeviktor
Copy link
Copy Markdown
Owner

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

Thank you for your PR!!

@szepeviktor
Copy link
Copy Markdown
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