From 99da1daae56846acd8c795a8db82163b2ba06ba5 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Sat, 9 Jan 2021 18:58:29 -0500 Subject: [PATCH] Fix #4958 - honour template as inheritance when comparing template types --- .../Type/Comparator/ObjectComparator.php | 17 ++++++++++---- tests/Template/FunctionTemplateTest.php | 23 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Type/Comparator/ObjectComparator.php b/src/Psalm/Internal/Type/Comparator/ObjectComparator.php index 7065f9f3d..230304d4c 100644 --- a/src/Psalm/Internal/Type/Comparator/ObjectComparator.php +++ b/src/Psalm/Internal/Type/Comparator/ObjectComparator.php @@ -73,15 +73,24 @@ class ObjectComparator continue; } - if (\substr($intersection_container_type->defining_class, 0, 3) === 'fn-') { - foreach ($intersection_input_types as $intersection_input_type) { - if ($intersection_input_type instanceof TTemplateParam - && \substr($intersection_input_type->defining_class, 0, 3) === 'fn-' + foreach ($intersection_input_types as $intersection_input_type) { + if ($intersection_input_type instanceof TTemplateParam + && (\substr($intersection_container_type->defining_class, 0, 3) === 'fn-' + || \substr($intersection_input_type->defining_class, 0, 3) === 'fn-') + ) { + if (\substr($intersection_input_type->defining_class, 0, 3) === 'fn-' + && \substr($intersection_container_type->defining_class, 0, 3) === 'fn-' && $intersection_input_type->defining_class !== $intersection_container_type->defining_class ) { continue 2; } + + foreach ($intersection_input_type->as->getAtomicTypes() as $input_as_atomic) { + if ($input_as_atomic->equals($intersection_container_type)) { + continue 3; + } + } } } diff --git a/tests/Template/FunctionTemplateTest.php b/tests/Template/FunctionTemplateTest.php index 2ac16fb8f..e3cf8c9c3 100644 --- a/tests/Template/FunctionTemplateTest.php +++ b/tests/Template/FunctionTemplateTest.php @@ -1485,6 +1485,29 @@ class FunctionTemplateTest extends TestCase [], '8.0' ], + 'templateChildClass' => [ + 'add($default); + + return $default; + } + }' + ], ]; }