Skip to content

Commit 80d8fc6

Browse files
Fix HasOffsetValueType::searchArray
1 parent 812d7da commit 80d8fc6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
use PHPStan\Type\ConstantScalarType;
1616
use PHPStan\Type\ErrorType;
1717
use PHPStan\Type\IntegerRangeType;
18+
use PHPStan\Type\IntegerType;
1819
use PHPStan\Type\IntersectionType;
1920
use PHPStan\Type\IsSuperTypeOfResult;
2021
use PHPStan\Type\MixedType;
2122
use PHPStan\Type\ObjectWithoutClassType;
23+
use PHPStan\Type\StringType;
2224
use PHPStan\Type\Traits\MaybeArrayTypeTrait;
2325
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
2426
use PHPStan\Type\Traits\MaybeIterableTypeTrait;
@@ -266,7 +268,10 @@ public function searchArray(Type $needleType): Type
266268
$needleType instanceof ConstantScalarType && $this->valueType instanceof ConstantScalarType
267269
&& $needleType->getValue() === $this->valueType->getValue()
268270
) {
269-
return $this->offsetType;
271+
return new UnionType([
272+
new IntegerType(),
273+
new StringType(),
274+
]);
270275
}
271276

272277
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)