Skip to content

Commit

Permalink
Support for non-empty-array and non-empty-list array shape kind
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 13, 2024
1 parent 856208a commit 107a7e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1016,10 +1016,20 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
}

$arrayType = $builder->getArray();
if ($typeNode->kind === ArrayShapeNode::KIND_LIST) {
if (in_array($typeNode->kind, [
ArrayShapeNode::KIND_LIST,
ArrayShapeNode::KIND_NON_EMPTY_LIST,
], true)) {
$arrayType = AccessoryArrayListType::intersectWith($arrayType);
}

if (in_array($typeNode->kind, [
ArrayShapeNode::KIND_NON_EMPTY_ARRAY,
ArrayShapeNode::KIND_NON_EMPTY_LIST,
], true)) {
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
}

return $arrayType;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-shape-list-optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ class Foo

/**
* @param list{0: string, 1: int, 2?: string, 3?: string} $valid1
* @param non-empty-list{0: string, 1: int, 2?: string, 3?: string} $valid2
* @param non-empty-array{0?: string, 1?: int, 2?: string, 3?: string} $valid3
* @param list{0: string, 1: int, 2?: string, 4?: string} $invalid1
* @param list{0: string, 1: int, 2?: string, foo?: string} $invalid2
*/
public function doFoo(
$valid1,
$valid2,
$valid3,
$invalid1,
$invalid2
): void
{
assertType('array{0: string, 1: int, 2?: string, 3?: string}&list', $valid1);
assertType('array{0: string, 1: int, 2?: string, 3?: string}&list', $valid2);
assertType('array{0?: string, 1?: int, 2?: string, 3?: string}&non-empty-array', $valid3);
assertType('*NEVER*', $invalid1);
assertType('*NEVER*', $invalid2);
}
Expand Down

0 comments on commit 107a7e3

Please sign in to comment.