From 0eed186edfff7fae4d5e3b6849b77efdb2308a9d Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Mon, 1 Feb 2021 21:40:30 -0500 Subject: [PATCH] Fix #5140 - always replace closure return types, whether or not a matching input type is present --- src/Psalm/Type/Atomic/CallableTrait.php | 9 ++- tests/Template/ClassTemplateExtendsTest.php | 61 +++++++++++++-------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/Psalm/Type/Atomic/CallableTrait.php b/src/Psalm/Type/Atomic/CallableTrait.php index 649b18c5b..ed579b063 100644 --- a/src/Psalm/Type/Atomic/CallableTrait.php +++ b/src/Psalm/Type/Atomic/CallableTrait.php @@ -222,16 +222,15 @@ trait CallableTrait } } - if (($input_type instanceof Atomic\TCallable || $input_type instanceof Atomic\TClosure) - && $callable->return_type - && $input_type->return_type - ) { + if ($callable->return_type) { $callable->return_type = TemplateStandinTypeReplacer::replace( $callable->return_type, $template_result, $codebase, $statements_analyzer, - $input_type->return_type, + $input_type instanceof Atomic\TCallable || $input_type instanceof Atomic\TClosure + ? $input_type->return_type + : null, $input_arg_offset, $calling_class, $calling_function, diff --git a/tests/Template/ClassTemplateExtendsTest.php b/tests/Template/ClassTemplateExtendsTest.php index 1fa7d90fe..325289874 100644 --- a/tests/Template/ClassTemplateExtendsTest.php +++ b/tests/Template/ClassTemplateExtendsTest.php @@ -4282,29 +4282,6 @@ class ClassTemplateExtendsTest extends TestCase function getIterator(): Traversable; }' ], - 'extendsWithoutAlias' => [ - ' - */ - abstract class B extends A { - /** - * @psalm-param TBValue $val - */ - abstract public function foo($val): void; - }' - ], 'extendsWithAlias' => [ ' [ + ' + */ + class B extends A { + /** + * @return T2|null + */ + public function getType() { + return $this->type; + } + + /** + * @return (Closure(): T2)|null + */ + public function getClosureReturningType() { + return $this->closure; + } + }' + ], ]; }