diff --git a/src/Psalm/Internal/Codebase/Populator.php b/src/Psalm/Internal/Codebase/Populator.php index 838c69788..6a08420ee 100644 --- a/src/Psalm/Internal/Codebase/Populator.php +++ b/src/Psalm/Internal/Codebase/Populator.php @@ -351,7 +351,12 @@ class Populator && $declaring_method_storage->return_type !== $declaring_method_storage->signature_return_type ) { - if ($declaring_method_storage->signature_return_type) { + if ($declaring_method_storage->signature_return_type + && TypeAnalyzer::isSimplyContainedBy( + $method_storage->signature_return_type, + $declaring_method_storage->signature_return_type + ) + ) { $method_storage->return_type = $declaring_method_storage->return_type; $method_storage->inherited_return_type = true; } elseif (TypeAnalyzer::isSimplyContainedBy( @@ -857,6 +862,10 @@ class Populator && $interface_method_storage->signature_return_type && $interface_method_storage->return_type !== $interface_method_storage->signature_return_type + && TypeAnalyzer::isSimplyContainedBy( + $interface_method_storage->signature_return_type, + $method_storage->signature_return_type + ) ) { $method_storage->return_type = $interface_method_storage->return_type; $method_storage->inherited_return_type = true; diff --git a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php index 0e5dcb5a7..d3a7c51eb 100644 --- a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php @@ -2004,6 +2004,10 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse $this->aliases ); + if ($class_storage && !$class_storage->is_trait && $return_type_fq_classlike_name === 'self') { + $return_type_fq_classlike_name = $class_storage->name; + } + $return_type_string = $return_type_fq_classlike_name . $suffix; } diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index 955c2bd0b..2d86e3eda 100644 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -1109,6 +1109,26 @@ class AnnotationTest extends TestCase return false; }' ], + 'dontInheritDocblockReturnWhenRedeclared' => [ + '