1.2.996. Property Cannot Be Readonly

This analysis reports different situations where a property is readonly, and has some impossible code.

Two cases are reported : + a self-updated property, where it is updated with a value that is created from itself. The most obvious is $this->a = `$this <https://www.php.net/manual/en/language.oop5.basic.php>`_->a; (which is reported as an error by PHP), and $this->a = foo(`$this <https://www.php.net/manual/en/language.oop5.basic.php>`_->a); is the most common. + a property which is set in the constructor, yet has a distinct method where it is updated too.

Most of thoses cases are dead code.

<?php

class x {
     private readonly $p;
     private readonly $q;

     function __construct($p) {
             $this->p = $p; // normal assignation
     }

     function foo() {
             $this->q = bar($this->q); // this is not possible with readonly
             $this->q++; // this is not possible with readonly
             $this->q[] = 2; // this is not possible with readonly
     }
}

?>

1.2.996.1. Suggestions

  • Remove the impossible code

1.2.996.2. Specs

Short name

Classes/CannotBeReadonly

Rulesets

All, Changed Behavior, Class Review

Exakat since

2.6.1

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

readonly, dead-code

Available in

Entreprise Edition, Exakat Cloud