Skip to content

Commit

Permalink
Add support for non-empty-array notation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio committed Mar 22, 2024
1 parent 153ae66 commit 39c7973
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/PseudoTypes/NonEmptyArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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_;

final class NonEmptyArray extends Array_ implements PseudoType

Check failure on line 21 in src/PseudoTypes/NonEmptyArray.php

View workflow job for this annotation

GitHub Actions / Static analysis / Static Code Analysis (8.0)

MissingImmutableAnnotation

src/PseudoTypes/NonEmptyArray.php:21:13: MissingImmutableAnnotation: phpDocumentor\Reflection\Type is marked @psalm-immutable, but phpDocumentor\Reflection\PseudoTypes\NonEmptyArray is not marked @psalm-immutable (see https://psalm.dev/213)

Check failure on line 21 in src/PseudoTypes/NonEmptyArray.php

View workflow job for this annotation

GitHub Actions / Static analysis / Static Code Analysis (8.0)

MissingImmutableAnnotation

src/PseudoTypes/NonEmptyArray.php:21:35: MissingImmutableAnnotation: phpDocumentor\Reflection\Types\Array_ is marked @psalm-immutable, but phpDocumentor\Reflection\PseudoTypes\NonEmptyArray is not marked @psalm-immutable (see https://psalm.dev/213)
{
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

Check failure on line 31 in src/PseudoTypes/NonEmptyArray.php

View workflow job for this annotation

GitHub Actions / Static analysis / Static Code Analysis (8.0)

MissingImmutableAnnotation

src/PseudoTypes/NonEmptyArray.php:31:5: MissingImmutableAnnotation: phpDocumentor\Reflection\Types\AbstractList::__toString is marked @psalm-immutable, but phpDocumentor\Reflection\PseudoTypes\NonEmptyArray::__toString is not marked @psalm-immutable (see https://psalm.dev/213)
{
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
49 changes: 49 additions & 0 deletions tests/unit/PseudoTypes/NonEmptyArrayTest.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\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 39c7973

Please sign in to comment.