Skip to content

Commit 2016780

Browse files
VincentLangletondrejmirtes
authored andcommitted
Array map on multiple elements is a list
1 parent 09eab21 commit 2016780

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/Type/Php/ArrayMapFunctionReturnTypeExtension.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
168168
);
169169
}
170170
} else {
171-
$mappedArrayType = TypeCombinator::intersect(new ArrayType(
172-
new IntegerType(),
173-
$valueType,
174-
), ...TypeUtils::getAccessoryTypes($arrayType));
171+
$mappedArrayType = AccessoryArrayListType::intersectWith(
172+
TypeCombinator::intersect(new ArrayType(
173+
new IntegerType(),
174+
$valueType,
175+
), ...TypeUtils::getAccessoryTypes($arrayType)),
176+
);
175177
}
176178

177179
if ($arrayType->isIterableAtLeastOnce()->yes()) {

tests/PHPStan/Analyser/nsrt/array_map_multiple.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function doFoo(int $i, string $s): void
1515

1616
return rand(0, 1) ? $a : $b;
1717
}, ['foo' => $i], ['bar' => $s]);
18-
assertType('non-empty-array<int, int|string>', $result);
18+
assertType('non-empty-list<int|string>', $result);
1919
}
2020

2121
/**
@@ -26,12 +26,12 @@ public function arrayMapNull(array $array, array $other): void
2626
{
2727
assertType('array{}', array_map(null, []));
2828
assertType('array{foo: true}', array_map(null, ['foo' => true]));
29-
assertType('non-empty-array<int, array{1|2|3, 4|5|6}>', array_map(null, [1, 2, 3], [4, 5, 6]));
29+
assertType('non-empty-list<array{1|2|3, 4|5|6}>', array_map(null, [1, 2, 3], [4, 5, 6]));
3030

3131
assertType('non-empty-array<string, int>', array_map(null, $array));
32-
assertType('non-empty-array<int, array{int, int}>', array_map(null, $array, $array));
33-
assertType('non-empty-array<int, array{int, int, int}>', array_map(null, $array, $array, $array));
34-
assertType('non-empty-array<int, array{int|null, bool|null}>', array_map(null, $array, $other));
32+
assertType('non-empty-list<array{int, int}>', array_map(null, $array, $array));
33+
assertType('non-empty-list<array{int, int, int}>', array_map(null, $array, $array, $array));
34+
assertType('non-empty-list<array{int|null, bool|null}>', array_map(null, $array, $other));
3535

3636
assertType('array{1}|array{true}', array_map(null, rand() ? [1] : [true]));
3737
assertType('array{1}|array{true, false}', array_map(null, rand() ? [1] : [true, false]));

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,4 +1064,9 @@ public function testBug11857(): void
10641064
$this->analyse([__DIR__ . '/data/bug-11857-builder.php'], []);
10651065
}
10661066

1067+
public function testBug12223(): void
1068+
{
1069+
$this->analyse([__DIR__ . '/data/bug-12223.php'], []);
1070+
}
1071+
10671072
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug12223;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @return list<string>
9+
*/
10+
public function sayHello(): array
11+
{
12+
$a = [1 => 'foo', 3 => 'bar', 5 => 'baz'];
13+
return array_map(static fn(string $s, int $i): string => $s . $i, $a, array_keys($a));
14+
}
15+
}

0 commit comments

Comments
 (0)