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:
parent
d169d399af
commit
3322ffb3f0
@ -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" />
|
||||
|
11
docs/running_psalm/issues/MismatchingDocblockPropertyType.md
Normal file
11
docs/running_psalm/issues/MismatchingDocblockPropertyType.md
Normal file
@ -0,0 +1,11 @@
|
||||
# MismatchingDocblockPropertyType
|
||||
|
||||
Emitted when an `@var` entry in a property’s docblock does not match the property's type.
|
||||
|
||||
```php
|
||||
<?php
|
||||
class A {
|
||||
/** @var array */
|
||||
public string $s = [];
|
||||
}
|
||||
```
|
@ -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) {
|
||||
|
8
src/Psalm/Issue/MismatchingDocblockPropertyType.php
Normal file
8
src/Psalm/Issue/MismatchingDocblockPropertyType.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Psalm\Issue;
|
||||
|
||||
class MismatchingDocblockPropertyType extends CodeIssue
|
||||
{
|
||||
public const ERROR_LEVEL = 2;
|
||||
public const SHORTCODE = 264;
|
||||
}
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user