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

Change some errors from InvalidReturnType to MoreSpecificReturnType

This commit is contained in:
Matthew Brown 2017-02-11 18:25:44 -05:00
parent a5f64aa0ad
commit deb11c9402
4 changed files with 52 additions and 36 deletions

View File

@ -1000,21 +1000,7 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
if (!$return_type) {
if ($inferred_return_type && !$inferred_return_type->isMixed()) {
FileChecker::addDocblockReturnType(
$this->source->getFileName(),
$this->function->getLine(),
(string)$this->function->getDocComment(),
$inferred_return_type->toNamespacedString(
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
false
),
$inferred_return_type->toNamespacedString(
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
true
)
);
$this->addDocblockReturnType($inferred_return_type);
}
return null;
@ -1076,34 +1062,20 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
$inferred_return_type,
$declared_return_type,
$this->getFileChecker(),
false,
true,
$has_scalar_match,
$type_coerced
)) {
if ($update_docblock) {
if (!in_array('InvalidReturnType', $this->suppressed_issues)) {
FileChecker::addDocblockReturnType(
$this->source->getFileName(),
$this->function->getLine(),
(string)$this->function->getDocComment(),
$inferred_return_type->toNamespacedString(
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
false
),
$inferred_return_type->toNamespacedString(
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
true
)
);
$this->addDocblockReturnType($inferred_return_type);
}
return null;
}
// is the declared return type more specific than the inferred one?
if ($declared_return_type->isNullable() === $inferred_return_type->isNullable() && $type_coerced) {
if ($type_coerced) {
if (IssueBuffer::accepts(
new MoreSpecificReturnType(
'The given return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
@ -1126,12 +1098,54 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
return false;
}
}
} elseif ($inferred_return_type->isNullable() !== $declared_return_type->isNullable()) {
if ($update_docblock) {
if (!in_array('InvalidReturnType', $this->suppressed_issues)) {
$this->addDocblockReturnType($inferred_return_type);
}
return null;
}
if (IssueBuffer::accepts(
new MoreSpecificReturnType(
'The given return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
' is more specific than the inferred return type \'' . $inferred_return_type . '\'',
$secondary_return_type_location ?: $return_type_location
),
$this->suppressed_issues
)) {
return false;
}
}
}
return null;
}
/**
* @param Type\Union $inferred_return_type
* @return void
*/
protected function addDocblockReturnType(Type\Union $inferred_return_type)
{
FileChecker::addDocblockReturnType(
$this->source->getFileName(),
$this->function->getLine(),
(string)$this->function->getDocComment(),
$inferred_return_type->toNamespacedString(
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
false
),
$inferred_return_type->toNamespacedString(
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
true
)
);
}
/**
* @param array<int, array{type:string,name:string,line_number:int}> $docblock_params
* @param FunctionLikeStorage $storage

View File

@ -945,7 +945,9 @@ class FetchChecker
$type->type_params[1]->isEmpty()
)
) {
$properties = $string_key_value ? [$string_key_value => $keyed_assignment_type] : [];
$properties = $keyed_assignment_type && $string_key_value
? [$string_key_value => $keyed_assignment_type]
: [];
$assignment_type = new Type\Union([
new Type\Atomic\ObjectLike($properties)

View File

@ -851,7 +851,7 @@ class TypeChecker
$input_param,
$container_param,
$file_checker,
$ignore_null,
false,
$has_scalar_match,
$type_coerced
)

View File

@ -587,7 +587,7 @@ class ReturnTypeTest extends PHPUnit_Framework_TestCase
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidReturnType
* @expectedExceptionMessage MoreSpecificReturnType
* @return void
*/
public function testWrongReturnType2()
@ -625,7 +625,7 @@ class ReturnTypeTest extends PHPUnit_Framework_TestCase
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidReturnType
* @expectedExceptionMessage MoreSpecificReturnType
* @return void
*/
public function testWrongReturnTypeInNamespace2()