Skip to content
Permalink
Browse files

Static methods inside @psalm-immutable class should be permitted

Fixes #2109
  • Loading branch information...
muglug committed Sep 9, 2019
1 parent f8b8f35 commit 56c884845fb1548793fcc903ec251c0a4f02fb14
@@ -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`

@@ -262,13 +262,17 @@ private function populateClassLikeStorage(ClassLikeStorage $storage, array $depe
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;
}
}
}
}
@@ -93,6 +93,26 @@ function addItemToCart(Cart $c, string $name, float $price) : Cart {
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));
}
}'
]
];
}

0 comments on commit 56c8848

Please sign in to comment.
You can’t perform that action at this time.