-
-
Notifications
You must be signed in to change notification settings - Fork 884
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
Array unions are improperly collapsed #3801
Comments
Actually, this seems to be an issue with array unions in general: I think my original message may have misemphasized the issue: the concern isn't just that the returned array type isn't deduced from the typecheck, but that an error isn't raised when an array of incorrect type is returned: |
Hi, just a heads-up - I consider this a feature request, with such complex needs you'd be much better served modeling your types as objects. |
I would consider it, but I'm getting bit by #3034 |
@gnarlyquack PHPStan now reports different result with your code snippet: @@ @@
45: Function foo\bar() never returns foo\Error<array<string>> so it can be removed from the return typehint.
-65: Function foo\bar() should return foo\Error<array<string>>|foo\Success<bool> but returns foo\Error<array<int, string>>.
+65: Function foo\bar() should return foo\Error<array<string>>|foo\Success<bool> but returns foo\Error<array(string)>. Full report
|
@gnarlyquack PHPStan now reports different result with your code snippet: @@ @@
45: Function foo\bar() never returns foo\Error<array<int, string>> so it can be removed from the return typehint.
-65: Function foo\bar() should return foo\Error<array<int, string>>|foo\Success<bool> but returns foo\Error<array<int, string>>.
+65: Function foo\bar() should return foo\Error<array<int, string>>|foo\Success<bool> but returns foo\Error<array(string)>. Full report
|
Another example for this feature request https://phpstan.org/r/14283564-cafe-450f-a734-ab6188da40d8 - bit me while trying to type the write() function here https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/AbstractProcessingHandler.php#L40 which accepts a record array with the addition of a Not a huge deal, I duplicated the record array in this one special instance, but would have been neat if it just worked :) And yes I can see the argument that records should be objects perhaps, but they've been arrays for 11+ years and there's lots of code working with that assumption. |
@gnarlyquack After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-45: Function foo\bar() never returns foo\Error<array<string>> so it can be removed from the return typehint.
-65: Function foo\bar() should return foo\Error<array<string>>|foo\Success<bool> but returns foo\Error<array<int, string>>.
+45: Function foo\bar() never returns foo\Error<array<string>> so it can be removed from the return type.
+65: Function foo\bar() should return foo\Error<array<string>>|foo\Success<bool> but returns foo\Error<array(string)>. Full report
|
@gnarlyquack After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-45: Function foo\bar() never returns foo\Error<array<int, string>> so it can be removed from the return typehint.
-65: Function foo\bar() should return foo\Error<array<int, string>>|foo\Success<bool> but returns foo\Error<array<int, string>>.
+45: Function foo\bar() never returns foo\Error<array<int, string>> so it can be removed from the return type.
+65: Function foo\bar() should return foo\Error<array<int, string>>|foo\Success<bool> but returns foo\Error<array(string)>. Full report
|
@gnarlyquack After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
10: Parameter #1 $flag of function do_bar expects bool, true|null given.
-30: Function do_foo() should return array(true|null, true|null) but returns array(null, int<1, max>).
+30: Function do_foo() should return array{true|null, true|null} but returns array{null, int<1, max>}. Full report
|
@gnarlyquack After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-45: Function foo\bar() never returns foo\Error<array<string>> so it can be removed from the return typehint.
-65: Function foo\bar() should return foo\Error<array<string>>|foo\Success<bool> but returns foo\Error<array<int, string>>.
+45: Function foo\bar() never returns foo\Error<array<string>> so it can be removed from the return type.
+65: Function foo\bar() should return foo\Error<array<string>>|foo\Success<bool> but returns foo\Error<array{string}>. Full report
|
@gnarlyquack After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-45: Function foo\bar() never returns foo\Error<array<int, string>> so it can be removed from the return typehint.
-65: Function foo\bar() should return foo\Error<array<int, string>>|foo\Success<bool> but returns foo\Error<array<int, string>>.
+45: Function foo\bar() never returns foo\Error<array<int, string>> so it can be removed from the return type.
+65: Function foo\bar() should return foo\Error<array<int, string>>|foo\Success<bool> but returns foo\Error<array{string}>. Full report
|
@gnarlyquack After the latest push in 1.8.x, PHPStan now reports different result with your code snippet: @@ @@
10: Parameter #1 $flag of function do_bar expects bool, true|null given.
-30: Function do_foo() should return array(true|null, true|null) but returns array(null, int<1, max>).
+30: Function do_foo() should return array{null, true}|array{true, null} but returns array{null, int<1, max>}. Full report
|
@gnarlyquack After the latest push in 1.8.x, PHPStan now reports different result with your code snippet: @@ @@
-No errors
+ 7: Function do_foo() never returns array{null, bool} so it can be removed from the return type.
+15: Function do_foo() should return array{bool, null}|array{null, bool} but returns array{false, true}.
+19: Function do_foo() should return array{bool, null}|array{null, bool} but returns array{false, false}. Full report
|
Hi, Thanks for working on this. This appears to fix the specific error on line 30 in the original code snippet, but in general this still doesn't seem to be working correctly: https://phpstan.org/r/794c728c-a1a8-4dee-8c80-42fcd9907ac8 Same thing here: I also see that in line 10 of the original snippet, the type of $result is still not being correctly determined. If $error isn't set (i.e., null), then $result must be a bool, so there shouldn't be an error. |
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
array{a, b}|array{c, d}
is improperly collapsed asarray{a|c, b|d}
Code snippet that reproduces the problem
https://phpstan.org/r/2fe252ba-d874-4f42-9164-d5672993d555
Expected output
Line 10: No error
Line 30:
Function do_foo() should return array(true, null)|array(null, true) but returns ....
Actually, something (else) weird seems to be going on in line 30 since the reported "actual" type doesn't seem to make any sense.
The text was updated successfully, but these errors were encountered: