Skip to content

Commit

Permalink
Emit MethodSignatureMismatch when descendant does not return by refer…
Browse files Browse the repository at this point in the history
…ence
  • Loading branch information
tuqqu committed Oct 3, 2023
1 parent 3a8f2d2 commit 9f9e5f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Internal/Analyzer/MethodComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions tests/MethodSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,34 @@ public function __destruct() {}
}
',
],
'allowByRefReturn' => [
'code' => '<?php
interface Foo {
public function &foo(): int;
}
class Bar implements Foo {
private int $x = 0;
public function &foo(): int {
return $this->x;
}
}
',
],
'descendantAddsByRefReturn' => [
'code' => '<?php
interface Foo {
public function foo(): int;
}
class Bar implements Foo {
private int $x = 0;
public function &foo(): int {
return $this->x;
}
}
',
],
];
}

Expand Down Expand Up @@ -1586,6 +1614,20 @@ public function jsonSerialize() {
'ignored_issues' => [],
'php_version' => '8.1',
],
'absentByRefReturnInDescendant' => [
'code' => '<?php
interface Foo {
public function &foo(): int;
}
class Bar implements Foo {
public function foo(): int {
return 1;
}
}
',
'error_message' => 'MethodSignatureMismatch',
],
];
}
}

0 comments on commit 9f9e5f1

Please sign in to comment.