Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readonly does not have write access, therefore is safe as long as the… #9887

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -802,7 +802,20 @@ public static function addContextProperties(
$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
) {
Expand Down
17 changes: 17 additions & 0 deletions tests/PropertyTypeInvarianceTest.php
Expand Up @@ -231,6 +231,23 @@ class Baz extends Bar {
public $d;
}',
],
'allowReadonly' => [
'code' => '<?php
class ParentClass
{
/**
* @readonly
* @var null|string
*/
protected $mightExist;
}

class ChildClass extends ParentClass
{
/** @var string */
protected $mightExist = "";
}',
],
];
}

Expand Down