diff --git a/src/PseudoTypes/NonEmptyList.php b/src/PseudoTypes/NonEmptyList.php new file mode 100644 index 0000000..dd6a653 --- /dev/null +++ b/src/PseudoTypes/NonEmptyList.php @@ -0,0 +1,50 @@ +valueType instanceof Mixed_) { + return 'non-empty-list'; + } + + return 'non-empty-list<' . $this->valueType . '>'; + } +} diff --git a/src/TypeResolver.php b/src/TypeResolver.php index d15822d..5cbbe44 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -27,6 +27,7 @@ use phpDocumentor\Reflection\PseudoTypes\LiteralString; use phpDocumentor\Reflection\PseudoTypes\LowercaseString; use phpDocumentor\Reflection\PseudoTypes\NegativeInteger; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyList; use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString; use phpDocumentor\Reflection\PseudoTypes\NonEmptyString; use phpDocumentor\Reflection\PseudoTypes\Numeric_; @@ -157,6 +158,7 @@ final class TypeResolver 'iterable' => Iterable_::class, 'never' => Never_::class, 'list' => List_::class, + 'non-empty-list' => NonEmptyList::class, ]; /** @psalm-readonly */ @@ -333,6 +335,11 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ $this->createType($type->genericTypes[0], $context) ); + case 'non-empty-list': + return new NonEmptyList( + $this->createType($type->genericTypes[0], $context) + ); + case 'int': if (isset($type->genericTypes[1]) === false) { throw new RuntimeException('int has not the correct format'); diff --git a/tests/unit/CollectionResolverTest.php b/tests/unit/CollectionResolverTest.php index 3468d94..7f480cd 100644 --- a/tests/unit/CollectionResolverTest.php +++ b/tests/unit/CollectionResolverTest.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Reflection; use phpDocumentor\Reflection\PseudoTypes\List_; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyList; use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Collection; use phpDocumentor\Reflection\Types\Compound; @@ -321,6 +322,30 @@ public function testResolvingList(): void $this->assertInstanceOf(Integer::class, $keyType); } + /** + * @uses \phpDocumentor\Reflection\Types\Context + * @uses \phpDocumentor\Reflection\Types\String_ + * + * @covers ::__construct + * @covers ::resolve + */ + public function testResolvingNonEmptyList(): void + { + $fixture = new TypeResolver(); + + $resolvedType = $fixture->resolve('non-empty-list', new Context('')); + + $this->assertInstanceOf(NonEmptyList::class, $resolvedType); + $this->assertSame('non-empty-list', (string) $resolvedType); + + $valueType = $resolvedType->getValueType(); + + $keyType = $resolvedType->getKeyType(); + + $this->assertInstanceOf(String_::class, $valueType); + $this->assertInstanceOf(Integer::class, $keyType); + } + /** * @uses \phpDocumentor\Reflection\Types\Context * @uses \phpDocumentor\Reflection\Types\Nullable diff --git a/tests/unit/PseudoTypes/NonEmptyListTest.php b/tests/unit/PseudoTypes/NonEmptyListTest.php new file mode 100644 index 0000000..a576f95 --- /dev/null +++ b/tests/unit/PseudoTypes/NonEmptyListTest.php @@ -0,0 +1,49 @@ +assertSame($expectedString, (string) $array); + } + + /** + * @return mixed[] + */ + public function provideArrays(): array + { + return [ + 'simple non-empty-list' => [new NonEmptyList(), 'non-empty-list'], + 'non-empty-list of mixed' => [new NonEmptyList(new Mixed_()), 'non-empty-list'], + 'non-empty-list of single type' => [new NonEmptyList(new String_()), 'non-empty-list'], + 'non-empty-list of compound type' => + [new NonEmptyList(new Compound([new Integer(), new String_()])), 'non-empty-list'], + ]; + } +} diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index b669f46..b3618e4 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -25,6 +25,7 @@ use phpDocumentor\Reflection\PseudoTypes\LiteralString; use phpDocumentor\Reflection\PseudoTypes\LowercaseString; use phpDocumentor\Reflection\PseudoTypes\NegativeInteger; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyList; use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString; use phpDocumentor\Reflection\PseudoTypes\NonEmptyString; use phpDocumentor\Reflection\PseudoTypes\Numeric_; @@ -784,6 +785,7 @@ public function provideKeywords(): array ['never', Never_::class], ['literal-string', LiteralString::class], ['list', List_::class], + ['non-empty-list', NonEmptyList::class], ]; }