From 80d8fc6fd277e34cdcceb022410273dd4b468629 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 24 May 2025 14:18:54 +0200 Subject: [PATCH] Fix HasOffsetValueType::searchArray --- src/Type/Accessory/HasOffsetValueType.php | 7 ++++++- tests/PHPStan/Analyser/nsrt/array-search.php | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Type/Accessory/HasOffsetValueType.php b/src/Type/Accessory/HasOffsetValueType.php index b673a3e7eb..6090022576 100644 --- a/src/Type/Accessory/HasOffsetValueType.php +++ b/src/Type/Accessory/HasOffsetValueType.php @@ -15,10 +15,12 @@ use PHPStan\Type\ConstantScalarType; use PHPStan\Type\ErrorType; use PHPStan\Type\IntegerRangeType; +use PHPStan\Type\IntegerType; use PHPStan\Type\IntersectionType; use PHPStan\Type\IsSuperTypeOfResult; use PHPStan\Type\MixedType; use PHPStan\Type\ObjectWithoutClassType; +use PHPStan\Type\StringType; use PHPStan\Type\Traits\MaybeArrayTypeTrait; use PHPStan\Type\Traits\MaybeCallableTypeTrait; use PHPStan\Type\Traits\MaybeIterableTypeTrait; @@ -266,7 +268,10 @@ public function searchArray(Type $needleType): Type $needleType instanceof ConstantScalarType && $this->valueType instanceof ConstantScalarType && $needleType->getValue() === $this->valueType->getValue() ) { - return $this->offsetType; + return new UnionType([ + new IntegerType(), + new StringType(), + ]); } return new MixedType(); diff --git a/tests/PHPStan/Analyser/nsrt/array-search.php b/tests/PHPStan/Analyser/nsrt/array-search.php index f26f6b7536..f11d02cb24 100644 --- a/tests/PHPStan/Analyser/nsrt/array-search.php +++ b/tests/PHPStan/Analyser/nsrt/array-search.php @@ -29,7 +29,7 @@ public function normalArrays(array $arr, string $string): void } if (array_key_exists(17, $arr) && $arr[17] === 'foo') { - assertType('17', array_search('foo', $arr, true)); + assertType('int', array_search('foo', $arr, true)); assertType('int|false', array_search('foo', $arr)); assertType('int|false', array_search($string, $arr, true)); }