1.2.134. Class Could Be Readonly

When all properties are readonly, it is possible to set the option at the class. This feature was introduced in PHP 8.2.

<?php

// This class could be readonly
class x {
     private readonly A $p;
     private readonly A $p2;
}

?>

See also PHP 8.2: readonly Classes.

1.2.134.1. Suggestions

  • Remove readonly from the properties, and add it to the classes.

1.2.134.2. Specs

Short name

Classes/CouldBeReadonly

Rulesets

All, Class Review, Suggestions

Exakat since

2.5.1

PHP Version

With PHP 8.2 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

readonly

Available in

Entreprise Edition, Exakat Cloud