Skip to content

DynamicReturnType for array search call not strict#215

Merged
ondrejmirtes merged 2 commits intophpstan:masterfrom
VincentLanglet:improveSearchArray
May 28, 2020
Merged

DynamicReturnType for array search call not strict#215
ondrejmirtes merged 2 commits intophpstan:masterfrom
VincentLanglet:improveSearchArray

Conversation

@VincentLanglet
Copy link
Copy Markdown
Contributor

Close phpstan/phpstan#3316

When a call array_search($foo, $array) or array_search($foo, $array, false) is made we can still restrict the type of the return value thanks to the type of the array keys.
And I saw no reason to add null as a possible return value.

Another thing. During writing/updating the tests, I saw

[
    'mixed',
    'array_search(1, $generalIntegers, true)',
],

When $generalIntegers is typed with int[].

This seems weird to me. There was a check about BenevolentUnionType, I don't see any reason to keep it ; tell me if I'm wrong. So I added a fix.

@ondrejmirtes
Copy link
Copy Markdown
Member

This is fine. Please fix the failed build so I can merge this, thanks.

@VincentLanglet
Copy link
Copy Markdown
Contributor Author

@ondrejmirtes Fixed !

@ondrejmirtes ondrejmirtes merged commit e9b9413 into phpstan:master May 28, 2020
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you :)

@adaamz
Copy link
Copy Markdown
Contributor

adaamz commented May 28, 2020

NULL is returned when some parameter doesnt match type.
doc: https://www.php.net/manual/en/function.array-search.php
playground: https://3v4l.org/6dsnC

@VincentLanglet
Copy link
Copy Markdown
Contributor Author

NULL is returned when some parameter doesnt match type.

I didn't removed all the null return type. Only the ones I thought as wrong.

@VincentLanglet VincentLanglet deleted the improveSearchArray branch May 28, 2020 08:07
@adaamz
Copy link
Copy Markdown
Contributor

adaamz commented May 28, 2020

Yeah, it was just answer to your question, which I now read correctly is not question 😄

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.

Array_search DynamicReturnType is not working

3 participants