From f38fccd2009aa44f7c4fe7d98cfa9aa3ea3fc2c2 Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Thu, 18 Aug 2022 15:54:44 +0200 Subject: [PATCH] Fix array_search extension incorrectly returning null --- ...earchFunctionDynamicReturnTypeExtension.php | 4 +--- .../PHPStan/Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-7809.php | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/PHPStan/Analyser/data/bug-7809.php diff --git a/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php b/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php index 7ef5a08adf..7190cb4d39 100644 --- a/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php +++ b/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php @@ -43,9 +43,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } $strictArgType = $scope->getType($functionCall->getArgs()[2]->value); - if (!($strictArgType instanceof ConstantBooleanType)) { - return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false), new NullType()); - } elseif ($strictArgType->getValue() === false) { + if (!$strictArgType instanceof ConstantBooleanType || $strictArgType->getValue() === false) { return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false)); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 69b06737f2..afc3a785d9 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -978,6 +978,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-argument-type.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7788.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7809.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-7809.php b/tests/PHPStan/Analyser/data/bug-7809.php new file mode 100644 index 0000000000..2769152110 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-7809.php @@ -0,0 +1,18 @@ +