-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Treat assertNotWPError() and assertWPError() as type-narrowing functions #30
Comments
All right. |
Could you try installing |
Yes the idea is that any parameter passed to https://phpstan.org/writing-php-code/narrowing-types I tried phpstan-phpunit but it had no effect, but I didn't play with it much. |
I hope we can tell PHPStan that @ondrejmirtes Could you give us directions? |
@johnbillion Could you point me to a simple WordPress plugin that has a unit test with |
Yes, you need to write and register a similar extension that phpstan-phpunit has: https://github.com/phpstan/phpstan-phpunit/blob/master/src/Type/PHPUnit/Assert/AssertMethodTypeSpecifyingExtension.php |
Some info about them also here: https://phpstan.org/developing-extensions/type-specifying-extensions |
Thank you. Now we need those hands writing code. |
@monojp Please advise. |
I'm not an expert and never used those WP test helpers. But it looks they are at least not part of the official WP release. Are they WP internal? Does WP advise to use them for plugin developers and if the do - how? I quickly searched for it but did not find out much |
@mono Here are your suspects The problem is these method narrow down types. |
@johnbillion are you still interested in this? If you could just provide me with an easy to work on example I can implement this next Wednesday, should not be hard. I just don't want to spend ages setting up the WP testing framework that I don't know anything about yet |
In the WordPress core test suite the following assertion methods are available:
$this->assertNotWPError()
$this->assertWPError()
It would be great if this extension treated these methods as type-narrowing functions, so the following code would not throw an error in PHPStan:
Currently this triggers the following error because
$terms
is assumed to bearray|WP_Error
on the last line.The text was updated successfully, but these errors were encountered: