Skip to content

Commit 7026403

Browse files
authored
Merge pull request #252 from mspirkov/add-enum-string
Add support for `enum-string`
2 parents 1d3f372 + 3d9411b commit 7026403

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/PseudoTypes/EnumString.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\String_;
19+
20+
/**
21+
* Value Object representing the type 'enum-string'.
22+
*
23+
* @psalm-immutable
24+
*/
25+
final class EnumString extends String_ implements PseudoType
26+
{
27+
/** @var Type|null */
28+
private $genericType;
29+
30+
public function __construct(?Type $genericType = null)
31+
{
32+
$this->genericType = $genericType;
33+
}
34+
35+
public function underlyingType(): Type
36+
{
37+
return new String_();
38+
}
39+
40+
public function getGenericType(): ?Type
41+
{
42+
return $this->genericType;
43+
}
44+
45+
/**
46+
* Returns a rendered output of the Type as it would be used in a DocBlock.
47+
*/
48+
public function __toString(): string
49+
{
50+
if ($this->genericType === null) {
51+
return 'enum-string';
52+
}
53+
54+
return 'enum-string<' . (string) $this->genericType . '>';
55+
}
56+
}

src/TypeResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use phpDocumentor\Reflection\PseudoTypes\Conditional;
2424
use phpDocumentor\Reflection\PseudoTypes\ConditionalForParameter;
2525
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
26+
use phpDocumentor\Reflection\PseudoTypes\EnumString;
2627
use phpDocumentor\Reflection\PseudoTypes\False_;
2728
use phpDocumentor\Reflection\PseudoTypes\FloatValue;
2829
use phpDocumentor\Reflection\PseudoTypes\Generic;
@@ -141,6 +142,7 @@ final class TypeResolver
141142
'numeric-string' => NumericString::class,
142143
'numeric' => Numeric_::class,
143144
'trait-string' => TraitString::class,
145+
'enum-string' => EnumString::class,
144146
'int' => Integer::class,
145147
'integer' => Integer::class,
146148
'positive-int' => PositiveInteger::class,
@@ -409,6 +411,9 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
409411
case 'trait-string':
410412
return new TraitString($this->createType($type->genericTypes[0], $context));
411413

414+
case 'enum-string':
415+
return new EnumString($this->createType($type->genericTypes[0], $context));
416+
412417
case 'list':
413418
return new List_(
414419
$this->createType($type->genericTypes[0], $context)

tests/unit/TypeResolverTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use phpDocumentor\Reflection\PseudoTypes\Conditional;
2424
use phpDocumentor\Reflection\PseudoTypes\ConditionalForParameter;
2525
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
26+
use phpDocumentor\Reflection\PseudoTypes\EnumString;
2627
use phpDocumentor\Reflection\PseudoTypes\False_;
2728
use phpDocumentor\Reflection\PseudoTypes\FloatValue;
2829
use phpDocumentor\Reflection\PseudoTypes\Generic;
@@ -1132,6 +1133,23 @@ public function genericsProvider(): array
11321133
])
11331134
),
11341135
],
1136+
[
1137+
'enum-string',
1138+
new EnumString(),
1139+
],
1140+
[
1141+
'enum-string<MyEnum>',
1142+
new EnumString(new Object_(new Fqsen('\\phpDocumentor\\MyEnum'))),
1143+
],
1144+
[
1145+
'enum-string<MyEnumFirst|MyEnumSecond>',
1146+
new EnumString(
1147+
new Compound([
1148+
new Object_(new Fqsen('\\phpDocumentor\\MyEnumFirst')),
1149+
new Object_(new Fqsen('\\phpDocumentor\\MyEnumSecond')),
1150+
])
1151+
),
1152+
],
11351153
[
11361154
'List<Foo>',
11371155
new List_(new Object_(new Fqsen('\\phpDocumentor\\Foo'))),

0 commit comments

Comments
 (0)