Skip to content

Commit 412a809

Browse files
committed
Add support for trait string types
1 parent 265919f commit 412a809

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/PseudoTypes/TraitString.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,33 @@
2424
*/
2525
final class TraitString extends String_ implements PseudoType
2626
{
27+
/** @var Type|null */
28+
private $genericType;
29+
30+
public function __construct(?Type $genericType = null)
31+
{
32+
$this->genericType = $genericType;
33+
}
34+
2735
public function underlyingType(): Type
2836
{
2937
return new String_();
3038
}
3139

40+
public function getGenericType(): ?Type
41+
{
42+
return $this->genericType;
43+
}
44+
3245
/**
3346
* Returns a rendered output of the Type as it would be used in a DocBlock.
3447
*/
3548
public function __toString(): string
3649
{
37-
return 'trait-string';
50+
if ($this->genericType === null) {
51+
return 'trait-string';
52+
}
53+
54+
return 'trait-string<' . (string) $this->genericType . '>';
3855
}
3956
}

src/TypeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
406406
case 'interface-string':
407407
return new InterfaceString($this->createType($type->genericTypes[0], $context));
408408

409+
case 'trait-string':
410+
return new TraitString($this->createType($type->genericTypes[0], $context));
411+
409412
case 'list':
410413
return new List_(
411414
$this->createType($type->genericTypes[0], $context)

tests/unit/TypeResolverTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,23 @@ public function genericsProvider(): array
11151115
])
11161116
),
11171117
],
1118+
[
1119+
'trait-string',
1120+
new TraitString(),
1121+
],
1122+
[
1123+
'trait-string<Foo>',
1124+
new TraitString(new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
1125+
],
1126+
[
1127+
'trait-string<Foo|Bar>',
1128+
new TraitString(
1129+
new Compound([
1130+
new Object_(new Fqsen('\\phpDocumentor\\Foo')),
1131+
new Object_(new Fqsen('\\phpDocumentor\\Bar')),
1132+
])
1133+
),
1134+
],
11181135
[
11191136
'List<Foo>',
11201137
new List_(new Object_(new Fqsen('\\phpDocumentor\\Foo'))),

0 commit comments

Comments
 (0)