mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Fix #2431 - allow null in template defaults
This commit is contained in:
parent
e1af0e01ee
commit
f40b6625eb
@ -1041,9 +1041,9 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
||||
) {
|
||||
if (IssueBuffer::accepts(
|
||||
new InvalidParamDefault(
|
||||
'Default value type ' . $default_type . ' for argument ' . ($offset + 1)
|
||||
'Default value type ' . $default_type->getId() . ' for argument ' . ($offset + 1)
|
||||
. ' of method ' . $cased_method_id
|
||||
. ' does not match the given type ' . $param_type,
|
||||
. ' does not match the given type ' . $param_type->getId(),
|
||||
$function_param->type_location
|
||||
)
|
||||
)) {
|
||||
|
@ -733,7 +733,7 @@ class TypeAnalyzer
|
||||
}
|
||||
|
||||
if ($container_type_part instanceof TTemplateParam
|
||||
&& $container_type_part->as->isNullable()
|
||||
&& ($container_type_part->as->isNullable() || $container_type_part->as->isMixed())
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
@ -2953,7 +2953,10 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
|
||||
$existing_param_type_nullable = $storage_param->is_nullable;
|
||||
|
||||
if (!$storage_param->type || $storage_param->type->hasMixed() || $storage->template_types) {
|
||||
if ($existing_param_type_nullable && !$new_param_type->isNullable()) {
|
||||
if ($existing_param_type_nullable
|
||||
&& !$new_param_type->isNullable()
|
||||
&& !$new_param_type->hasTemplate()
|
||||
) {
|
||||
$new_param_type->addType(new Type\Atomic\TNull());
|
||||
}
|
||||
|
||||
|
@ -856,6 +856,28 @@ class FunctionTemplateTest extends TestCase
|
||||
}
|
||||
}'
|
||||
],
|
||||
'falseDefault' => [
|
||||
'<?php
|
||||
/**
|
||||
* @template T
|
||||
* @param T $v
|
||||
* @return T
|
||||
*/
|
||||
function exampleWithNullDefault($v = false) {
|
||||
return $v;
|
||||
}'
|
||||
],
|
||||
'nullDefault' => [
|
||||
'<?php
|
||||
/**
|
||||
* @template T
|
||||
* @param T $v
|
||||
* @return T
|
||||
*/
|
||||
function exampleWithNullDefault($v = null) {
|
||||
return $v;
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user