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
11 changes: 10 additions & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
Expand Down Expand Up @@ -1736,8 +1737,16 @@ public function typeMapFromList(array $types): TemplateTypeMap

$map = [];
$i = 0;
$className = $this->getName();
foreach ($resolvedPhpDoc->getTemplateTags() as $tag) {
$map[$tag->getName()] = $types[$i] ?? $tag->getDefault() ?? $tag->getBound();
$type = $types[$i] ?? $tag->getDefault() ?? $tag->getBound();
if ($type instanceof TemplateType && $type->getScope()->getClassName() === $className) {
$resolved = $map[$type->getName()] ?? null;
if ($resolved !== null && !$resolved instanceof TemplateType) {
$type = $resolved;
}
}
$map[$tag->getName()] = $type;
$i++;
}

Expand Down
117 changes: 117 additions & 0 deletions tests/PHPStan/Analyser/nsrt/template-default-referring-other.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php // lint >= 8.1

namespace TemplateDefaultReferringOther;

use function PHPStan\Testing\assertType;

class MoneyValue
{

public function __construct(
public readonly string $currency,
public readonly int $cents,
)
{
}

}

/**
* @template-contravariant DI
* @template-contravariant EI
* @template-covariant DO of EI = EI
* @template-covariant EO of DI = DI
*/
interface Codec
{

/**
* @param DI $data
* @return DO
*/
public function decode(mixed $data): mixed;

/**
* @param EI $data
* @return EO
*/
public function encode(mixed $data): mixed;

}

/**
* @implements Codec<
* array{currency: string, cents: int},
* MoneyValue,
* >
*/
class MoneyCodec implements Codec
{

public function decode(mixed $data): MoneyValue
{
return new MoneyValue($data['currency'], $data['cents']);
}

public function encode(mixed $data): array
{
return [
'currency' => $data->currency,
'cents' => $data->cents,
];
}

}

/**
* @implements Codec<
* string,
* \DateTimeInterface,
* \DateTimeImmutable,
* string,
* >
*/
class DateTimeInterfaceCodec implements Codec
{

public function decode(mixed $data): \DateTimeImmutable
{
return new \DateTimeImmutable($data);
}

public function encode(mixed $data): string
{
return $data->format('c');
}

}

/**
* @param Codec<array{currency: string, cents: int}, MoneyValue> $moneyCodec
* @param Codec<string, \DateTimeInterface, \DateTimeImmutable, string> $dtCodec
*/
function test(
Codec $moneyCodec,
Codec $dtCodec,
string $dtString,
\DateTimeInterface $dtInterface,
): void
{
assertType('TemplateDefaultReferringOther\MoneyValue', $moneyCodec->decode(['currency' => 'CZK', 'cents' => 123]));
assertType('array{currency: string, cents: int}', $moneyCodec->encode(new MoneyValue('CZK', 100)));

assertType('DateTimeImmutable', $dtCodec->decode($dtString));
assertType('string', $dtCodec->encode($dtInterface));
}

function testMoneyCodecDirect(MoneyCodec $codec): void
{
assertType('TemplateDefaultReferringOther\MoneyValue', $codec->decode(['currency' => 'CZK', 'cents' => 123]));
assertType('array{currency: string, cents: int}', $codec->encode(new MoneyValue('CZK', 100)));
}

function testDateTimeCodecDirect(DateTimeInterfaceCodec $codec): void
{
assertType('DateTimeImmutable', $codec->decode('2024-01-01'));
assertType('string', $codec->encode(new \DateTimeImmutable()));
}
Loading