Skip to content

Commit 63cc1b7

Browse files
committed
Fix always-true detection in in_array with union type haystack
1 parent e8e58db commit 63cc1b7

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public function findSpecifiedType(
148148
foreach ($haystackArrayTypes as $haystackArrayType) {
149149
if ($haystackArrayType instanceof ConstantArrayType) {
150150
foreach ($haystackArrayType->getValueTypes() as $i => $haystackArrayValueType) {
151+
if ($haystackArrayValueType instanceof UnionType) {
152+
continue;
153+
}
154+
151155
if ($haystackArrayType->isOptionalKey($i)) {
152156
continue;
153157
}

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,4 +1004,10 @@ public function testBugPR3404(): void
10041004
]);
10051005
}
10061006

1007+
public function testBug12755(): void
1008+
{
1009+
$this->treatPhpDocTypesAsCertain = true;
1010+
$this->analyse([__DIR__ . '/data/bug-12755.php'], []);
1011+
}
1012+
10071013
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12755;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param array{
9+
* key1: ?int,
10+
* key2: ?string,
11+
* } $myArray
12+
*/
13+
public function testOther(array $myArray): ?\stdClass
14+
{
15+
if (\in_array(null, $myArray, true)) {
16+
return null;
17+
}
18+
19+
return (object) $myArray;
20+
}
21+
22+
/**
23+
* @param array{
24+
* key1: ?bool,
25+
* } $myArray
26+
*/
27+
public function testBool(array $myArray): ?\stdClass
28+
{
29+
if (\in_array(null, $myArray, true)) {
30+
return null;
31+
}
32+
33+
return (object) $myArray;
34+
}
35+
}

0 commit comments

Comments
 (0)