1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #3150 - only inherit docblock return when same type is returned

This commit is contained in:
Brown 2020-04-20 09:22:58 -04:00
parent a3ae2a713b
commit 6b42efed3f
3 changed files with 34 additions and 1 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -1109,6 +1109,26 @@ class AnnotationTest extends TestCase
return false;
}'
],
'dontInheritDocblockReturnWhenRedeclared' => [
'<?php
interface Id {}
class UserId implements Id {}
interface Entity {
/** @psalm-return Id */
function id(): Id;
}
class User implements Entity {
public function id(): UserId {
return new UserId();
}
}',
[],
[],
'7.4'
],
];
}