1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix #4912 - detect mismatching property type

This commit is contained in:
Matthew Brown 2020-12-29 16:36:54 +00:00 committed by Daniil Gentili
parent d169d399af
commit 3322ffb3f0
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 55 additions and 0 deletions

View File

@ -275,6 +275,7 @@
<xs:element name="MethodSignatureMismatch" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MethodSignatureMustOmitReturnType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MismatchingDocblockParamType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MismatchingDocblockPropertyType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MismatchingDocblockReturnType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MissingClosureParamType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MissingClosureReturnType" type="IssueHandlerType" minOccurs="0" />

View File

@ -0,0 +1,11 @@
# MismatchingDocblockPropertyType
Emitted when an `@var` entry in a propertys docblock does not match the property's type.
```php
<?php
class A {
/** @var array */
public string $s = [];
}
```

View File

@ -23,6 +23,7 @@ use Psalm\Issue\InternalClass;
use Psalm\Issue\InvalidExtendClass;
use Psalm\Issue\InvalidTemplateParam;
use Psalm\Issue\MethodSignatureMismatch;
use Psalm\Issue\MismatchingDocblockPropertyType;
use Psalm\Issue\MissingConstructor;
use Psalm\Issue\MissingImmutableAnnotation;
use Psalm\Issue\MissingPropertyType;
@ -1137,6 +1138,32 @@ class ClassAnalyzer extends ClassLikeAnalyzer
[],
false
);
if ($property_storage->signature_type) {
$union_comparison_result = new \Psalm\Internal\Type\Comparator\TypeComparisonResult();
if (!UnionTypeComparator::isContainedBy(
$codebase,
$fleshed_out_type,
$property_storage->signature_type,
false,
false,
$union_comparison_result
) && !$union_comparison_result->type_coerced_from_mixed
) {
if (IssueBuffer::accepts(
new MismatchingDocblockPropertyType(
'Parameter '
. $property_class_name . '::$' . $property_name
. ' has wrong type \'' . $fleshed_out_type .
'\', should be \'' . $property_storage->signature_type . '\'',
$property_type_location
)
)) {
// do nothing
}
}
}
}
if ($property_storage->is_static) {

View File

@ -0,0 +1,8 @@
<?php
namespace Psalm\Issue;
class MismatchingDocblockPropertyType extends CodeIssue
{
public const ERROR_LEVEL = 2;
public const SHORTCODE = 264;
}

View File

@ -3534,6 +3534,14 @@ class PropertyTypeTest extends TestCase
echo (new A)->foo;',
'error_message' => 'InaccessibleProperty',
],
'overwritePropertyType' => [
'<?php
class A {
/** @var array */
public string $s = [];
}',
'error_message' => 'MismatchingDocblockPropertyType',
],
];
}
}