1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #7403 from orklah/readonly_phpdoc_promoted

support @readonly for promoted properties
This commit is contained in:
orklah 2022-01-16 09:38:37 +01:00 committed by GitHub
commit de824d6724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -607,6 +607,7 @@ class FunctionLikeNodeScanner
$doc_comment = $param->getDocComment();
$var_comment_type = null;
$var_comment_readonly = false;
if ($doc_comment) {
$var_comments = CommentAnalyzer::getTypeFromComment(
$doc_comment,
@ -620,6 +621,7 @@ class FunctionLikeNodeScanner
if ($var_comment !== null) {
$var_comment_type = $var_comment->type;
$var_comment_readonly = $var_comment->readonly;
}
}
@ -650,7 +652,8 @@ class FunctionLikeNodeScanner
$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;

View File

@ -275,6 +275,29 @@ class ReadonlyPropertyTest extends TestCase
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',
],
];
}
}