From 052599192ab53ea522b9d9deb50c7dba2a47a912 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 25 May 2019 12:44:47 -0400 Subject: [PATCH] Fix #1680 - narrow nullable templated matching --- src/Psalm/Type/Union.php | 6 ++++++ tests/Template/TemplateTest.php | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index 2c5aff139..fb9740d59 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -1009,10 +1009,16 @@ class Union if ($input_type) { $generic_param = clone $input_type; + + if ($this->isNullable() && $generic_param->isNullable()) { + $generic_param->removeType('null'); + } + $generic_param->setFromDocblock(); if (isset($generic_params[$key][$atomic_type->defining_class ?: ''][0])) { $existing_depth = $generic_params[$key][$atomic_type->defining_class ?: ''][1] ?? -1; + if ($existing_depth > $depth) { continue; } diff --git a/tests/Template/TemplateTest.php b/tests/Template/TemplateTest.php index f6405e6c6..158604ec6 100644 --- a/tests/Template/TemplateTest.php +++ b/tests/Template/TemplateTest.php @@ -2572,6 +2572,28 @@ class TemplateTest extends TestCase return get(B::class, $mapB, $id); }' ], + 'dontGeneraliseBoundParamWithWiderCallable' => [ + ' 'C', + ] + ], ]; }