Skip to content

Commit

Permalink
Merge pull request #7403 from orklah/readonly_phpdoc_promoted
Browse files Browse the repository at this point in the history
 support @readonly for promoted properties
  • Loading branch information
orklah committed Jan 16, 2022
2 parents 3c726e7 + 6578396 commit de824d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Expand Up @@ -607,6 +607,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal

$doc_comment = $param->getDocComment();
$var_comment_type = null;
$var_comment_readonly = false;
if ($doc_comment) {
$var_comments = CommentAnalyzer::getTypeFromComment(
$doc_comment,
Expand All @@ -620,6 +621,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal

if ($var_comment !== null) {
$var_comment_type = $var_comment->type;
$var_comment_readonly = $var_comment->readonly;
}
}

Expand Down Expand Up @@ -650,7 +652,8 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
$property_storage->location = $param_storage->location;
$property_storage->stmt_location = new CodeLocation($this->file_scanner, $param);
$property_storage->has_default = (bool)$param->default;
$property_storage->readonly = (bool)($param->flags & PhpParser\Node\Stmt\Class_::MODIFIER_READONLY);
$param_type_readonly = (bool)($param->flags & PhpParser\Node\Stmt\Class_::MODIFIER_READONLY);
$property_storage->readonly = $param_type_readonly ?: $var_comment_readonly;
$param_storage->promoted_property = true;
$property_storage->is_promoted = true;

Expand Down
23 changes: 23 additions & 0 deletions tests/ReadonlyPropertyTest.php
Expand Up @@ -275,6 +275,29 @@ public function __construct(private readonly string $bar) {
false,
'8.1',
],
'readonlyPhpDocPromotedPropertyAssignOperator' => [
'<?php
final class A
{
public function __construct(
/**
* @psalm-readonly
*/
private string $string,
) {
}
private function mutateString(): void
{
$this->string = "";
}
}',
'error_message' => 'InaccessibleProperty',
[],
false,
'8.1',
],
];
}
}

0 comments on commit de824d6

Please sign in to comment.