Skip to content

Commit 107a7e3

Browse files
committed
Support for non-empty-array and non-empty-list array shape kind
1 parent 856208a commit 107a7e3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/PhpDoc/TypeNodeResolver.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,20 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
10161016
}
10171017

10181018
$arrayType = $builder->getArray();
1019-
if ($typeNode->kind === ArrayShapeNode::KIND_LIST) {
1019+
if (in_array($typeNode->kind, [
1020+
ArrayShapeNode::KIND_LIST,
1021+
ArrayShapeNode::KIND_NON_EMPTY_LIST,
1022+
], true)) {
10201023
$arrayType = AccessoryArrayListType::intersectWith($arrayType);
10211024
}
10221025

1026+
if (in_array($typeNode->kind, [
1027+
ArrayShapeNode::KIND_NON_EMPTY_ARRAY,
1028+
ArrayShapeNode::KIND_NON_EMPTY_LIST,
1029+
], true)) {
1030+
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
1031+
}
1032+
10231033
return $arrayType;
10241034
}
10251035

tests/PHPStan/Analyser/nsrt/array-shape-list-optional.php

+6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ class Foo
99

1010
/**
1111
* @param list{0: string, 1: int, 2?: string, 3?: string} $valid1
12+
* @param non-empty-list{0: string, 1: int, 2?: string, 3?: string} $valid2
13+
* @param non-empty-array{0?: string, 1?: int, 2?: string, 3?: string} $valid3
1214
* @param list{0: string, 1: int, 2?: string, 4?: string} $invalid1
1315
* @param list{0: string, 1: int, 2?: string, foo?: string} $invalid2
1416
*/
1517
public function doFoo(
1618
$valid1,
19+
$valid2,
20+
$valid3,
1721
$invalid1,
1822
$invalid2
1923
): void
2024
{
2125
assertType('array{0: string, 1: int, 2?: string, 3?: string}&list', $valid1);
26+
assertType('array{0: string, 1: int, 2?: string, 3?: string}&list', $valid2);
27+
assertType('array{0?: string, 1?: int, 2?: string, 3?: string}&non-empty-array', $valid3);
2228
assertType('*NEVER*', $invalid1);
2329
assertType('*NEVER*', $invalid2);
2430
}

0 commit comments

Comments
 (0)