1.2.717. Modify Immutable

A class, marked as immutable, is being modified.

This attribute is supported as a PHPdoc comment, @immutable, and as a PHP 8.0 `attribute.

<?php

/** @Immutable */
#[Immutable]
class x {
    public $x = 1, $y, $z;
}

$x = new X;
// $x->x is modified, while it should not
$x->x = 2 + $x->z;

// $x->z is read only, as expected

?>

See also phpstorm-stubs/meta/attributes/Immutable.php and PhpStorm 2020.3 EAP #4: Custom PHP 8 Attributes.

1.2.717.1. Suggestions

  • Removed the modification

  • Clone the immutable object

1.2.717.2. Specs

Short name

Attributes/ModifyImmutable

Rulesets

All, Analyze, Attributes, Changed Behavior

Exakat since

2.2.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

attribute

Available in

Entreprise Edition, Exakat Cloud