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', + ], ]; } }