From e26e0224284e2529c4ba82a3db293bb7567ad0d0 Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 16 Jan 2022 09:29:05 +0100 Subject: [PATCH 1/2] support @readonly for promoted properties --- .../PhpVisitor/Reflector/FunctionLikeNodeScanner.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php index de938d517..821c04127 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php @@ -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; From 6578396afb705a7c6e9676691a5ee0e2776434d8 Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 16 Jan 2022 09:29:16 +0100 Subject: [PATCH 2/2] add test --- tests/ReadonlyPropertyTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/ReadonlyPropertyTest.php b/tests/ReadonlyPropertyTest.php index 4a85f39fa..4cf7c4728 100644 --- a/tests/ReadonlyPropertyTest.php +++ b/tests/ReadonlyPropertyTest.php @@ -275,6 +275,29 @@ class ReadonlyPropertyTest extends TestCase false, '8.1', ], + 'readonlyPhpDocPromotedPropertyAssignOperator' => [ + 'string = ""; + } + }', + 'error_message' => 'InaccessibleProperty', + [], + false, + '8.1', + ], ]; } }