Skip to content

Commit 6f50ece

Browse files
committed
Add support for non-empty-array notation
1 parent 153ae66 commit 6f50ece

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

src/PseudoTypes/NonEmptyArray.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\PseudoType;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\Array_;
19+
use phpDocumentor\Reflection\Types\Mixed_;
20+
21+
/**
22+
* Value Object representing the type 'non-empty-array'.
23+
*
24+
* @psalm-immutable
25+
*/
26+
final class NonEmptyArray extends Array_ implements PseudoType
27+
{
28+
public function underlyingType(): Type
29+
{
30+
return new Array_($this->valueType, $this->keyType);
31+
}
32+
33+
/**
34+
* Returns a rendered output of the Type as it would be used in a DocBlock.
35+
*/
36+
public function __toString(): string
37+
{
38+
if ($this->keyType) {
39+
return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>';
40+
}
41+
42+
if ($this->valueType instanceof Mixed_) {
43+
return 'non-empty-array';
44+
}
45+
46+
return 'non-empty-array<' . $this->valueType . '>';
47+
}
48+
}

src/PseudoTypes/NonEmptyList.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class NonEmptyList extends Array_ implements PseudoType
2828
{
2929
public function underlyingType(): Type
3030
{
31-
return new Array_();
31+
return new Array_($this->valueType, $this->keyType);
3232
}
3333

3434
public function __construct(?Type $valueType = null)

src/TypeResolver.php

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use phpDocumentor\Reflection\PseudoTypes\LiteralString;
2929
use phpDocumentor\Reflection\PseudoTypes\LowercaseString;
3030
use phpDocumentor\Reflection\PseudoTypes\NegativeInteger;
31+
use phpDocumentor\Reflection\PseudoTypes\NonEmptyArray;
3132
use phpDocumentor\Reflection\PseudoTypes\NonEmptyList;
3233
use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString;
3334
use phpDocumentor\Reflection\PseudoTypes\NonEmptyString;
@@ -139,6 +140,7 @@ final class TypeResolver
139140
'mixed' => Mixed_::class,
140141
'array' => Array_::class,
141142
'array-key' => ArrayKey::class,
143+
'non-empty-array' => NonEmptyArray::class,
142144
'resource' => Resource_::class,
143145
'void' => Void_::class,
144146
'null' => Null_::class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\Types\Compound;
17+
use phpDocumentor\Reflection\Types\Integer;
18+
use phpDocumentor\Reflection\Types\Mixed_;
19+
use phpDocumentor\Reflection\Types\String_;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @coversDefaultClass \phpDocumentor\Reflection\PseudoTypes\NonEmptyArray
24+
*/
25+
class NonEmptyArrayTest extends TestCase
26+
{
27+
/**
28+
* @dataProvider provideArrays
29+
* @covers ::__toString
30+
*/
31+
public function testArrayStringifyCorrectly(NonEmptyArray $array, string $expectedString): void
32+
{
33+
$this->assertSame($expectedString, (string) $array);
34+
}
35+
36+
/**
37+
* @return mixed[]
38+
*/
39+
public function provideArrays(): array
40+
{
41+
return [
42+
'simple non-empty-array' => [new NonEmptyArray(), 'non-empty-array'],
43+
'non-empty-array of mixed' => [new NonEmptyArray(new Mixed_()), 'non-empty-array'],
44+
'non-empty-array of single type' => [new NonEmptyArray(new String_()), 'non-empty-array<string>'],
45+
'non-empty-array of compound type' =>
46+
[
47+
new NonEmptyArray(
48+
new Compound([new Integer(), new String_()]),
49+
new String_()
50+
),
51+
'non-empty-array<string,int|string>',
52+
],
53+
];
54+
}
55+
}

tests/unit/TypeResolverTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use phpDocumentor\Reflection\PseudoTypes\LiteralString;
2727
use phpDocumentor\Reflection\PseudoTypes\LowercaseString;
2828
use phpDocumentor\Reflection\PseudoTypes\NegativeInteger;
29+
use phpDocumentor\Reflection\PseudoTypes\NonEmptyArray;
2930
use phpDocumentor\Reflection\PseudoTypes\NonEmptyList;
3031
use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString;
3132
use phpDocumentor\Reflection\PseudoTypes\NonEmptyString;
@@ -789,6 +790,7 @@ public function provideKeywords(): array
789790
['literal-string', LiteralString::class],
790791
['list', List_::class],
791792
['non-empty-list', NonEmptyList::class],
793+
['non-empty-array', NonEmptyArray::class],
792794
];
793795
}
794796

0 commit comments

Comments
 (0)