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:
commit
5df772b31b
@ -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
|
||||
) {
|
||||
|
@ -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 = "";
|
||||
}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user