From 980e931ab302f94a2bafe2d229345af1adc7a3f7 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 | 23 +++++++++++++++++++ 3 files changed, 28 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 63d0e6b494..0ac63052df 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -748,37 +748,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 7cca5ea24f..98ab525e18 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -1141,6 +1141,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7805.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-82.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4565.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 0000000000..133ce49b50 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-3789.php @@ -0,0 +1,23 @@ +