Skip to content

Commit aed4b58

Browse files
herndlmondrejmirtes
authored andcommitted
Use TypeUtils::getOldConstantArrays in array_map extension
1 parent ca616c3 commit aed4b58

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Type/Php/ArrayMapFunctionReturnTypeExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6363
if ($callableIsNull) {
6464
return $arrayType;
6565
}
66-
$constantArrays = TypeUtils::getConstantArrays($arrayType);
66+
$constantArrays = TypeUtils::getOldConstantArrays($arrayType);
6767
if (count($constantArrays) > 0) {
6868
$arrayTypes = [];
6969
foreach ($constantArrays as $constantArray) {
7070
$returnedArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
71-
foreach ($constantArray->getKeyTypes() as $keyType) {
71+
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
7272
$returnedArrayBuilder->setOffsetValueType(
7373
$keyType,
7474
$valueType,
75+
$constantArray->isOptionalKey($i),
7576
);
7677
}
7778
$arrayTypes[] = $returnedArrayBuilder->getArray();

tests/PHPStan/Analyser/data/array-map.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,15 @@ static function(string $string): string {
6060

6161
assertType('non-empty-array<int, string>', $mapped);
6262
}
63+
64+
/** @param array{foo?: 0, bar?: 1, baz?: 2} $array */
65+
function foo5(array $array): void {
66+
$mapped = array_map(
67+
static function(string $string): string {
68+
return (string) $string;
69+
},
70+
$array
71+
);
72+
73+
assertType('array{foo?: string, bar?: string, baz?: string}', $mapped);
74+
}

0 commit comments

Comments
 (0)