Skip to content

Commit

Permalink
List shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe authored Feb 10, 2023
1 parent e6ba509 commit 0e2e1b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,12 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
$builder->setOffsetValueType($offsetType, $this->resolve($itemNode->valueType, $nameScope), $itemNode->optional);
}

return $builder->getArray();
$arrayType = $builder->getArray();
if ($typeNode->kind === ArrayShapeNode::KIND_LIST) {
$arrayType = AccessoryArrayListType::intersectWith($arrayType);
}

return $arrayType;
}

private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameScope): Type
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8775.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8752.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/trait-instance-of.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/list-shapes.php');
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/list-shapes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace ListShapes;

use function PHPStan\Testing\assertType;

class Foo
{
/**
* @param list{} $l1
* @param list{'a'} $l2
* @param list{0: 'a'} $l3
* @param list{0: 'a', 1: 'b'} $l4
* @param list{0: 'a', 1?: 'b'} $l5
* @param list{'a', 'b', ...} $l6
*/
public function bar($l1, $l2, $l3, $l4, $l5, $l6): void
{
// TODO: list{}
assertType('array{}', $l1);
assertType("array{'a'}", $l2);
assertType("array{'a'}", $l3);
assertType("array{'a', 'b'}", $l4);
assertType("array{0: 'a', 1?: 'b'}", $l5);
assertType("array{'a', 'b'}", $l6);
}
}

0 comments on commit 0e2e1b8

Please sign in to comment.