Skip to content

Commit 40a3535

Browse files
Fix HasOffsetValueType::searchArray
1 parent 812d7da commit 40a3535

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
use PHPStan\Type\Constant\ConstantStringType;
1515
use PHPStan\Type\ConstantScalarType;
1616
use PHPStan\Type\ErrorType;
17+
use PHPStan\Type\GeneralizePrecision;
1718
use PHPStan\Type\IntegerRangeType;
19+
use PHPStan\Type\IntegerType;
1820
use PHPStan\Type\IntersectionType;
1921
use PHPStan\Type\IsSuperTypeOfResult;
2022
use PHPStan\Type\MixedType;
2123
use PHPStan\Type\ObjectWithoutClassType;
24+
use PHPStan\Type\StringType;
2225
use PHPStan\Type\Traits\MaybeArrayTypeTrait;
2326
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
2427
use PHPStan\Type\Traits\MaybeIterableTypeTrait;
@@ -266,7 +269,10 @@ public function searchArray(Type $needleType): Type
266269
$needleType instanceof ConstantScalarType && $this->valueType instanceof ConstantScalarType
267270
&& $needleType->getValue() === $this->valueType->getValue()
268271
) {
269-
return $this->offsetType;
272+
return new UnionType([
273+
new IntegerType(),
274+
new StringType(),
275+
]);
270276
}
271277

272278
return new MixedType();

tests/PHPStan/Analyser/nsrt/array-search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function normalArrays(array $arr, string $string): void
2929
}
3030

3131
if (array_key_exists(17, $arr) && $arr[17] === 'foo') {
32-
assertType('17', array_search('foo', $arr, true));
32+
assertType('int', array_search('foo', $arr, true));
3333
assertType('int|false', array_search('foo', $arr));
3434
assertType('int|false', array_search($string, $arr, true));
3535
}

0 commit comments

Comments
 (0)