php-stubs / wordpress-stubs

Up-to-date WordPress function and class declaration stubs for static analysis by PHPStan
https://packagist.org/packages/php-stubs/wordpress-stubs
MIT License
151 stars 19 forks source link

Additional tests and fix for WP_REST_Request #190

Closed IanDelMar closed 4 months ago

IanDelMar commented 4 months ago

Tests for array<string, string> were still failing. For now, I don't know how to remove null from the return type of WP_REST_Request::get_parameter() when used with an array shape. Either the array shape works, or the general array types work. The current version at least narrows down types without producing incorrect types, such as bool|int|string, when WP_REST_Request::get_parameter() is called with a non-existent parameter. Maybe @lipemat has an idea.

According to the doc blocks in WP core, $key is always a string. So, it might be reasonable to change '@phpstan-template' => 'T of array' to '@phpstan-template' => 'T of array<string, mixed>'.

szepeviktor commented 4 months ago

What does T[TOffset] mean?

herndlm commented 4 months ago

Index types like in https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html

Was implemented for / via https://github.com/phpstan/phpstan/issues/7094 but might have never been fully documented. I didn't use them in php yet tbh though :)

szepeviktor commented 4 months ago

All right. I wouldn't use arrays for non-list data. Only objects.

Thank you both.