1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Emit MethodSignatureMismatch when descendant does not return by reference

This commit is contained in:
tuqqu 2023-10-03 03:41:11 +02:00
parent 3a8f2d2a07
commit 9f9e5f1e18
2 changed files with 52 additions and 0 deletions

View File

@ -313,6 +313,16 @@ class MethodComparator
);
}
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

View File

@ -929,6 +929,34 @@ class MethodSignatureTest extends TestCase
}
',
],
'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;
}
}
',
],
];
}
@ -1586,6 +1614,20 @@ class MethodSignatureTest extends TestCase
'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',
],
];
}
}