Skip to content

Commit e947287

Browse files
Avoid false positif in_array always true
1 parent 42eac28 commit e947287

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public function findSpecifiedType(
157157
continue;
158158
}
159159

160+
$haystackArrayValueConstantScalarTypes = $haystackArrayValueType->getConstantScalarTypes();
161+
if (count($haystackArrayValueConstantScalarTypes) > 1) {
162+
continue;
163+
}
164+
160165
foreach ($haystackArrayValueType->getConstantScalarTypes() as $constantScalarType) {
161166
if ($constantScalarType->isSuperTypeOf($needleType)->yes()) {
162167
continue 3;

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,4 +981,10 @@ public function testBugPR3404(): void
981981
]);
982982
}
983983

984+
public function testBug13151(): void
985+
{
986+
$this->treatPhpDocTypesAsCertain = true;
987+
$this->analyse([__DIR__ . '/data/bug-13151.php'], []);
988+
}
989+
984990
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13151;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @return array{a:0|1, b:0|1, c:0|1}
9+
*/
10+
public function extractAsArray(): array
11+
{
12+
return [
13+
'a' => 1,
14+
'b' => 1,
15+
'c' => 1
16+
];
17+
}
18+
19+
public function test(): void
20+
{
21+
echo in_array(0, $this->extractAsArray(), true) ? "True" : "False";
22+
}
23+
24+
public function test2(): void
25+
{
26+
echo in_array(0, $this->extractAsArray()) ? "True" : "False";
27+
}
28+
}

0 commit comments

Comments
 (0)