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

Fix #3870 - mark properties as reference-free only for externally-immutable classes

This commit is contained in:
Brown 2020-07-23 01:48:06 -04:00
parent 3bc91b9944
commit 3848fa6872
2 changed files with 27 additions and 3 deletions

View File

@ -216,9 +216,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
$context->vars_in_scope['$this'] = new Type\Union([$this_object_type]); $context->vars_in_scope['$this'] = new Type\Union([$this_object_type]);
if ($storage->external_mutation_free if ($appearing_class_storage->external_mutation_free) {
&& !$storage->mutation_free_inferred
) {
$context->vars_in_scope['$this']->reference_free = true; $context->vars_in_scope['$this']->reference_free = true;
if ($this->function->name->name !== '__construct') { if ($this->function->name->name !== '__construct') {

View File

@ -699,6 +699,32 @@ class ImmutableAnnotationTest extends TestCase
}', }',
'error_message' => 'ImpurePropertyAssignment', 'error_message' => 'ImpurePropertyAssignment',
], ],
'mutationInPropertyAssignment' => [
'<?php
class D {
private string $s;
public function __construct(string $s) {
$this->s = $s;
}
/**
* @psalm-mutation-free
*/
public function getShort() : string {
return substr($this->s, 0, 5);
}
/**
* @psalm-mutation-free
*/
public function getShortMutating() : string {
$this->s .= "hello";
return substr($this->s, 0, 5);
}
}',
'error_message' => 'ImpurePropertyAssignment',
],
]; ];
} }
} }