Skip to content
Permalink
Browse files

Fix #2431 - allow null in template defaults

  • Loading branch information
muglug committed Dec 14, 2019
1 parent e1af0e0 commit f40b6625ebc2525901211a1f5fa86616849e00b4
@@ -1041,9 +1041,9 @@ private function processParams(
) {
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 @@ public static function isAtomicContainedBy(
}

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 @@ private function improveParamsFromDocblock(
$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 @@ function example($value): void {
}
}'
],
'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;
}'
],
];
}

0 comments on commit f40b662

Please sign in to comment.
You can’t perform that action at this time.