Skip to content

Commit

Permalink
vimeo#7731 - recognize @psalm-allow-private-mutation in PHP 8+ cons…
Browse files Browse the repository at this point in the history
…tructors
  • Loading branch information
someniatko committed Jul 29, 2022
1 parent d7cd84c commit 0abde25
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Expand Up @@ -606,6 +606,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
$doc_comment = $param->getDocComment();
$var_comment_type = null;
$var_comment_readonly = false;
$var_comment_allow_private_mutation = 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;
$var_comment_allow_private_mutation = $var_comment->allow_private_mutation;
}
}

Expand Down Expand Up @@ -652,6 +654,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
$property_storage->has_default = (bool)$param->default;
$param_type_readonly = (bool)($param->flags & PhpParser\Node\Stmt\Class_::MODIFIER_READONLY);
$property_storage->readonly = $param_type_readonly ?: $var_comment_readonly;
$property_storage->allow_private_mutation = $var_comment_allow_private_mutation;
$param_storage->promoted_property = true;
$property_storage->is_promoted = true;

Expand Down
37 changes: 36 additions & 1 deletion tests/ReadonlyPropertyTest.php
Expand Up @@ -64,7 +64,7 @@ public function setBar(string $s) : void {
echo (new A)->bar;'
],
'readonlyPublicPropertySetInAnotherMEthod' => [
'readonlyPublicPropertySetInAnotherMethod' => [
'<?php
class A {
/**
Expand All @@ -79,6 +79,41 @@ public function setBar(string $s) : void {
echo (new A)->bar;'
],
'docblockReadonlyWithPrivateMutationsAllowedConstructorPropertySetInAnotherMethod' => [
'<?php
class A {
public function __construct(
/**
* @readonly
* @psalm-allow-private-mutation
*/
public ?string $bar = null,
) {}
public function setBar(string $s) : void {
$this->bar = $s;
}
}
echo (new A)->bar;'
],
'readonlyPublicConstructorPropertySetInAnotherMethod' => [
'<?php
class A {
public function __construct(
/**
* @psalm-readonly-allow-private-mutation
*/
public ?string $bar = null,
) {}
public function setBar(string $s) : void {
$this->bar = $s;
}
}
echo (new A)->bar;'
],
'readonlyPropertySetChildClass' => [
'<?php
abstract class A {
Expand Down

0 comments on commit 0abde25

Please sign in to comment.