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

Substracted type must be rendered in parenthesis if not simple #10227

Closed
mvorisek opened this issue Nov 29, 2023 · 5 comments · Fixed by phpstan/phpstan-src#3360
Closed

Substracted type must be rendered in parenthesis if not simple #10227

mvorisek opened this issue Nov 29, 2023 · 5 comments · Fixed by phpstan/phpstan-src#3360

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Nov 29, 2023

Bug report

Currently, the rendered type is wrong.

Code snippet that reproduces the problem

https://phpstan.org/r/1f20a038-6db0-4183-ad08-65e07202da24

Expected output

-Dumped type: mixed~0|0.0|''|'0'|array{}|false|null
+Dumped type: mixed~(0|0.0|''|'0'|array{}|false|null)
@mvorisek mvorisek changed the title Substracted type must be rendered if parenthesis if not simple Substracted type must be rendered in parenthesis if not simple Nov 29, 2023
@thg2k
Copy link

thg2k commented Aug 27, 2024

I agree it looks weird without parens, but it really depends on the grammar definition of the type syntax. Does ~ have a lower precedence of |?

I ran a quick test but it seems phpdoc parser does not accept ~

@mvorisek
Copy link
Contributor Author

I agree it looks weird without parens, but it really depends on the grammar definition of the type syntax. Does ~ have a lower precedence of |?

The answer is no, type like A~(false|null)|Z is currently rendered like A~false|null|Z. This is simply an unimplemented feature in the describe.

@phpstan-bot
Copy link
Contributor

@mvorisek After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
-8: Dumped type: mixed~0|0.0|''|'0'|array{}|false|null
+PHP 8.0 – 8.3 (1 error)
+==========
+
+8: Dumped type: mixed~0|0.0|''|'0'|array{}|false|null
+
+PHP 7.2 – 7.4 (3 errors)
+==========
+
+5: Parameter $arr of method HelloWorld::test() has invalid type mixed.
+7: Variable $arr in empty() always exists and is not falsy.
+8: Dumped type: mixed
Full report

PHP 8.0 – 8.3 (1 error)

Line Error
8 `Dumped type: mixed~0

PHP 7.2 – 7.4 (3 errors)

Line Error
5 Parameter $arr of method HelloWorld::test() has invalid type mixed.
7 Variable $arr in empty() always exists and is not falsy.
8 Dumped type: mixed

@ondrejmirtes
Copy link
Member

Implemented phpstan/phpstan-src#3360

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants