From 3322ffb3f0fb3e4bc314573c680f6199fb0a7cb6 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 29 Dec 2020 16:36:54 +0000 Subject: [PATCH] Fix #4912 - detect mismatching property type --- config.xsd | 1 + .../issues/MismatchingDocblockPropertyType.md | 11 ++++++++ src/Psalm/Internal/Analyzer/ClassAnalyzer.php | 27 +++++++++++++++++++ .../Issue/MismatchingDocblockPropertyType.php | 8 ++++++ tests/PropertyTypeTest.php | 8 ++++++ 5 files changed, 55 insertions(+) create mode 100644 docs/running_psalm/issues/MismatchingDocblockPropertyType.md create mode 100644 src/Psalm/Issue/MismatchingDocblockPropertyType.php diff --git a/config.xsd b/config.xsd index 0f21145cc..05e12b323 100644 --- a/config.xsd +++ b/config.xsd @@ -275,6 +275,7 @@ + diff --git a/docs/running_psalm/issues/MismatchingDocblockPropertyType.md b/docs/running_psalm/issues/MismatchingDocblockPropertyType.md new file mode 100644 index 000000000..b9b82ba79 --- /dev/null +++ b/docs/running_psalm/issues/MismatchingDocblockPropertyType.md @@ -0,0 +1,11 @@ +# MismatchingDocblockPropertyType + +Emitted when an `@var` entry in a property’s docblock does not match the property's type. + +```php +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) { diff --git a/src/Psalm/Issue/MismatchingDocblockPropertyType.php b/src/Psalm/Issue/MismatchingDocblockPropertyType.php new file mode 100644 index 000000000..6955bfd27 --- /dev/null +++ b/src/Psalm/Issue/MismatchingDocblockPropertyType.php @@ -0,0 +1,8 @@ +foo;', 'error_message' => 'InaccessibleProperty', ], + 'overwritePropertyType' => [ + ' 'MismatchingDocblockPropertyType', + ], ]; } }