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
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ function () use ($node, $value, $temporaryContext) {
Str::endsWith($node->name, '-covariant') => TemplateTypeVariance::COVARIANT,
Str::endsWith($node->name, '-contravariant') => TemplateTypeVariance::CONTRAVARIANT,
default => TemplateTypeVariance::INVARIANT
}
},
default: $value->default ?
$this->phpDocTypeMapper->map(
$value->default,
$temporaryContext->value(),
) :
null,
);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
public readonly bool $variadic,
public readonly ?Type $upperBound,
public readonly TemplateTypeVariance $variance,
public readonly ?Type $default = null,
) {}

public function __toString(): string
Expand All @@ -28,6 +29,7 @@ public function __toString(): string
},
($this->variadic ? '...' : '') . $this->name,
$this->upperBound ? "of {$this->upperBound}" : '',
$this->default ? "= {$this->default}" : '',
];

return implode(' ', array_filter($result));
Expand Down
8 changes: 5 additions & 3 deletions src/Type/Template/TypeParameterMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
*/
public static function fromArguments(array $arguments, iterable $typeParameters): self
{
if (!$arguments) {
if (!$typeParameters) {
return self::empty();
}

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

if (!$argument = $arguments[$i] ?? null) {
break;
$argument = $arguments[$i] ?? $parameter->default;

if (!$argument) {
continue;
}

$map[$parameter->name] = $argument;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type: Tests\Stubs\Classes\DoubleTemplateType
type: Tests\Stubs\Classes\DoubleTemplateType<int>
qualifiedName: Tests\Stubs\Classes\DoubleTemplateType
shortName: DoubleTemplateType
location: Tests\Stubs\Classes\DoubleTemplateType
Expand All @@ -22,13 +22,35 @@ typeParameters:
variadic: false
upperBound: mixed
variance: INVARIANT
asString: TTwo
asString: 'TTwo = int'
extends: null
implements: { }
uses:
traits: { }
excludedTraitMethods: { }
declaredProperties: { }
properties: { }
declaredMethods: { }
methods: { }
declaredMethods:
-
asString: something()
name: something
location: 'Tests\Stubs\Classes\DoubleTemplateType::something()'
declaringType: Tests\Stubs\Classes\DoubleTemplateType<int>
attributes:
asString: '#[]'
all: { }
typeParameters: { }
parameters: { }
returnType: int
methods:
-
asString: something()
name: something
location: 'Tests\Stubs\Classes\DoubleTemplateType::something()'
declaringType: Tests\Stubs\Classes\DoubleTemplateType<int>
attributes:
asString: '#[]'
all: { }
typeParameters: { }
parameters: { }
returnType: int
6 changes: 5 additions & 1 deletion tests/Stubs/Classes/DoubleTemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

/**
* @template TOne
* @template TTwo
* @template TTwo = int
*/
class DoubleTemplateType
{
/**
* @return TTwo
*/
public function something(): mixed {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GoodPhp\Reflection\NativePHPDoc\Definition\TypeDefinition\TypeParameterDefinition;
use GoodPhp\Reflection\Type\PrimitiveType;
use GoodPhp\Reflection\Type\Special\MixedType;
use GoodPhp\Reflection\Type\Template\TemplateTypeVariance;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -93,5 +94,16 @@ public static function toStringProvider(): iterable
variance: TemplateTypeVariance::COVARIANT,
),
];

yield [
'covariant T of mixed = int',
new TypeParameterDefinition(
name: 'T',
variadic: false,
upperBound: MixedType::get(),
variance: TemplateTypeVariance::COVARIANT,
default: PrimitiveType::integer(),
),
];
}
}