1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

PHP 8.1: Report missing typehints in overridden native methods

This commit is contained in:
Daniil Gentili 2022-01-10 13:50:16 +01:00
parent a0f52eae2f
commit cee9eb0ead
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 59 additions and 1 deletions

View File

@ -27,6 +27,7 @@ use Psalm\Issue\OverriddenMethodAccess;
use Psalm\Issue\ParamNameMismatch;
use Psalm\Issue\TraitMethodSignatureMismatch;
use Psalm\IssueBuffer;
use Psalm\Storage\AttributeStorage;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Storage\MethodStorage;
@ -35,6 +36,7 @@ use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use function array_filter;
use function in_array;
use function strpos;
use function strtolower;
@ -113,6 +115,27 @@ class MethodComparator
);
}
if (!$guide_classlike_storage->user_defined
&& $implementer_classlike_storage->user_defined
&& $codebase->analysis_php_version_id >= 8_01_00
&& ($guide_method_storage->return_type
|| $guide_method_storage->signature_return_type
)
&& !$implementer_method_storage->signature_return_type
&& !array_filter(
$implementer_method_storage->attributes,
fn (AttributeStorage $s) => $s->fq_class_name === 'ReturnTypeWillChange'
)
) {
IssueBuffer::maybeAdd(
new MethodSignatureMismatch(
'Method ' . $cased_implementer_method_id . ' is missing a return type signature!',
$implementer_method_storage->location ?: $code_location
),
$suppressed_issues + $implementer_classlike_storage->suppressed_issues
);
}
if ($guide_method_storage->return_type
&& $implementer_method_storage->return_type
&& !$implementer_method_storage->inherited_return_type
@ -862,7 +885,11 @@ class MethodComparator
$implementer_signature_return_type,
$guide_signature_return_type
)
: UnionTypeComparator::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type);
: (!$implementer_signature_return_type
&& $guide_signature_return_type->isMixed()
? false
: UnionTypeComparator::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type)
);
if (!$is_contained_by) {
if ($codebase->php_major_version >= 8

View File

@ -1569,6 +1569,37 @@ class MethodSignatureTest extends TestCase
',
'error_message' => 'MethodSignatureMismatch',
],
'noMixedTypehintInDescendant' => [
'<?php
class a {
public function test(): mixed {
return 0;
}
}
class b extends a {
public function test() {
return 0;
}
}
',
'error_message' => 'MethodSignatureMismatch',
[],
false,
'8.0'
],
'noTypehintInNativeDescendant' => [
'<?php
class a implements JsonSerializable {
public function jsonSerialize() {
return 0;
}
}
',
'error_message' => 'MethodSignatureMismatch',
[],
false,
'8.1'
],
];
}
}