diff --git a/src/PseudoTypes/NonEmptyArray.php b/src/PseudoTypes/NonEmptyArray.php new file mode 100644 index 0000000..f0cc4a6 --- /dev/null +++ b/src/PseudoTypes/NonEmptyArray.php @@ -0,0 +1,48 @@ +valueType, $this->keyType); + } + + /** + * Returns a rendered output of the Type as it would be used in a DocBlock. + */ + public function __toString(): string + { + if ($this->keyType) { + return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>'; + } + + if ($this->valueType instanceof Mixed_) { + return 'non-empty-array'; + } + + return 'non-empty-array<' . $this->valueType . '>'; + } +} diff --git a/src/PseudoTypes/NonEmptyList.php b/src/PseudoTypes/NonEmptyList.php index dd6a653..384b629 100644 --- a/src/PseudoTypes/NonEmptyList.php +++ b/src/PseudoTypes/NonEmptyList.php @@ -28,7 +28,7 @@ final class NonEmptyList extends Array_ implements PseudoType { public function underlyingType(): Type { - return new Array_(); + return new Array_($this->valueType, $this->keyType); } public function __construct(?Type $valueType = null) diff --git a/src/TypeResolver.php b/src/TypeResolver.php index 0c558c9..ede83ae 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -28,6 +28,7 @@ use phpDocumentor\Reflection\PseudoTypes\LiteralString; use phpDocumentor\Reflection\PseudoTypes\LowercaseString; use phpDocumentor\Reflection\PseudoTypes\NegativeInteger; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyArray; use phpDocumentor\Reflection\PseudoTypes\NonEmptyList; use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString; use phpDocumentor\Reflection\PseudoTypes\NonEmptyString; @@ -139,6 +140,7 @@ final class TypeResolver 'mixed' => Mixed_::class, 'array' => Array_::class, 'array-key' => ArrayKey::class, + 'non-empty-array' => NonEmptyArray::class, 'resource' => Resource_::class, 'void' => Void_::class, 'null' => Null_::class, diff --git a/tests/unit/PseudoTypes/NonEmptyArrayTest.php b/tests/unit/PseudoTypes/NonEmptyArrayTest.php new file mode 100644 index 0000000..5d7e508 --- /dev/null +++ b/tests/unit/PseudoTypes/NonEmptyArrayTest.php @@ -0,0 +1,55 @@ +assertSame($expectedString, (string) $array); + } + + /** + * @return mixed[] + */ + public function provideArrays(): array + { + return [ + 'simple non-empty-array' => [new NonEmptyArray(), 'non-empty-array'], + 'non-empty-array of mixed' => [new NonEmptyArray(new Mixed_()), 'non-empty-array'], + 'non-empty-array of single type' => [new NonEmptyArray(new String_()), 'non-empty-array'], + 'non-empty-array of compound type' => + [ + new NonEmptyArray( + new Compound([new Integer(), new String_()]), + new String_() + ), + 'non-empty-array', + ], + ]; + } +} diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index eac321d..926b27e 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -26,6 +26,7 @@ use phpDocumentor\Reflection\PseudoTypes\LiteralString; use phpDocumentor\Reflection\PseudoTypes\LowercaseString; use phpDocumentor\Reflection\PseudoTypes\NegativeInteger; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyArray; use phpDocumentor\Reflection\PseudoTypes\NonEmptyList; use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString; use phpDocumentor\Reflection\PseudoTypes\NonEmptyString; @@ -789,6 +790,7 @@ public function provideKeywords(): array ['literal-string', LiteralString::class], ['list', List_::class], ['non-empty-list', NonEmptyList::class], + ['non-empty-array', NonEmptyArray::class], ]; }