1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Use better solution for inherited more-specific return types

This commit is contained in:
Brown 2019-05-09 10:41:30 -04:00
parent 629eeed3e6
commit 24dbc34bbe
6 changed files with 26 additions and 29 deletions

View File

@ -655,12 +655,11 @@ class ReturnTypeAnalyzer
$context->self
);
if ((!$storage instanceof MethodStorage || !$storage->imported_return_type)
&& !TypeAnalyzer::isContainedBy(
$codebase,
$fleshed_out_return_type,
$fleshed_out_signature_type
)
if (!TypeAnalyzer::isContainedBy(
$codebase,
$fleshed_out_return_type,
$fleshed_out_signature_type
)
) {
if ($codebase->alter_code
&& isset($project_analyzer->getIssuesToFix()['MismatchingDocblockReturnType'])

View File

@ -237,7 +237,7 @@ class TypeAnalyzer
$container_type_not_null = clone $container_type;
$container_type_not_null->removeType('null');
return (bool) array_intersect_key(
return !array_diff_key(
$input_type_not_null->getTypes(),
$container_type_not_null->getTypes()
);

View File

@ -322,13 +322,11 @@ class Populator
) {
if ($declaring_method_storage->signature_return_type) {
$method_storage->return_type = $declaring_method_storage->return_type;
$method_storage->imported_return_type = true;
} elseif (TypeAnalyzer::isSimplyContainedBy(
$declaring_method_storage->return_type,
$method_storage->signature_return_type
)) {
$method_storage->return_type = $declaring_method_storage->return_type;
$method_storage->imported_return_type = true;
}
}
}

View File

@ -59,9 +59,4 @@ class MethodStorage extends FunctionLikeStorage
* @var bool
*/
public $has_docblock_param_types = false;
/**
* @var bool
*/
public $imported_return_type = false;
}

View File

@ -967,21 +967,6 @@ class AnnotationTest extends TestCase
'$b' => 'array<array-key, int>',
]
],
'allowLessSpecificDocblockTypeOnParent' => [
'<?php
abstract class Foo {
/**
* @return array|string
*/
abstract public function getTargets();
}
class Bar extends Foo {
public function getTargets(): string {
return "baz";
}
}',
],
];
}

View File

@ -447,6 +447,26 @@ class MethodSignatureTest extends TestCase
(new C)->f("b", 3, 0.5);
(new C)->f("b", 3, 0.5, 0.8);',
],
'allowLessSpecificDocblockTypeOnParent' => [
'<?php
abstract class Foo {
/**
* @return array|string
*/
abstract public function getTargets();
}
class Bar extends Foo {
public function getTargets(): string {
return "baz";
}
}
$a = (new Bar)->getTargets();',
[
'$a' => 'string',
]
],
];
}