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
Type of ReflectionEnum::getBackingType() should/could be narrowed after ::isBacked() #10167
Comments
Alternatively you can write this more SA-friendly code https://phpstan.org/r/03d3e5a0-4820-4c4f-a481-c728f6f14735 |
I think this would be a relatively simple issue which I might fix myself given some pointers. I think it might even be possible to implement it using simple assertions in a stub file? Based on narrowing types (especially the <?php
class ReflectionEnum
{
/**
* @phpstan-assert-if-true !null $this->getBackingType()
*/
public function isBacked()
{
}
} Correct? I'll then just need some pointers on testing the stub |
First off, wow, that reply was way quicker then I expected 😃. Secondly, you're right, not sure why I didn't think of that. But still would be nice if PHPStan automatically narrowed the type 😅. And might be a good "first issue" for myself to learn more about PHPStan "internals". |
Yeah, sure, should be easy. Just check how I tested the return type of getCase/getCases around the same commit ReflectionEnum stub was added. Hint: in NodeScopeResolverTest |
Would be possible to also narrow the template type to https://phpstan.org/r/63e742a3-f3b7-4d5a-9c6e-3a60de5bfa32 Update: Just saw #10192 (comment) |
@franmomu After the latest push in 1.11.x, PHPStan now reports different result with your code snippet: @@ @@
==========
6: Dumped type: class-string<UnitEnum>
-8: Dumped type: class-string<UnitEnum>
+8: Dumped type: class-string<BackedEnum>
PHP 7.2 – 8.0 (7 errors)
========== Full reportPHP 8.1 – 8.3 (2 errors)
PHP 7.2 – 8.0 (7 errors)
|
Fixed phpstan/phpstan-src#2830 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
The formal return value of
ReflectionEnum::getBackingType()
is?ReflectionNamedType
. But it will always returnReflectionNamedType
(i.e.: be non-null) for backed enums. So when aReflectionEnum::isBacked
condition is applied the type could/should be narrowed.Code snippet that reproduces the problem
https://phpstan.org/r/6708f7db-5547-47c6-aa37-38dc5974a1a4
Expected output
Type could/should be just
ReflectionNamedType
(but isReflectionNamedType|null
).Did PHPStan help you today? Did it make you happy in any way?
No response
The text was updated successfully, but these errors were encountered: