Skip to content

Commit 2815ccf

Browse files
committed
feat: Support for @template default types
1 parent bf28eaf commit 2815ccf

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

src/NativePHPDoc/Definition/NativePHPDoc/NativePHPDocDefinitionProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,13 @@ function () use ($node, $value, $temporaryContext) {
315315
Str::endsWith($node->name, '-covariant') => TemplateTypeVariance::COVARIANT,
316316
Str::endsWith($node->name, '-contravariant') => TemplateTypeVariance::CONTRAVARIANT,
317317
default => TemplateTypeVariance::INVARIANT
318-
}
318+
},
319+
default: $value->default ?
320+
$this->phpDocTypeMapper->map(
321+
$value->default,
322+
$temporaryContext->value(),
323+
) :
324+
null,
319325
);
320326
}
321327
);

src/NativePHPDoc/Definition/TypeDefinition/TypeParameterDefinition.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct(
1616
public readonly bool $variadic,
1717
public readonly ?Type $upperBound,
1818
public readonly TemplateTypeVariance $variance,
19+
public readonly ?Type $default = null,
1920
) {}
2021

2122
public function __toString(): string
@@ -28,6 +29,7 @@ public function __toString(): string
2829
},
2930
($this->variadic ? '...' : '') . $this->name,
3031
$this->upperBound ? "of {$this->upperBound}" : '',
32+
$this->default ? "= {$this->default}" : '',
3133
];
3234

3335
return implode(' ', array_filter($result));

src/Type/Template/TypeParameterMap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
*/
2424
public static function fromArguments(array $arguments, iterable $typeParameters): self
2525
{
26-
if (!$arguments) {
26+
if (!$typeParameters) {
2727
return self::empty();
2828
}
2929

@@ -37,8 +37,10 @@ public static function fromArguments(array $arguments, iterable $typeParameters)
3737
break;
3838
}
3939

40-
if (!$argument = $arguments[$i] ?? null) {
41-
break;
40+
$argument = $arguments[$i] ?? $parameter->default;
41+
42+
if (!$argument) {
43+
continue;
4244
}
4345

4446
$map[$parameter->name] = $argument;

tests/Integration/Reflection/ReflectionSnapshots/Tests.Stubs.Classes.DoubleTemplateType.yaml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type: Tests\Stubs\Classes\DoubleTemplateType
1+
type: Tests\Stubs\Classes\DoubleTemplateType<int>
22
qualifiedName: Tests\Stubs\Classes\DoubleTemplateType
33
shortName: DoubleTemplateType
44
location: Tests\Stubs\Classes\DoubleTemplateType
@@ -22,13 +22,35 @@ typeParameters:
2222
variadic: false
2323
upperBound: mixed
2424
variance: INVARIANT
25-
asString: TTwo
25+
asString: 'TTwo = int'
2626
extends: null
2727
implements: { }
2828
uses:
2929
traits: { }
3030
excludedTraitMethods: { }
3131
declaredProperties: { }
3232
properties: { }
33-
declaredMethods: { }
34-
methods: { }
33+
declaredMethods:
34+
-
35+
asString: something()
36+
name: something
37+
location: 'Tests\Stubs\Classes\DoubleTemplateType::something()'
38+
declaringType: Tests\Stubs\Classes\DoubleTemplateType<int>
39+
attributes:
40+
asString: '#[]'
41+
all: { }
42+
typeParameters: { }
43+
parameters: { }
44+
returnType: int
45+
methods:
46+
-
47+
asString: something()
48+
name: something
49+
location: 'Tests\Stubs\Classes\DoubleTemplateType::something()'
50+
declaringType: Tests\Stubs\Classes\DoubleTemplateType<int>
51+
attributes:
52+
asString: '#[]'
53+
all: { }
54+
typeParameters: { }
55+
parameters: { }
56+
returnType: int

tests/Stubs/Classes/DoubleTemplateType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
/**
66
* @template TOne
7-
* @template TTwo
7+
* @template TTwo = int
88
*/
99
class DoubleTemplateType
1010
{
11+
/**
12+
* @return TTwo
13+
*/
14+
public function something(): mixed {}
1115
}

tests/Unit/Type/Definition/TypeDefinition/TypeParameterDefinitionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GoodPhp\Reflection\NativePHPDoc\Definition\TypeDefinition\TypeParameterDefinition;
66
use GoodPhp\Reflection\Type\PrimitiveType;
7+
use GoodPhp\Reflection\Type\Special\MixedType;
78
use GoodPhp\Reflection\Type\Template\TemplateTypeVariance;
89
use PHPUnit\Framework\Attributes\DataProvider;
910
use PHPUnit\Framework\TestCase;
@@ -93,5 +94,16 @@ public static function toStringProvider(): iterable
9394
variance: TemplateTypeVariance::COVARIANT,
9495
),
9596
];
97+
98+
yield [
99+
'covariant T of mixed = int',
100+
new TypeParameterDefinition(
101+
name: 'T',
102+
variadic: false,
103+
upperBound: MixedType::get(),
104+
variance: TemplateTypeVariance::COVARIANT,
105+
default: PrimitiveType::integer(),
106+
),
107+
];
96108
}
97109
}

0 commit comments

Comments
 (0)