mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #3870 - mark properties as reference-free only for externally-immutable classes
This commit is contained in:
parent
3bc91b9944
commit
3848fa6872
@ -216,9 +216,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
||||
|
||||
$context->vars_in_scope['$this'] = new Type\Union([$this_object_type]);
|
||||
|
||||
if ($storage->external_mutation_free
|
||||
&& !$storage->mutation_free_inferred
|
||||
) {
|
||||
if ($appearing_class_storage->external_mutation_free) {
|
||||
$context->vars_in_scope['$this']->reference_free = true;
|
||||
|
||||
if ($this->function->name->name !== '__construct') {
|
||||
|
@ -699,6 +699,32 @@ class ImmutableAnnotationTest extends TestCase
|
||||
}',
|
||||
'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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user