Skip to content

Commit a2c0403

Browse files
orklahondrejmirtes
authored andcommitted
list and list<foo> support
1 parent 92df236 commit a2c0403

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
191191
return new NeverType(true);
192192

193193
case 'list':
194-
return new ErrorType();
194+
return new ArrayType(new IntegerType(), new MixedType());
195195
}
196196

197197
if ($nameScope->getClassName() !== null) {
@@ -313,6 +313,12 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na
313313
return new ArrayType($genericTypes[0], $genericTypes[1]);
314314
}
315315

316+
} elseif ($mainTypeName === 'list') {
317+
if (count($genericTypes) === 1) { // list<ValueType>
318+
return new ArrayType(new IntegerType(), $genericTypes[0]);
319+
}
320+
321+
return new ErrorType();
316322
} elseif ($mainTypeName === 'iterable') {
317323
if (count($genericTypes) === 1) { // iterable<ValueType>
318324
return new IterableType(new MixedType(true), $genericTypes[0]);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9737,6 +9737,11 @@ public function dataPhpDocInheritanceParameterRemapping(): array
97379737
return $this->gatherAssertTypes(__DIR__ . '/data/inheritdoc-parameter-remapping.php');
97389738
}
97399739

9740+
public function dataListType(): array
9741+
{
9742+
return $this->gatherAssertTypes(__DIR__ . '/data/list-type.php');
9743+
}
9744+
97409745
/**
97419746
* @dataProvider dataBug2574
97429747
* @dataProvider dataBug2577
@@ -9757,6 +9762,7 @@ public function dataPhpDocInheritanceParameterRemapping(): array
97579762
* @dataProvider dataBug2648
97589763
* @dataProvider dataBug2740
97599764
* @dataProvider dataPhpDocInheritanceParameterRemapping
9765+
* @dataProvider dataListType
97609766
* @param ConstantStringType $expectedType
97619767
* @param Type $actualType
97629768
*/
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace ListType;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
/** @param list $list */
10+
public function directAssertion($list): void
11+
{
12+
assertType('array<int, mixed>', $list);
13+
}
14+
15+
/** @param list $list */
16+
public function directAssertionParamHint(array $list): void
17+
{
18+
assertType('array<int, mixed>', $list);
19+
}
20+
21+
/** @param list $list */
22+
public function directAssertionNullableParamHint(array $list = null): void
23+
{
24+
assertType('array<int, mixed>|null', $list);
25+
}
26+
27+
/** @param list<\DateTime> $list */
28+
public function directAssertionObjectParamHint($list): void
29+
{
30+
assertType('array<int, DateTime>', $list);
31+
}
32+
33+
public function withoutGenerics(): void
34+
{
35+
/** @var list $list */
36+
$list = [];
37+
$list[] = '1';
38+
$list[] = true;
39+
$list[] = new \stdClass();
40+
assertType('array<int, mixed>', $list);
41+
}
42+
43+
44+
public function withMixedType(): void
45+
{
46+
/** @var list<mixed> $list */
47+
$list = [];
48+
$list[] = '1';
49+
$list[] = true;
50+
$list[] = new \stdClass();
51+
assertType('array<int, mixed>', $list);
52+
}
53+
54+
public function withObjectType(): void
55+
{
56+
/** @var list<\DateTime> $list */
57+
$list = [];
58+
$list[] = new \DateTime();
59+
assertType('array<int, DateTime>', $list);
60+
}
61+
62+
/** @return list<scalar> */
63+
public function withScalarGoodContent(): void
64+
{
65+
/** @var list<scalar> $list */
66+
$list = [];
67+
$list[] = '1';
68+
$list[] = true;
69+
assertType('array<int, bool|float|int|string>', $list);
70+
}
71+
72+
public function withNumericKey(): void
73+
{
74+
/** @var list $list */
75+
$list = [];
76+
$list[] = '1';
77+
$list['1'] = true;
78+
assertType('array<int, mixed>', $list);
79+
}
80+
81+
public function withFullListFunctionality(): void
82+
{
83+
// These won't output errors for now but should when list type will be fully implemented
84+
/** @var list $list */
85+
$list = [];
86+
$list[] = '1';
87+
$list[] = '2';
88+
unset($list[0]);//break list behaviour
89+
assertType('array<int, mixed>', $list);
90+
91+
/** @var list $list2 */
92+
$list2 = [];
93+
$list2[2] = '1';//Most likely to create a gap in indexes
94+
assertType('array<int, mixed>', $list2);
95+
}
96+
97+
}

0 commit comments

Comments
 (0)