diff --git a/src/Rules/Comparison/ImpossibleCheckTypeHelper.php b/src/Rules/Comparison/ImpossibleCheckTypeHelper.php index 269e032be9..b0e71448b5 100644 --- a/src/Rules/Comparison/ImpossibleCheckTypeHelper.php +++ b/src/Rules/Comparison/ImpossibleCheckTypeHelper.php @@ -157,6 +157,11 @@ public function findSpecifiedType( continue; } + $haystackArrayValueConstantScalarTypes = $haystackArrayValueType->getConstantScalarTypes(); + if (count($haystackArrayValueConstantScalarTypes) > 1) { + continue; + } + foreach ($haystackArrayValueType->getConstantScalarTypes() as $constantScalarType) { if ($constantScalarType->isSuperTypeOf($needleType)->yes()) { continue 3; diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index bc8f4ac04e..32d7746d54 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -88,6 +88,7 @@ private static function findTestFiles(): iterable yield __DIR__ . '/../Rules/Variables/data/bug-10577.php'; yield __DIR__ . '/../Rules/Variables/data/bug-10610.php'; yield __DIR__ . '/../Rules/Comparison/data/bug-2550.php'; + yield __DIR__ . '/../Rules/Comparison/data/bug-12412.php'; yield __DIR__ . '/../Rules/Properties/data/bug-3777.php'; yield __DIR__ . '/../Rules/Methods/data/bug-4552.php'; yield __DIR__ . '/../Rules/Methods/data/infer-array-key.php'; diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php index 2ffbddf715..f83bd6f26f 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php @@ -981,4 +981,28 @@ public function testBugPR3404(): void ]); } + public function testBug13151(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-13151.php'], []); + } + + public function testBug8818(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-8818.php'], []); + } + + public function testBug12755(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-12755.php'], []); + } + + public function testBug12412(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-12412.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-12412.php b/tests/PHPStan/Rules/Comparison/data/bug-12412.php new file mode 100644 index 0000000000..397088dfa8 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-12412.php @@ -0,0 +1,26 @@ + + */ +function f(string $type): array { + $field_list = [ ]; + if ($type === 'A') { + array_push($field_list, 'x1'); + } + if ($type === 'B') { + array_push($field_list, 'x2'); + } + + assertType('bool', in_array('x1', $field_list, true)); + + array_push($field_list, 'x3'); + assertType('bool', in_array('x1', $field_list, true)); + + return $field_list; +} diff --git a/tests/PHPStan/Rules/Comparison/data/bug-12755.php b/tests/PHPStan/Rules/Comparison/data/bug-12755.php new file mode 100644 index 0000000000..6df2876e3b --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-12755.php @@ -0,0 +1,35 @@ + 1, + 'b' => 1, + 'c' => 1 + ]; + } + + public function test(): void + { + echo in_array(0, $this->extractAsArray(), true) ? "True" : "False"; + } + + public function test2(): void + { + echo in_array(0, $this->extractAsArray()) ? "True" : "False"; + } +} diff --git a/tests/PHPStan/Rules/Comparison/data/bug-8818.php b/tests/PHPStan/Rules/Comparison/data/bug-8818.php new file mode 100644 index 0000000000..b2d506a9bc --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-8818.php @@ -0,0 +1,20 @@ + $stack + */ + public function sayHello(array $stack): bool + { + return count($stack) === 1 && in_array(MyEnum::ONE, $stack, true); + } +}