Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit MethodSignatureMismatch when descendant does not return by reference #10248

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
],
];
}
}
Loading