-
Notifications
You must be signed in to change notification settings - Fork 459
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
Add ArrayFindFunctionReturnTypeExtension #3518
base: 1.12.x
Are you sure you want to change the base?
Conversation
function testConstant(array $array, callable $callback): void | ||
{ | ||
assertType("1|'foo'|DateTime|null", array_find($array, $callback)); | ||
assertType("1|null", array_find($array, 'is_int')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cannot be Null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed f0d9c25
function testMixed(array $array, callable $callback): void | ||
{ | ||
assertType('mixed', array_find($array, $callback)); | ||
assertType('int|null', array_find($array, 'is_int')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also test it on non empty array, and the result should be an int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it can be improved at the same time as array_filter()
.
function testConstant(array $array, callable $callback): void | ||
{ | ||
assertType("0|1|2|null", array_find_key($array, $callback)); | ||
assertType("0|1|2|null", array_find_key($array, 'is_int')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, that's correct. This PR does not implement the logic to detect this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the current behavior ? I assume the type is considered as mixed.
This means that merging the PR as if will introduce a lot of false positif PHPstan errors on level 7 (unions) when it's currently on level 9 (mixed).
That's why IMHO if a dynamic return type extension is introduced, the implementation need to handle such case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the current behavior ? I assume the type is considered as mixed.
just copy the test over into the playground:
https://phpstan.org/r/ece55e5c-33bf-47cb-b538-35708d025dfa
function testMixed(array $array, callable $callback): void | ||
{ | ||
assertType('int|string|null', array_find_key($array, $callback)); | ||
assertType('int|string|null', array_find_key($array, 'is_int')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should test on non-empty-array and it should gives int|string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a test case, but if you pass a non-empty-array<mixed>
the function may return any int|string|null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, my bad.
581fba3
to
7dd1e43
Compare
Supports PHP RFC: array_find for PHP 8.4.