From d06510833bf5c9b6ace2b68b9f6ef533d702d42f Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 13 Nov 2021 12:37:08 -0600 Subject: [PATCH 1/2] Fix detection of readonly on promoted properties --- .../Reflector/FunctionLikeNodeScanner.php | 1 + tests/ReadonlyPropertyTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php index fa4d4a36c46..68fe0e68952 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php @@ -631,6 +631,7 @@ 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_storage->promoted_property = true; $property_storage->is_promoted = true; diff --git a/tests/ReadonlyPropertyTest.php b/tests/ReadonlyPropertyTest.php index 7f3a0f131c9..b302d3325f4 100644 --- a/tests/ReadonlyPropertyTest.php +++ b/tests/ReadonlyPropertyTest.php @@ -243,6 +243,20 @@ class A { false, '8.1', ], + 'readonlyPromotedPropertyAssignOperator' => [ + 'bar = "goodbye";', + 'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:21', + [], + false, + '8.1', + ], ]; } } From 1bb204db768e456ecb973d7e123e24e04cb4a190 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 13 Nov 2021 12:50:02 -0600 Subject: [PATCH 2/2] Fix detecting readonly promoted property visibility --- .../Reflector/FunctionLikeNodeScanner.php | 2 +- tests/ReadonlyPropertyTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php index 68fe0e68952..4d85c3c8413 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php @@ -637,7 +637,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal $property_id = $fq_classlike_name . '::$' . $param_storage->name; - switch ($param->flags) { + switch ($param->flags & \PhpParser\Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) { case \PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC: $property_storage->visibility = ClassLikeAnalyzer::VISIBILITY_PUBLIC; $classlike_storage->inheritable_property_ids[$param_storage->name] = $property_id; diff --git a/tests/ReadonlyPropertyTest.php b/tests/ReadonlyPropertyTest.php index b302d3325f4..8bec3192352 100644 --- a/tests/ReadonlyPropertyTest.php +++ b/tests/ReadonlyPropertyTest.php @@ -257,6 +257,20 @@ public function __construct(public readonly string $bar) { false, '8.1', ], + 'readonlyPromotedPropertyAccess' => [ + 'bar;', + 'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:26', + [], + false, + '8.1', + ], ]; } }