1.2.1376. Useless Assignation Of Promoted Property

Promoted properties save the assignation of constructor argument to the property. It is useless to do it with that syntax, and in the constructor too.

<?php

class x {
     private $b;

     function __construct(private $a,
                                              $b,
                                              ) {
             // This is already done with the promoted property
             $this->a = $a;

             // This is the traditional way (up to PHP 8.0)
             $this->b = $b;
             }
}

?>

1.2.1376.1. Suggestions

  • Remove the assignation in the constructor

1.2.1376.2. Specs

Short name

Classes/UselessAssignationOfPromotedProperty

Rulesets

All, Analyze, Changed Behavior, Class Review

Exakat since

2.5.0

PHP Version

With PHP 8.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

promoted-property

Available in

Entreprise Edition, Exakat Cloud