1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

#7731 - recognize @psalm-allow-private-mutation in PHP 8+ constructors

This commit is contained in:
someniatko 2022-07-29 16:50:56 +03:00
parent d7cd84c4eb
commit 0abde258fa
2 changed files with 39 additions and 1 deletions

View File

@ -606,6 +606,7 @@ class FunctionLikeNodeScanner
$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,
@ -620,6 +621,7 @@ class FunctionLikeNodeScanner
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;
}
}
@ -652,6 +654,7 @@ class FunctionLikeNodeScanner
$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;

View File

@ -64,7 +64,7 @@ class ReadonlyPropertyTest extends TestCase
echo (new A)->bar;'
],
'readonlyPublicPropertySetInAnotherMEthod' => [
'readonlyPublicPropertySetInAnotherMethod' => [
'<?php
class A {
/**
@ -79,6 +79,41 @@ class ReadonlyPropertyTest extends TestCase
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 {