Skip to content

Commit b3c99fb

Browse files
staabmondrejmirtes
authored andcommitted
Fix wrongly convertion of list<T> to array{T}
1 parent 3592cf0 commit b3c99fb

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Analyser/TypeSpecifier.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,8 @@ private function turnListIntoConstantArray(FuncCall $countFuncCall, Type $type,
10591059
}
10601060
$valueTypesBuilder->setOffsetValueType($offsetType, $type->getOffsetValueType($offsetType), !$hasOffset->yes());
10611061
}
1062-
1062+
} else {
1063+
return null;
10631064
}
10641065

10651066
$arrayType = $valueTypesBuilder->getArray();
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11642;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Repository
8+
{
9+
/**
10+
* @psalm-param array<string|int, mixed> $criteria
11+
*
12+
* @return object[] The objects.
13+
* @psalm-return list<\stdClass>
14+
*/
15+
function findBy(array $criteria): array
16+
{
17+
return [new \stdClass, new \stdCLass, new \stdClass, new \stdClass];
18+
}
19+
}
20+
21+
class Payload {
22+
/** @var non-empty-list<string> */
23+
public array $ids = ['one', 'two'];
24+
}
25+
26+
function doFoo() {
27+
$payload = new Payload();
28+
29+
$fetcher = new Repository();
30+
$entries = $fetcher->findBy($payload->ids);
31+
assertType('list<stdClass>', $entries);
32+
assertType('int<0, max>', count($entries));
33+
assertType('int<1, max>', count($payload->ids));
34+
if (count($entries) !== count($payload->ids)) {
35+
exit();
36+
}
37+
38+
assertType('non-empty-list<stdClass>', $entries);
39+
if (count($entries) > 3) {
40+
throw new \RuntimeException();
41+
}
42+
43+
assertType('non-empty-list<stdClass>', $entries);
44+
}

0 commit comments

Comments
 (0)