Skip to content
Permalink
Browse files

Fix #1593 - still inherit more specific docblock from parent when par…

…ent has no signature return
  • Loading branch information...
muglug committed May 2, 2019
1 parent dcfe9bf commit 0fe378a46772ec83debf1acc711197fcdd4eea53
@@ -211,6 +211,38 @@ public static function isContainedByInPhp(
return false;
}
/**
* Used for comparing docblock types to signature types before we know about all types
*
* @param Type\Union $input_type
* @param Type\Union $container_type
*
* @return bool
*/
public static function isSimplyContainedBy(
Type\Union $input_type,
Type\Union $container_type
) {
if ($input_type->getId() === $container_type->getId()) {
return true;
}
if ($input_type->isNullable() && !$container_type->isNullable()) {
return false;
}
$input_type_not_null = clone $input_type;
$input_type_not_null->removeType('null');
$container_type_not_null = clone $container_type;
$container_type_not_null->removeType('null');
return (bool) array_intersect_key(
$input_type_not_null->getTypes(),
$container_type_not_null->getTypes()
);
}
/**
* Does the input param type match the given param type
*
@@ -2,6 +2,7 @@
namespace Psalm\Internal\Codebase;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\TypeAnalyzer;
use Psalm\Config;
use Psalm\Issue\CircularReference;
use Psalm\IssueBuffer;
@@ -316,11 +317,17 @@ private function populateOverriddenMethods(
$declaring_method_storage = $declaring_class_storage->methods[$method_name];
if ($declaring_method_storage->return_type
&& $declaring_method_storage->signature_return_type
&& $declaring_method_storage->return_type
!== $declaring_method_storage->signature_return_type
) {
$method_storage->return_type = $declaring_method_storage->return_type;
if ($declaring_method_storage->signature_return_type) {
$method_storage->return_type = $declaring_method_storage->return_type;
} elseif (TypeAnalyzer::isSimplyContainedBy(
$declaring_method_storage->return_type,
$method_storage->signature_return_type
)) {
$method_storage->return_type = $declaring_method_storage->return_type;
}
}
}
}
@@ -945,6 +945,28 @@ function bar(array $arr) : void {
foo($arr);
}',
],
'inheritParentReturnDocbblock' => [
'<?php
class Foo {
/**
* @return int[]
*/
public function doFoo() {
return [1, 2, 3];
}
}
class Bar extends Foo {
public function doFoo(): array {
return [4, 5, 6];
}
}
$b = (new Bar)->doFoo();',
[
'$b' => 'array<array-key, int>',
]
],
];
}

0 comments on commit 0fe378a

Please sign in to comment.
You can’t perform that action at this time.