From 107a7e38e2173d8b8b4f18e5d592fc8ead02b96a Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 13 Oct 2024 13:35:47 +0200 Subject: [PATCH] Support for non-empty-array and non-empty-list array shape kind --- src/PhpDoc/TypeNodeResolver.php | 12 +++++++++++- .../Analyser/nsrt/array-shape-list-optional.php | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 6e483222d7b..43f93cb8f52 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -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; } diff --git a/tests/PHPStan/Analyser/nsrt/array-shape-list-optional.php b/tests/PHPStan/Analyser/nsrt/array-shape-list-optional.php index 0eaa4471d29..f059d14c4df 100644 --- a/tests/PHPStan/Analyser/nsrt/array-shape-list-optional.php +++ b/tests/PHPStan/Analyser/nsrt/array-shape-list-optional.php @@ -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); }