Skip to content

Commit ba1ece5

Browse files
committed
Fix always-true detection in in_array with union type haystack
1 parent 85c709d commit ba1ece5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public function findSpecifiedType(
147147
foreach ($haystackArrayTypes as $haystackArrayType) {
148148
if ($haystackArrayType instanceof ConstantArrayType) {
149149
foreach ($haystackArrayType->getValueTypes() as $i => $haystackArrayValueType) {
150+
if ($haystackArrayValueType instanceof UnionType) {
151+
continue;
152+
}
153+
150154
if ($haystackArrayType->isOptionalKey($i)) {
151155
continue;
152156
}

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -971,4 +971,10 @@ public function testAlwaysTruePregMatch(): void
971971
$this->analyse([__DIR__ . '/data/always-true-preg-match.php'], []);
972972
}
973973

974+
public function testBug12755(): void
975+
{
976+
$this->treatPhpDocTypesAsCertain = true;
977+
$this->analyse([__DIR__ . '/data/bug-12755.php'], []);
978+
}
979+
974980
}
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)