1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #8341 from someniatko/issue-7731

recognize `@psalm-allow-private-mutation` in PHP 8+ constructors
This commit is contained in:
AndrolGenhald 2022-07-29 09:31:20 -05:00 committed by GitHub
commit 148264305f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 {