diff --git a/src/Rules/Comparison/ImpossibleCheckTypeHelper.php b/src/Rules/Comparison/ImpossibleCheckTypeHelper.php index 1ebe5692897..97a01794056 100644 --- a/src/Rules/Comparison/ImpossibleCheckTypeHelper.php +++ b/src/Rules/Comparison/ImpossibleCheckTypeHelper.php @@ -141,6 +141,11 @@ public function findSpecifiedType( return null; } + + if (!$isNeedleSupertype->no()) { + // Array might be empty, so in_array can return false + return null; + } } if (!$haystackType instanceof ConstantArrayType || count($haystackType->getValueTypes()) > 0) { diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php index cf03dabd093..69e992139d8 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php @@ -1195,4 +1195,16 @@ public function testBug13566(): void ]); } + public function testBug13799(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-13799.php'], [ + [ + 'Call to function in_array() with arguments \'c\', list<\'a\'|\'b\'> and true will always evaluate to false.', + 24, + 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.', + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-13799.php b/tests/PHPStan/Rules/Comparison/data/bug-13799.php new file mode 100644 index 00000000000..8224e7e6def --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-13799.php @@ -0,0 +1,26 @@ + + */ +function get_whitelist(): array { + $s = []; + if (rand(0, 1)) { + $s[] = 'a'; + } + if (rand(0, 1)) { + $s[] = 'b'; + } + return $s; +} + +if (in_array('a', get_whitelist(), true)) { + echo 'ok'; +} + +if (in_array('c', get_whitelist(), true)) { + echo 'ok'; +}