2019-08-30 18:36:35 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-08-30 18:36:35 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2019-08-30 18:36:35 +02:00
|
|
|
class ImmutableAnnotationTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2019-08-30 18:36:35 +02:00
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:array<string>}>
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2019-08-30 18:36:35 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'immutableClassGenerating' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-30 18:36:35 +02:00
|
|
|
/**
|
2019-09-01 00:43:39 +02:00
|
|
|
* @psalm-immutable
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
public function __construct(int $a, string $b) {
|
|
|
|
$this->a = $a;
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setA(int $a) : self {
|
2019-09-02 04:13:16 +02:00
|
|
|
return new self($a, $this->b);
|
2019-08-30 18:36:35 +02:00
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'callInternalClassMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-30 18:36:35 +02:00
|
|
|
/**
|
2019-09-01 00:43:39 +02:00
|
|
|
* @psalm-immutable
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct(string $a) {
|
|
|
|
$this->a = $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getA() : string {
|
|
|
|
return $this->a;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHelloA() : string {
|
|
|
|
return "hello" . $this->getA();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-09-01 04:03:37 +02:00
|
|
|
'addToCart' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-01 04:03:37 +02:00
|
|
|
/** @psalm-immutable */
|
|
|
|
class Cart {
|
|
|
|
/** @var CartItem[] */
|
|
|
|
public array $items;
|
|
|
|
|
|
|
|
/** @param CartItem[] $items */
|
|
|
|
public function __construct(array $items) {
|
|
|
|
$this->items = $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addItem(CartItem $item) : self {
|
|
|
|
$items = $this->items;
|
|
|
|
$items[] = $item;
|
|
|
|
return new Cart($items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-immutable */
|
|
|
|
class CartItem {
|
|
|
|
public string $name;
|
|
|
|
public float $price;
|
|
|
|
|
|
|
|
public function __construct(string $name, float $price) {
|
|
|
|
$this->name = $name;
|
|
|
|
$this->price = $price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-pure */
|
|
|
|
function addItemToCart(Cart $c, string $name, float $price) : Cart {
|
|
|
|
return $c->addItem(new CartItem($name, $price));
|
|
|
|
}',
|
|
|
|
],
|
2019-09-09 16:38:55 +02:00
|
|
|
'allowImpureStaticMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-09 16:38:55 +02:00
|
|
|
/**
|
|
|
|
* @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));
|
|
|
|
}
|
|
|
|
}'
|
2019-09-09 17:14:40 +02:00
|
|
|
],
|
|
|
|
'allowPropertySetOnNewInstance' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-09 17:14:40 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
protected string $bar;
|
|
|
|
|
|
|
|
public function __construct(string $bar) {
|
|
|
|
$this->bar = $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withBar(string $bar): self {
|
|
|
|
$new = new Foo("hello");
|
|
|
|
$new->bar = $bar;
|
|
|
|
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-09-09 18:11:04 +02:00
|
|
|
'allowArrayMapCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-09 18:11:04 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Address
|
|
|
|
{
|
|
|
|
private $line1;
|
|
|
|
private $line2;
|
|
|
|
private $city;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $line1,
|
|
|
|
?string $line2,
|
|
|
|
string $city
|
|
|
|
) {
|
|
|
|
$this->line1 = $line1;
|
|
|
|
$this->line2 = $line2;
|
|
|
|
$this->city = $city;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
$parts = [
|
|
|
|
$this->line1,
|
|
|
|
$this->line2 ?? "",
|
|
|
|
$this->city,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Remove empty parts
|
|
|
|
$parts = \array_map("trim", $parts);
|
|
|
|
$parts = \array_filter($parts, "strlen");
|
|
|
|
$parts = \array_map(function(string $s) { return $s;}, $parts);
|
|
|
|
|
|
|
|
return \implode(", ", $parts);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-09-10 16:31:46 +02:00
|
|
|
'allowPropertyAssignmentInUnserialize' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-10 16:31:46 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Foo implements \Serializable {
|
|
|
|
/** @var string */
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->data = "Foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unserialize($data) {
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getData(): string {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-07-20 10:50:50 +02:00
|
|
|
'allowPropertyAssignmentInMagicUnserialize' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-20 10:50:50 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->data = "Foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __serialize(): array {
|
|
|
|
return ["data" => $this->data];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param array{data: string} $data */
|
|
|
|
public function __unserialize(array $data): void {
|
|
|
|
$this->data = $data["data"];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getData(): string {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-09-25 23:39:32 +02:00
|
|
|
'allowMethodOverriding' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-25 23:39:32 +02:00
|
|
|
class A {
|
|
|
|
private string $a;
|
|
|
|
|
|
|
|
public function __construct(string $a) {
|
|
|
|
$this->a = $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getA() : string {
|
|
|
|
return $this->a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @method string getA() */
|
|
|
|
class B extends A {}',
|
|
|
|
],
|
2019-10-01 04:07:20 +02:00
|
|
|
'immutableClassWithCloneAndPropertyChange' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-01 04:07:20 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
protected string $bar;
|
|
|
|
|
|
|
|
public function __construct(string $bar) {
|
|
|
|
$this->bar = $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withBar(string $bar): self {
|
|
|
|
$new = clone $this;
|
|
|
|
$new->bar = $bar;
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'immutableClassWithCloneAndPropertyAppend' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-01 04:07:20 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
protected string $bar;
|
|
|
|
|
|
|
|
public function __construct(string $bar) {
|
|
|
|
$this->bar = $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withBar(string $bar): self {
|
|
|
|
$new = clone $this;
|
|
|
|
$new->bar .= $bar;
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-12-12 15:32:33 +01:00
|
|
|
'memoizeImmutableCalls' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-12 15:32:33 +01:00
|
|
|
function takesString(string $s) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class DTO {
|
|
|
|
/** @var string|null */
|
|
|
|
private $error;
|
|
|
|
|
|
|
|
public function __construct(?string $error) {
|
|
|
|
$this->error = $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getError(): ?string {
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$dto = new DTO("BOOM!");
|
|
|
|
|
|
|
|
if ($dto->getError()) {
|
|
|
|
takesString($dto->getError());
|
|
|
|
}'
|
|
|
|
],
|
2020-01-03 17:29:45 +01:00
|
|
|
'allowConstructorPrivateUnusedMethods' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-03 17:29:45 +01:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class PaymentShared
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
private $commission;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->test();
|
|
|
|
$this->commission = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function test(): void {}
|
|
|
|
}'
|
|
|
|
],
|
2020-02-22 00:25:35 +01:00
|
|
|
'canPassImmutableIntoImmutable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 00:25:35 +01:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Item {
|
|
|
|
private int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-mutation-free */
|
|
|
|
public function get(): int {
|
|
|
|
return $this->i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Immutable {
|
|
|
|
private $item;
|
|
|
|
|
|
|
|
public function __construct(Item $item) {
|
|
|
|
$this->item = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(): int {
|
|
|
|
return $this->item->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = new Item(5);
|
|
|
|
new Immutable($item);',
|
|
|
|
],
|
2020-02-22 16:04:38 +01:00
|
|
|
'preventNonImmutableTraitInImmutableClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 16:04:38 +01:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
trait ImmutableTrait {
|
|
|
|
public int $i = 0;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
final class NotReallyImmutableClass {
|
|
|
|
use ImmutableTrait;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'preventImmutableClassInheritingMutableParent' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 16:04:38 +01:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class ImmutableParent {
|
|
|
|
public int $i = 0;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
final class ImmutableClass extends ImmutableParent {}',
|
|
|
|
],
|
2020-03-09 14:45:35 +01:00
|
|
|
'passDateTimeZone' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-09 14:45:35 +01:00
|
|
|
echo (new DateTimeImmutable("now", new DateTimeZone("UTC")))->format("Y-m-d");'
|
|
|
|
],
|
2020-03-14 06:09:12 +01:00
|
|
|
'allowPassingCloneOfMutableIntoImmutable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-14 06:09:12 +01:00
|
|
|
class Item {
|
|
|
|
private int $i = 0;
|
|
|
|
|
|
|
|
public function mutate(): void {
|
|
|
|
$this->i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-mutation-free */
|
|
|
|
public function get(): int {
|
|
|
|
return $this->i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Immutable {
|
|
|
|
private Item $item;
|
|
|
|
|
|
|
|
public function __construct(Item $item) {
|
|
|
|
$this->item = clone $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(): int {
|
|
|
|
return $this->item->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = new Item();
|
|
|
|
new Immutable($item);',
|
|
|
|
],
|
2020-06-11 17:58:31 +02:00
|
|
|
'noCrashWhenCheckingValueTwice' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-11 17:58:31 +02:00
|
|
|
/**
|
|
|
|
* @psalm-template T
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
abstract class Enum {
|
|
|
|
/** @var T */
|
|
|
|
private $value;
|
|
|
|
|
|
|
|
/** @param T $value */
|
|
|
|
public function __construct($value) {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
public function getValue() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends Enum<string>
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class TestEnum extends Enum {
|
|
|
|
public const TEST = "test";
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(TestEnum $e): void {
|
|
|
|
if ($e->getValue() === TestEnum::TEST
|
|
|
|
&& $e->getValue() === TestEnum::TEST
|
|
|
|
) {}
|
|
|
|
}'
|
|
|
|
],
|
2020-08-25 01:24:27 +02:00
|
|
|
'allowMutablePropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
class B {
|
|
|
|
public int $j = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlusOther(B $b) : int {
|
|
|
|
return $this->i + $b->j;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-09-03 21:28:09 +02:00
|
|
|
'allowPassingMutableIntoImmutable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-03 21:28:09 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class Immutable {
|
|
|
|
private $item;
|
|
|
|
|
|
|
|
public function __construct(Item $item) {
|
|
|
|
$this->item = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(): int {
|
|
|
|
return $this->item->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Item {
|
|
|
|
private int $i = 0;
|
|
|
|
|
|
|
|
public function mutate(): void {
|
|
|
|
$this->i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-mutation-free */
|
|
|
|
public function get(): int {
|
|
|
|
return $this->i;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2021-12-30 22:29:36 +01:00
|
|
|
'allowMutationFreeCallInMutationFreeContext' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-30 22:29:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-mutation-free
|
|
|
|
*/
|
|
|
|
function getData(): array {
|
|
|
|
/** @var mixed $arr */
|
|
|
|
$arr = $GLOBALS["cachedData"] ?? [];
|
|
|
|
|
|
|
|
return is_array($arr) ? $arr : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-mutation-free
|
2021-12-30 22:39:30 +01:00
|
|
|
* @return mixed
|
2021-12-30 22:29:36 +01:00
|
|
|
*/
|
2021-12-30 22:39:30 +01:00
|
|
|
function getDataItem(string $key) {
|
2021-12-30 22:29:36 +01:00
|
|
|
return getData()[$key] ?? null;
|
|
|
|
}',
|
|
|
|
],
|
2019-08-30 18:36:35 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:array<string>,php_version?:string}>
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2019-08-30 18:36:35 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'immutablePropertyAssignmentInternally' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-30 18:36:35 +02:00
|
|
|
/**
|
2019-09-01 00:43:39 +02:00
|
|
|
* @psalm-immutable
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
public function __construct(int $a, string $b) {
|
|
|
|
$this->a = $a;
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setA(int $a): void {
|
|
|
|
$this->a = $a;
|
|
|
|
}
|
|
|
|
}',
|
2019-09-09 17:14:40 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2019-08-30 18:36:35 +02:00
|
|
|
],
|
|
|
|
'immutablePropertyAssignmentExternally' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-30 18:36:35 +02:00
|
|
|
/**
|
2019-09-01 00:43:39 +02:00
|
|
|
* @psalm-immutable
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
public function __construct(int $a, string $b) {
|
|
|
|
$this->a = $a;
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A(4, "hello");
|
|
|
|
|
|
|
|
$a->b = "goodbye";',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
|
|
|
'callImpureFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-30 18:36:35 +02:00
|
|
|
/**
|
2019-09-01 00:43:39 +02:00
|
|
|
* @psalm-immutable
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
public function __construct(int $a, string $b) {
|
|
|
|
$this->a = $a;
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {
|
|
|
|
header("Location: https://vimeo.com");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImpureFunctionCall',
|
|
|
|
],
|
|
|
|
'callExternalClassMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-30 18:36:35 +02:00
|
|
|
/**
|
2019-09-01 00:43:39 +02:00
|
|
|
* @psalm-immutable
|
2019-08-30 18:36:35 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct(string $a) {
|
|
|
|
$this->a = $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getA() : string {
|
|
|
|
return $this->a;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function redirectToA() : void {
|
|
|
|
B::setRedirectHeader($this->getA());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public static function setRedirectHeader(string $s) : void {
|
|
|
|
header("Location: $s");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImpureMethodCall',
|
|
|
|
],
|
2019-09-22 04:52:30 +02:00
|
|
|
'mustBeImmutableLikeInterfaces' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-22 04:52:30 +02:00
|
|
|
/** @psalm-immutable */
|
|
|
|
interface SomethingImmutable {
|
|
|
|
public function someInteger() : int;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MutableImplementation implements SomethingImmutable {
|
|
|
|
private int $counter = 0;
|
|
|
|
public function someInteger() : int {
|
|
|
|
return ++$this->counter;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingImmutableAnnotation',
|
|
|
|
],
|
|
|
|
'inheritImmutabilityFromParent' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-22 04:52:30 +02:00
|
|
|
/** @psalm-immutable */
|
|
|
|
abstract class SomethingImmutable {
|
|
|
|
abstract public function someInteger() : int;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MutableImplementation extends SomethingImmutable {
|
|
|
|
private int $counter = 0;
|
|
|
|
public function someInteger() : int {
|
|
|
|
return ++$this->counter;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingImmutableAnnotation',
|
|
|
|
],
|
2020-02-22 16:04:38 +01:00
|
|
|
'preventNonImmutableTraitInImmutableClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 16:04:38 +01:00
|
|
|
trait MutableTrait {
|
|
|
|
public int $i = 0;
|
|
|
|
|
|
|
|
public function increment() : void {
|
|
|
|
$this->i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
final class NotReallyImmutableClass {
|
|
|
|
use MutableTrait;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MutableDependency'
|
|
|
|
],
|
|
|
|
'preventImmutableClassInheritingMutableParent' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 16:04:38 +01:00
|
|
|
class MutableParent {
|
|
|
|
public int $i = 0;
|
|
|
|
|
|
|
|
public function increment() : void {
|
|
|
|
$this->i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
final class NotReallyImmutableClass extends MutableParent {}',
|
|
|
|
'error_message' => 'MutableDependency'
|
|
|
|
],
|
2020-07-23 07:48:06 +02:00
|
|
|
'mutationInPropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-23 07:48:06 +02:00
|
|
|
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 {
|
2020-07-23 08:40:35 +02:00
|
|
|
$this->s = "hello";
|
2020-07-23 07:48:06 +02:00
|
|
|
return substr($this->s, 0, 5);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ImpurePropertyAssignment',
|
|
|
|
],
|
2020-07-24 22:01:45 +02:00
|
|
|
'mutationInPropertyConcat' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-24 22:01:45 +02:00
|
|
|
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',
|
|
|
|
],
|
2020-08-26 18:18:34 +02:00
|
|
|
'preventUnset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 18:18:34 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
public function __construct(string $b) {
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A("hello");
|
|
|
|
unset($a->b);',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
2019-08-30 18:36:35 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|