diff --git a/src/Psalm/Codebase.php b/src/Psalm/Codebase.php index a97d884c7..6c949a83e 100644 --- a/src/Psalm/Codebase.php +++ b/src/Psalm/Codebase.php @@ -844,7 +844,9 @@ class Codebase */ 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 { - 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; } /** diff --git a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php index f5585ee93..7f49dbb13 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php @@ -18,6 +18,7 @@ use Psalm\Internal\Type\Comparator\UnionTypeComparator; use Psalm\CodeLocation; use Psalm\Context; use Psalm\Internal\FileManipulation\FunctionDocblockManipulator; +use Psalm\Issue\ImplicitToStringCast; use Psalm\Issue\InvalidFalsableReturnType; use Psalm\Issue\InvalidNullableReturnType; 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 && $inferred_return_type->isNullable() && !$declared_return_type->isNullable() diff --git a/src/Psalm/Internal/Codebase/Methods.php b/src/Psalm/Internal/Codebase/Methods.php index df4ead7d6..864f49645 100644 --- a/src/Psalm/Internal/Codebase/Methods.php +++ b/src/Psalm/Internal/Codebase/Methods.php @@ -1040,7 +1040,7 @@ class Methods $method_id = $this->getDeclaringMethodId($original_method_id); if ($method_id === null) { - return $original_method_id; + return (string) $original_method_id; } $fq_class_name = $method_id->fq_class_name;