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

Emit an issue when returning a Stringable object when a string is expected (#4657)

* Emit an issue when returning a Stringable object when a string is expected

* Fix issue in Psalm codebase
This commit is contained in:
orklah 2020-11-24 06:18:24 +01:00 committed by Daniil Gentili
parent e9c608e7cb
commit 26410ad19d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 22 additions and 3 deletions

View File

@ -844,7 +844,9 @@ class Codebase
*/ */
public function getDeclaringMethodId($method_id): ?string public function getDeclaringMethodId($method_id): ?string
{ {
return $this->methods->getDeclaringMethodId(Internal\MethodIdentifier::wrap($method_id)); $new_method_id = $this->methods->getDeclaringMethodId(Internal\MethodIdentifier::wrap($method_id));
return $new_method_id ? (string) $new_method_id : null;
} }
/** /**
@ -855,7 +857,9 @@ class Codebase
*/ */
public function getAppearingMethodId($method_id): ?string public function getAppearingMethodId($method_id): ?string
{ {
return $this->methods->getAppearingMethodId(Internal\MethodIdentifier::wrap($method_id)); $new_method_id = $this->methods->getAppearingMethodId(Internal\MethodIdentifier::wrap($method_id));
return $new_method_id ? (string) $new_method_id : null;
} }
/** /**

View File

@ -18,6 +18,7 @@ use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\CodeLocation; use Psalm\CodeLocation;
use Psalm\Context; use Psalm\Context;
use Psalm\Internal\FileManipulation\FunctionDocblockManipulator; use Psalm\Internal\FileManipulation\FunctionDocblockManipulator;
use Psalm\Issue\ImplicitToStringCast;
use Psalm\Issue\InvalidFalsableReturnType; use Psalm\Issue\InvalidFalsableReturnType;
use Psalm\Issue\InvalidNullableReturnType; use Psalm\Issue\InvalidNullableReturnType;
use Psalm\Issue\InvalidParent; use Psalm\Issue\InvalidParent;
@ -591,6 +592,20 @@ class ReturnTypeAnalyzer
} }
} }
if ($union_comparison_results->to_string_cast) {
if (IssueBuffer::accepts(
new ImplicitToStringCast(
'The declared return type for ' . $cased_method_id . ' expects \'' .
$declared_return_type . '\', ' . '\'' . $inferred_return_type .
'\' provided with a __toString method',
$return_type_location
),
$suppressed_issues
)) {
// fall through
}
}
if (!$inferred_return_type->ignore_nullable_issues if (!$inferred_return_type->ignore_nullable_issues
&& $inferred_return_type->isNullable() && $inferred_return_type->isNullable()
&& !$declared_return_type->isNullable() && !$declared_return_type->isNullable()

View File

@ -1040,7 +1040,7 @@ class Methods
$method_id = $this->getDeclaringMethodId($original_method_id); $method_id = $this->getDeclaringMethodId($original_method_id);
if ($method_id === null) { if ($method_id === null) {
return $original_method_id; return (string) $original_method_id;
} }
$fq_class_name = $method_id->fq_class_name; $fq_class_name = $method_id->fq_class_name;