From f40b6625ebc2525901211a1f5fa86616849e00b4 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 13 Dec 2019 21:40:09 -0500 Subject: [PATCH] Fix #2431 - allow null in template defaults --- .../Analyzer/FunctionLikeAnalyzer.php | 4 ++-- src/Psalm/Internal/Analyzer/TypeAnalyzer.php | 2 +- .../Internal/Visitor/ReflectorVisitor.php | 5 ++++- tests/Template/FunctionTemplateTest.php | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index ab7c5f968..47192abcf 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -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 ) )) { diff --git a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php index f8036e7fb..b1f7b1678 100644 --- a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php @@ -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; } diff --git a/src/Psalm/Internal/Visitor/ReflectorVisitor.php b/src/Psalm/Internal/Visitor/ReflectorVisitor.php index b63f7d44d..89024f259 100644 --- a/src/Psalm/Internal/Visitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/Visitor/ReflectorVisitor.php @@ -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()); } diff --git a/tests/Template/FunctionTemplateTest.php b/tests/Template/FunctionTemplateTest.php index 9a7424367..9e861edc1 100644 --- a/tests/Template/FunctionTemplateTest.php +++ b/tests/Template/FunctionTemplateTest.php @@ -856,6 +856,28 @@ class FunctionTemplateTest extends TestCase } }' ], + 'falseDefault' => [ + ' [ + '