From fe102f9f4a6237b20a7cc00da8ea90c1bdd0973e Mon Sep 17 00:00:00 2001 From: Takuya Aramaki Date: Sat, 5 Nov 2022 21:57:52 +0900 Subject: [PATCH] Fix return type of array_search() with constant array type haystack --- src/Type/Constant/ConstantArrayType.php | 19 ++++--------------- .../Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-3789.php | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 tests/PHPStan/Analyser/data/bug-3789.php diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 1cebc81f3c5..17a92a98bf0 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -743,37 +743,26 @@ public function popArray(): Type public function searchArray(Type $needleType): Type { $matches = []; - $optionalDirectMatches = []; + $hasIdenticalValue = false; foreach ($this->valueTypes as $index => $valueType) { $isNeedleSuperType = $valueType->isSuperTypeOf($needleType); if ($isNeedleSuperType->no()) { - $matches[] = new ConstantBooleanType(false); continue; } if ($needleType instanceof ConstantScalarType && $valueType instanceof ConstantScalarType && $needleType->getValue() === $valueType->getValue() + && !$this->isOptionalKey($index) ) { - if (!$this->isOptionalKey($index)) { - return TypeCombinator::union($this->keyTypes[$index], ...$optionalDirectMatches); - } - $optionalDirectMatches[] = $this->keyTypes[$index]; + $hasIdenticalValue = true; } $matches[] = $this->keyTypes[$index]; - if (!$isNeedleSuperType->maybe()) { - continue; - } - - $matches[] = new ConstantBooleanType(false); } if (count($matches) > 0) { - if ( - $this->getIterableValueType()->accepts($needleType, true)->yes() - && $needleType->isSuperTypeOf(new ObjectWithoutClassType())->no() - ) { + if ($hasIdenticalValue) { return TypeCombinator::union(...$matches); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index a827d06f3b9..54184b793ae 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -1124,6 +1124,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8272.php'); yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-8277.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/strtr.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3789.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-3789.php b/tests/PHPStan/Analyser/data/bug-3789.php new file mode 100644 index 00000000000..c5d89f2a5df --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-3789.php @@ -0,0 +1,17 @@ +