Skip to content

Commit

Permalink
Fix template default type issue
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Nov 30, 2019
1 parent b1e8c38 commit 088228f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Psalm/Internal/Visitor/ReflectorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,7 @@ private function useTemplatedType(
private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_method = false)
{
$class_storage = null;
$fq_classlike_name = null;

if ($fake_method && $stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
$cased_function_id = '@method ' . $stmt->name->name;
Expand Down Expand Up @@ -1803,7 +1804,7 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m
continue;
}

$param_array = $this->getTranslatedFunctionParam($param, $stmt, $fake_method);
$param_array = $this->getTranslatedFunctionParam($param, $stmt, $fake_method, $fq_classlike_name);

if (isset($existing_params['$' . $param_array->name])) {
if (IssueBuffer::accepts(
Expand Down Expand Up @@ -2691,7 +2692,8 @@ private function getAssertionParts(
public function getTranslatedFunctionParam(
PhpParser\Node\Param $param,
PhpParser\Node\FunctionLike $stmt,
bool $fake_method
bool $fake_method,
?string $fq_classlike_name
) : FunctionLikeParameter {
$param_type = null;

Expand Down Expand Up @@ -2783,7 +2785,10 @@ public function getTranslatedFunctionParam(
$this->codebase,
new \Psalm\Internal\Provider\NodeDataProvider(),
$param->default,
$this->aliases
$this->aliases,
null,
null,
$fq_classlike_name
)
: null
);
Expand Down
26 changes: 25 additions & 1 deletion tests/Template/ClassTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ function __construct(string $t = FOO) {
'$e===' => 'E<string(bar)>',
],
],
'SKIPPED-templateDefaultClassConstant' => [
'SKIPPED-templateDefaultClassMemberConstant' => [
'<?php
class D {
const FOO = "bar";
Expand All @@ -968,6 +968,30 @@ function __construct(string $t = D::FOO) {
'$e===' => 'E<string(bar)>',
],
],
'templateDefaultClassConstant' => [
'<?php
class D {}
/**
* @template T as object
*/
class E {
/** @var class-string<T> */
public $t;
/**
* @param class-string<T> $t
*/
function __construct(string $t = D::class) {
$this->t = $t;
}
}
$e = new E();',
'assertions' => [
'$e===' => 'E<D>',
],
],
'allowNullablePropertyAssignment' => [
'<?php
/**
Expand Down

0 comments on commit 088228f

Please sign in to comment.