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

Add ArrayFindFunctionReturnTypeExtension #3518

Draft
wants to merge 3 commits into
base: 1.12.x
Choose a base branch
from

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Sep 30, 2024

Supports PHP RFC: array_find for PHP 8.4.

function testConstant(array $array, callable $callback): void
{
assertType("1|'foo'|DateTime|null", array_find($array, $callback));
assertType("1|null", array_find($array, 'is_int'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be Null.

Copy link
Contributor Author

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'));
Copy link
Contributor

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

Copy link
Contributor Author

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().

https://phpstan.org/r/ab62bff1-d485-46ae-9dfa-1931ce320a25

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'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be 0

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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'));
Copy link
Contributor

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

Copy link
Contributor Author

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.

https://3v4l.org/Biok8

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, my bad.

@zonuexe zonuexe marked this pull request as draft October 4, 2024 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants