-
Notifications
You must be signed in to change notification settings - Fork 509
Fix always-true detection in in_array with union type haystack #3888
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
base: 2.1.x
Are you sure you want to change the base?
Conversation
your PR has impact on more open bugs. please look at the changes and decide whether these are correct/intended please add additional tests to cover this bugs, in case they are fixed |
/** | ||
* @param array{ | ||
* key1: ?bool, | ||
* } $myArray | ||
*/ | ||
public function testBool(array $myArray): ?\stdClass | ||
{ | ||
if (\in_array(null, $myArray, true)) { | ||
return null; | ||
} | ||
|
||
return (object) $myArray; | ||
} |
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 a case which is expected to still emit an error after the change (a array which always contains null)
@@ -147,7 +147,7 @@ public function findSpecifiedType( | |||
foreach ($haystackArrayTypes as $haystackArrayType) { | |||
if ($haystackArrayType instanceof ConstantArrayType) { | |||
foreach ($haystackArrayType->getValueTypes() as $i => $haystackArrayValueType) { | |||
if ($haystackArrayType->isOptionalKey($i)) { | |||
if ($haystackArrayValueType instanceof UnionType || $haystackArrayType->isOptionalKey($i)) { |
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.
I believe there's a better way to ask this. Something like count($haystackArrayValueType->getFiniteTypes()) === 1
.
resolve phpstan/phpstan#12755