1.2.266. Could Be Readonly Property

This property could be made readonly. For that, the property is set in the constructor, and optionally in the __clone magic method, and never modified otherwise.

<?php

class x {
     private int $ok, $ok2;

     function __construct() {
             $this->ok = 1;
             $this->ok2 = 1;
     }

     function getOk2() {
             return $this->ko;
     }

     function __clone() {
             $this->ok2 = 1;
     }
}
?>

1.2.266.1. Suggestions

  • Add the readonly option to the property definition

1.2.266.2. Specs

Short name

Classes/CouldBeReadonlyProperty

Rulesets

All, Changed Behavior, Class Review, Suggestions

Exakat since

2.6.4

PHP Version

With PHP 8.1 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud