-
Notifications
You must be signed in to change notification settings - Fork 504
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
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
7dd1e43
to
380c077
Compare
This pull request has been marked as ready for review. |
* @param self::USE_* $mode | ||
* @return array{list<Arg>, ?Variable, ?Variable} | ||
*/ | ||
private function createDummyArgs(int $mode): array |
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.
Do this refactoring in a separate PR first please.
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.
Ok, create a pull request.
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.
Cherry-picked #3606
8df85ed
to
cc25101
Compare
I just cherry-picked them, so there should be no conflicts when #3606 is merged. |
Fix CS please. |
3a80801
to
2a12198
Compare
@ondrejmirtes I fixed and rebased. |
]; | ||
} | ||
|
||
if (isset($args[0]) && (bool) $args[0]->getAttribute(ArrayWalkArgVisitor::ATTRIBUTE_NAME)) { |
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.
Why is there a section about array_walk and it has no tests? It should be a separate PR in any 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.
Oops, I copied too much existing code.
I'm sure you're right, I'll split out the changes to ParametersAcceptorSelector into a separate PR.
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.
Now you’ve removed too much 😀 It’s useful to declare the callable parameters for two reasons:
- To infer the parameter type inside the callable based on array (even when the parameter does not have a native type).
- To report wrong callable parameter tpe when it’s different to the passed array.
Please add tests for these.
Ok, I'll get to it right away. |
…rnTypeExtension
7d72141
to
8459f4d
Compare
8459f4d
to
104d3fb
Compare
This pull request has been marked as ready for review. |
Perfect, thank you! |
Supports PHP RFC: array_find for PHP 8.4.