Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/PseudoTypes/EnumString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\String_;

/**
* Value Object representing the type 'enum-string'.
*
* @psalm-immutable
*/
final class EnumString extends String_ implements PseudoType
{
/** @var Type|null */
private $genericType;

public function __construct(?Type $genericType = null)
{
$this->genericType = $genericType;
}

public function underlyingType(): Type
{
return new String_();
}

public function getGenericType(): ?Type
{
return $this->genericType;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString(): string
{
if ($this->genericType === null) {
return 'enum-string';
}

return 'enum-string<' . (string) $this->genericType . '>';
}
}
5 changes: 5 additions & 0 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use phpDocumentor\Reflection\PseudoTypes\Conditional;
use phpDocumentor\Reflection\PseudoTypes\ConditionalForParameter;
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
use phpDocumentor\Reflection\PseudoTypes\EnumString;
use phpDocumentor\Reflection\PseudoTypes\False_;
use phpDocumentor\Reflection\PseudoTypes\FloatValue;
use phpDocumentor\Reflection\PseudoTypes\Generic;
Expand Down Expand Up @@ -141,6 +142,7 @@ final class TypeResolver
'numeric-string' => NumericString::class,
'numeric' => Numeric_::class,
'trait-string' => TraitString::class,
'enum-string' => EnumString::class,
'int' => Integer::class,
'integer' => Integer::class,
'positive-int' => PositiveInteger::class,
Expand Down Expand Up @@ -409,6 +411,9 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
case 'trait-string':
return new TraitString($this->createType($type->genericTypes[0], $context));

case 'enum-string':
return new EnumString($this->createType($type->genericTypes[0], $context));

case 'list':
return new List_(
$this->createType($type->genericTypes[0], $context)
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use phpDocumentor\Reflection\PseudoTypes\Conditional;
use phpDocumentor\Reflection\PseudoTypes\ConditionalForParameter;
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
use phpDocumentor\Reflection\PseudoTypes\EnumString;
use phpDocumentor\Reflection\PseudoTypes\False_;
use phpDocumentor\Reflection\PseudoTypes\FloatValue;
use phpDocumentor\Reflection\PseudoTypes\Generic;
Expand Down Expand Up @@ -1132,6 +1133,23 @@ public function genericsProvider(): array
])
),
],
[
'enum-string',
new EnumString(),
],
[
'enum-string<MyEnum>',
new EnumString(new Object_(new Fqsen('\\phpDocumentor\\MyEnum'))),
],
[
'enum-string<MyEnumFirst|MyEnumSecond>',
new EnumString(
new Compound([
new Object_(new Fqsen('\\phpDocumentor\\MyEnumFirst')),
new Object_(new Fqsen('\\phpDocumentor\\MyEnumSecond')),
])
),
],
[
'List<Foo>',
new List_(new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
Expand Down