From 9af8a9ac384d38fe69dbc839d1505aff8d4af460 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 27 Sep 2019 12:58:32 -0400 Subject: [PATCH] Fix #2181 - prevent class template types being wiped by anonymous class use --- .../Internal/Visitor/ReflectorVisitor.php | 4 +++- tests/Template/ClassTemplateTest.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Visitor/ReflectorVisitor.php b/src/Psalm/Internal/Visitor/ReflectorVisitor.php index 91f1cd8a8..7a57cd48a 100644 --- a/src/Psalm/Internal/Visitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/Visitor/ReflectorVisitor.php @@ -632,7 +632,9 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse $this->file_storage->has_docblock_issues = true; } - $this->class_template_types = []; + if ($node->name) { + $this->class_template_types = []; + } if ($this->after_classlike_check_plugins) { $file_manipulations = []; diff --git a/tests/Template/ClassTemplateTest.php b/tests/Template/ClassTemplateTest.php index d47ae796b..dc772f298 100644 --- a/tests/Template/ClassTemplateTest.php +++ b/tests/Template/ClassTemplateTest.php @@ -1703,6 +1703,25 @@ class ClassTemplateTest extends TestCase } }', ], + 'anonymousClassMustNotBreakParentTemplate' => [ + 'value = $val; + new class extends Foo {}; + } + + /** @psalm-return ?T */ + public function get() { + return $this->value; + } + }' + ], ]; }