From 18931295c4f2025350403169c1c261692dea2c88 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Thu, 13 May 2021 16:55:22 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20#5073=20=E2=80=94=20don=E2=80=99t=20crash?= =?UTF-8?q?=20when=20templates=20cannot=20be=20resolved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Type/TemplateStandinTypeReplacer.php | 2 +- tests/Template/ClassTemplateTest.php | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php index 91a3e8acf..a01b5cf66 100644 --- a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php @@ -88,7 +88,7 @@ class TemplateStandinTypeReplacer } if (!$atomic_types) { - throw new \UnexpectedValueException('Cannot remove all keys'); + return $union_type; } if (count($atomic_types) > 1) { diff --git a/tests/Template/ClassTemplateTest.php b/tests/Template/ClassTemplateTest.php index 2c1d1596a..2a04b8b49 100644 --- a/tests/Template/ClassTemplateTest.php +++ b/tests/Template/ClassTemplateTest.php @@ -3370,7 +3370,7 @@ class ClassTemplateTest extends TestCase $wm[$ex] = 42; } ', - ] + ], ]; } @@ -4010,6 +4010,29 @@ class ClassTemplateTest extends TestCase takesA($child);', 'error_message' => 'InvalidArgument', ], + 'noCrashTemplatedClosure' => [ + 'callback = $callback; + } + + /** @param TCallback $callback */ + public function setCallback(Closure $callback): void { + $this->callback = $callback; + } + } + $a = new A(function() { return "a";}); + $a->setCallback(function() { return "b";});', + 'error_message' => 'InvalidScalarArgument', + ], ]; } }