1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Use better metric for a properly mismatching type

This commit is contained in:
Brown 2019-05-14 16:15:31 -04:00
parent 91fbb976ab
commit e7f4a52d2b
2 changed files with 48 additions and 11 deletions

View File

@ -908,9 +908,17 @@ class MethodAnalyzer extends FunctionLikeAnalyzer
} }
} }
} else { } else {
if (TypeAnalyzer::isContainedBy(
$codebase,
$implementer_method_storage_param_type,
$guide_method_storage_param_type,
!$guide_classlike_storage->user_defined,
!$guide_classlike_storage->user_defined
)) {
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
new ImplementedParamTypeMismatch( new MoreSpecificImplementedParamType(
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' . 'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id
. ' has the more specific type \'' .
$implementer_method_storage_param_type->getId() . '\', expecting \'' . $implementer_method_storage_param_type->getId() . '\', expecting \'' .
$guide_method_storage_param_type->getId() . '\' as defined by ' . $guide_method_storage_param_type->getId() . '\' as defined by ' .
$cased_guide_method_id, $cased_guide_method_id,
@ -921,6 +929,22 @@ class MethodAnalyzer extends FunctionLikeAnalyzer
)) { )) {
return false; return false;
} }
} else {
if (IssueBuffer::accepts(
new ImplementedParamTypeMismatch(
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id
. ' has wrong type \'' .
$implementer_method_storage_param_type->getId() . '\', expecting \'' .
$guide_method_storage_param_type->getId() . '\' as defined by ' .
$cased_guide_method_id,
$implementer_method_storage->params[$i]->location
?: $code_location
),
$suppressed_issues
)) {
return false;
}
}
} }
} }
} }

View File

@ -943,6 +943,19 @@ class MethodSignatureTest extends TestCase
'error_message' => 'MethodSignatureMismatch', 'error_message' => 'MethodSignatureMismatch',
['MoreSpecificImplementedParamType'] ['MoreSpecificImplementedParamType']
], ],
'preventOneOfUnionMoreSpecific' => [
'<?php
class A {
/** @param string|int $s */
public function foo($s) : void {}
}
class B extends A {
/** @param string $s */
public function foo($s) : void {}
}',
'error_message' => 'MoreSpecificImplementedParamType',
],
'preventImplementingSerializableWithType' => [ 'preventImplementingSerializableWithType' => [
'<?php '<?php
class Foo implements \Serializable { class Foo implements \Serializable {