Skip to content

Commit

Permalink
Merge pull request #183 from schodemeiss/1.x
Browse files Browse the repository at this point in the history
Add non-empty-list pseudo-type
  • Loading branch information
jaapio authored Mar 12, 2023
2 parents 1478a93 + c5521c9 commit ac4f96a
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/PseudoTypes/NonEmptyList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\PseudoTypes;

use phpDocumentor\Reflection\PseudoType;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_;

/**
* Value Object representing the type 'non-empty-list'.
*
* @psalm-immutable
*/
final class NonEmptyList extends Array_ implements PseudoType
{
public function underlyingType(): Type
{
return new Array_();
}

public function __construct(?Type $valueType = null)
{
parent::__construct($valueType, new Integer());
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString(): string
{
if ($this->valueType instanceof Mixed_) {
return 'non-empty-list';
}

return 'non-empty-list<' . $this->valueType . '>';
}
}
7 changes: 7 additions & 0 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -157,6 +158,7 @@ final class TypeResolver
'iterable' => Iterable_::class,
'never' => Never_::class,
'list' => List_::class,
'non-empty-list' => NonEmptyList::class,
];

/** @psalm-readonly */
Expand Down Expand Up @@ -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<min,max> has not the correct format');
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/CollectionResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string>', new Context(''));

$this->assertInstanceOf(NonEmptyList::class, $resolvedType);
$this->assertSame('non-empty-list<string>', (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
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/PseudoTypes/NonEmptyListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\PseudoTypes;

use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\String_;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \phpDocumentor\Reflection\PseudoTypes\NonEmptyList
*/
class NonEmptyListTest extends TestCase
{
/**
* @dataProvider provideArrays
* @covers ::__toString
*/
public function testArrayStringifyCorrectly(NonEmptyList $array, string $expectedString): void
{
$this->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<string>'],
'non-empty-list of compound type' =>
[new NonEmptyList(new Compound([new Integer(), new String_()])), 'non-empty-list<int|string>'],
];
}
}
2 changes: 2 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -784,6 +785,7 @@ public function provideKeywords(): array
['never', Never_::class],
['literal-string', LiteralString::class],
['list', List_::class],
['non-empty-list', NonEmptyList::class],
];
}

Expand Down

0 comments on commit ac4f96a

Please sign in to comment.