From 9f9e5f1e18208a72aa5c6dc8188ffdbf36aadf06 Mon Sep 17 00:00:00 2001 From: tuqqu Date: Tue, 3 Oct 2023 03:41:11 +0200 Subject: [PATCH] Emit MethodSignatureMismatch when descendant does not return by reference --- .../Internal/Analyzer/MethodComparator.php | 10 +++++ tests/MethodSignatureTest.php | 42 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/MethodComparator.php b/src/Psalm/Internal/Analyzer/MethodComparator.php index 82e1c6c4863..d595df15803 100644 --- a/src/Psalm/Internal/Analyzer/MethodComparator.php +++ b/src/Psalm/Internal/Analyzer/MethodComparator.php @@ -313,6 +313,16 @@ private static function checkForObviousMethodMismatches( ); } + if ($guide_method_storage->returns_by_ref && !$implementer_method_storage->returns_by_ref) { + IssueBuffer::maybeAdd( + new MethodSignatureMismatch( + 'Method ' . $cased_implementer_method_id . ' must return by-reference', + $code_location, + ), + $suppressed_issues + $implementer_classlike_storage->suppressed_issues, + ); + } + if ($guide_method_storage->external_mutation_free && !$implementer_method_storage->external_mutation_free && !$guide_method_storage->mutation_free_inferred diff --git a/tests/MethodSignatureTest.php b/tests/MethodSignatureTest.php index 4c65719ee5f..61cd41b9b99 100644 --- a/tests/MethodSignatureTest.php +++ b/tests/MethodSignatureTest.php @@ -929,6 +929,34 @@ public function __destruct() {} } ', ], + 'allowByRefReturn' => [ + 'code' => 'x; + } + } + ', + ], + 'descendantAddsByRefReturn' => [ + 'code' => 'x; + } + } + ', + ], ]; } @@ -1586,6 +1614,20 @@ public function jsonSerialize() { 'ignored_issues' => [], 'php_version' => '8.1', ], + 'absentByRefReturnInDescendant' => [ + 'code' => ' 'MethodSignatureMismatch', + ], ]; } }