Skip to content

Commit 0e2e1b8

Browse files
authored
List shapes
1 parent e6ba509 commit 0e2e1b8

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,12 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
823823
$builder->setOffsetValueType($offsetType, $this->resolve($itemNode->valueType, $nameScope), $itemNode->optional);
824824
}
825825

826-
return $builder->getArray();
826+
$arrayType = $builder->getArray();
827+
if ($typeNode->kind === ArrayShapeNode::KIND_LIST) {
828+
$arrayType = AccessoryArrayListType::intersectWith($arrayType);
829+
}
830+
831+
return $arrayType;
827832
}
828833

829834
private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameScope): Type

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ public function dataFileAsserts(): iterable
11801180
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8775.php');
11811181
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8752.php');
11821182
yield from $this->gatherAssertTypes(__DIR__ . '/data/trait-instance-of.php');
1183+
yield from $this->gatherAssertTypes(__DIR__ . '/data/list-shapes.php');
11831184
}
11841185

11851186
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace ListShapes;
3+
4+
use function PHPStan\Testing\assertType;
5+
6+
class Foo
7+
{
8+
/**
9+
* @param list{} $l1
10+
* @param list{'a'} $l2
11+
* @param list{0: 'a'} $l3
12+
* @param list{0: 'a', 1: 'b'} $l4
13+
* @param list{0: 'a', 1?: 'b'} $l5
14+
* @param list{'a', 'b', ...} $l6
15+
*/
16+
public function bar($l1, $l2, $l3, $l4, $l5, $l6): void
17+
{
18+
// TODO: list{}
19+
assertType('array{}', $l1);
20+
assertType("array{'a'}", $l2);
21+
assertType("array{'a'}", $l3);
22+
assertType("array{'a', 'b'}", $l4);
23+
assertType("array{0: 'a', 1?: 'b'}", $l5);
24+
assertType("array{'a', 'b'}", $l6);
25+
}
26+
}

0 commit comments

Comments
 (0)