Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassReflection: resolve missing template type to its default rather than bound #3623

Merged
merged 1 commit into from
Nov 11, 2024
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
2 changes: 1 addition & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ public function getActiveTemplateTypeMap(): TemplateTypeMap
if ($type instanceof ErrorType) {
$templateType = $templateTypeMap->getType($name);
if ($templateType !== null) {
return TemplateTypeHelper::resolveToBounds($templateType);
return TemplateTypeHelper::resolveToDefaults($templateType);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/Type/Generic/TemplateTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public static function resolveTemplateTypes(
});
}

public static function resolveToDefaults(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof TemplateType) {
return $traverse($type->getDefault() ?? $type->getBound());
}

return $traverse($type);
});
}

public static function resolveToBounds(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/GenericTypeVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function getType(
return new MixedType(false);
}

return $bound;
return TemplateTypeHelper::resolveToDefaults($templateType);
}

return $type;
Expand Down
2 changes: 1 addition & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ public function getTemplateType(string $ancestorClassName, string $templateTypeN
return new MixedType(false);
}

return $bound;
return TemplateTypeHelper::resolveToDefaults($templateType);
}

return $type;
Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11899.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bug11899;

use function PHPStan\Testing\assertType;

abstract class Test {}

interface InvertedQuestions {}

class SomeTest extends Test implements InvertedQuestions {}

/**
* @template TTestType = Test
* @property-read ?TTestType $test
*/
class UserTest {
public function __get() : mixed {
return new SomeTest();
}
}

function acceptUserTest1(UserTest $ut) : void {
assertType('Bug11899\\UserTest', $ut);
assertType('Bug11899\\Test|null', $ut->test);
}

/**
* @param UserTest<InvertedQuestions> $ut
*/
function acceptUserTest2(UserTest $ut) : void {
assertType('Bug11899\\UserTest<Bug11899\\InvertedQuestions>', $ut);
assertType('Bug11899\\InvertedQuestions|null', $ut->test);
}
Loading