1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Static methods inside @psalm-immutable class should be permitted

Fixes #2109
This commit is contained in:
Brown 2019-09-09 10:38:55 -04:00
parent f8b8f35c03
commit 56c884845f
3 changed files with 34 additions and 10 deletions

View File

@ -184,7 +184,7 @@ Used to annotate a property that can only be written to in its defining class's
class B {
/** @readonly */
public string $s;
public function __construct(string $s) {
$this->s = $s;
}
@ -202,18 +202,18 @@ Used to annotate a class method that does not mutate state, either internally or
```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
*/
@ -231,11 +231,11 @@ Used to annotate a class method that does not mutate state, either internally or
```php
class E {
private string $s;
public function __construct(string $s) {
$this->s = $s;
}
/**
* @psalm-external-mutation-free
*/
@ -255,7 +255,7 @@ class E {
### `@psalm-immutable`
Used to annotate a class where every property is treated by consumers as `@psalm-readonly` and every method is treated as `@psalm-mutation-free`.
Used to annotate a class where every property is treated by consumers as `@psalm-readonly` and every instance method is treated as `@psalm-mutation-free`.
### `@psalm-pure`

View File

@ -262,13 +262,17 @@ class Populator
if ($storage->mutation_free || $storage->external_mutation_free) {
foreach ($storage->methods as $method) {
$method->mutation_free = $storage->mutation_free;
$method->external_mutation_free = $storage->external_mutation_free;
if (!$method->is_static) {
$method->mutation_free = $storage->mutation_free;
$method->external_mutation_free = $storage->external_mutation_free;
}
}
if ($storage->mutation_free) {
foreach ($storage->properties as $property) {
$property->readonly = true;
if (!$property->is_static) {
$property->readonly = true;
}
}
}
}

View File

@ -93,6 +93,26 @@ class ImmutableAnnotationTest extends TestCase
return $c->addItem(new CartItem($name, $price));
}',
],
'allowImpureStaticMethod' => [
'<?php
/**
* @psalm-immutable
*/
final class ClientId
{
public string $id;
private function __construct(string $id)
{
$this->id = $id;
}
public static function fromString(string $id): self
{
return new self($id . rand(0, 1));
}
}'
]
];
}