1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add support for literal templated defaults

This commit is contained in:
Matthew Brown 2019-03-07 22:32:38 -05:00
parent 94b1161a80
commit 85f0fa6e7d
2 changed files with 72 additions and 5 deletions

View File

@ -1135,6 +1135,27 @@ class CallAnalyzer
break;
}
if ($param->is_optional
&& $param->type
&& $param->default_type
&& !$param->is_variadic
&& $template_types
) {
if ($generic_params === null) {
$generic_params = [];
}
$param_type = clone $param->type;
$param_type->replaceTemplateTypesWithStandins(
$template_types,
$generic_params,
$codebase,
clone $param->default_type,
true
);
}
}
}
}

View File

@ -1711,7 +1711,7 @@ class TemplateTest extends TestCase
*/
function bar(Foo $a) : void {}'
],
'SKIPPED-templateDefault' => [
'templateDefaultSimpleString' => [
'<?php
/**
* @template T as string
@ -1728,15 +1728,61 @@ class TemplateTest extends TestCase
}
}
$a = new C("hello");
$b = new C("goodbye");
$c = new C();',
'assertions' => [
'$a===' => 'C<string(hello)>',
'$b===' => 'C<string(goodbye)>',
'$c===' => 'C<string(hello)>',
],
],
'SKIPPED-templateDefaultConstant' => [
'<?php
const FOO = "bar";
/**
* @template T as string
*/
class E {
/** @var T */
public $t;
/**
* @param T $t
*/
function __construct(string $t = FOO) {
$this->t = $t;
}
}
$e = new E();',
'assertions' => [
'$e===' => 'E<string(bar)>',
],
],
'SKIPPED-templateDefaultClassConstant' => [
'<?php
class D {
const FOO = "bar";
}
/**
* @template T as string
*/
class E {
/** @var T */
public $t;
/**
* @param T $t
*/
function __construct(string $t = D::FOO) {
$this->t = $t;
}
}
$e = new E();',
'assertions' => [
'$e===' => 'E<string(bar)>',
],
],
];
}