Skip to content

Commit

Permalink
Merge pull request #206 from phpDocumentor/feature/non-empty-array
Browse files Browse the repository at this point in the history
Add support for non-empty-array notation
  • Loading branch information
jaapio authored Mar 22, 2024
2 parents 153ae66 + 6f50ece commit f237fbd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/PseudoTypes/NonEmptyArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Mixed_;

/**
* Value Object representing the type 'non-empty-array'.
*
* @psalm-immutable
*/
final class NonEmptyArray extends Array_ implements PseudoType
{
public function underlyingType(): Type
{
return new Array_($this->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 . '>';
}
}
2 changes: 1 addition & 1 deletion src/PseudoTypes/NonEmptyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/PseudoTypes/NonEmptyArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\NonEmptyArray
*/
class NonEmptyArrayTest extends TestCase
{
/**
* @dataProvider provideArrays
* @covers ::__toString
*/
public function testArrayStringifyCorrectly(NonEmptyArray $array, string $expectedString): void
{
$this->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<string>'],
'non-empty-array of compound type' =>
[
new NonEmptyArray(
new Compound([new Integer(), new String_()]),
new String_()
),
'non-empty-array<string,int|string>',
],
];
}
}
2 changes: 2 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
];
}

Expand Down

0 comments on commit f237fbd

Please sign in to comment.