Skip to content

Commit

Permalink
Merge pull request #6909 from trowski/promoted-readonly-properties
Browse files Browse the repository at this point in the history
Fix promoted readonly properties
  • Loading branch information
orklah committed Nov 13, 2021
2 parents c21aefa + 1bb204d commit 6d21288
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Expand Up @@ -631,12 +631,13 @@ 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;

$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;
Expand Down
28 changes: 28 additions & 0 deletions tests/ReadonlyPropertyTest.php
Expand Up @@ -243,6 +243,34 @@ class A {
false,
'8.1',
],
'readonlyPromotedPropertyAssignOperator' => [
'<?php
class A {
public function __construct(public readonly string $bar) {
}
}
$a = new A("hello");
$a->bar = "goodbye";',
'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:21',
[],
false,
'8.1',
],
'readonlyPromotedPropertyAccess' => [
'<?php
class A {
public function __construct(private readonly string $bar) {
}
}
$a = new A("hello");
$b = $a->bar;',
'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:26',
[],
false,
'8.1',
],
];
}
}

0 comments on commit 6d21288

Please sign in to comment.