1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Merge pull request #9887 from kkmuffme/readonly-property-ignore-invariance-if-contained

readonly does not have write access, therefore is safe as long as the…
This commit is contained in:
orklah 2023-06-09 13:07:19 +02:00 committed by GitHub
commit 5df772b31b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -802,7 +802,20 @@ class ClassAnalyzer extends ClassLikeAnalyzer
$codebase,
);
if ($property_storage->location
if ($guide_property_storage->readonly
&& UnionTypeComparator::isContainedBy(
$codebase,
$property_type,
$guide_property_type,
false,
false,
null,
false,
false,
)) {
// if the original property is readonly, it cannot be written
// therefore invariance is not a problem, if the parent type contains the child type
} elseif ($property_storage->location
&& !$property_type->equals($guide_property_type, false)
&& $guide_class_storage->user_defined
) {

View File

@ -231,6 +231,23 @@ class PropertyTypeInvarianceTest extends TestCase
public $d;
}',
],
'allowReadonly' => [
'code' => '<?php
class ParentClass
{
/**
* @readonly
* @var null|string
*/
protected $mightExist;
}
class ChildClass extends ParentClass
{
/** @var string */
protected $mightExist = "";
}',
],
];
}