Skip to content

PropertyInitializationFlaws ​

info on by default quick-fix

Group: Unused · PhpStorm name: PropertyInitializationFlawsInspection

Property defaults and constructor assignments that do nothing: an explicit = null default (untyped properties are already null), a default that merely repeats the inherited one, a default on a private property that the constructor unconditionally overwrites, and a constructor assignment that writes exactly the default value.

Example ​

php
<?php
namespace Shop;

class Tag {}

class BaseCart
{
    protected $items = [];
    private $secret = 'x';
    public static $currency = 'EUR';
    protected $tagClass = Tag::class;
    protected $notes = NULL;
}

class Cart extends BaseCart
{
    protected $items = [];
    private $secret = 'x';
    public static $currency = 'EUR';
    protected $tagClass = Tag::class;
    protected $notes = 'n/a';

    private $total = 0;
    private $lines = [];
    private $count = 0;
    private $owner;
    private $history = [];
    private $later = 'a';
    protected $shown = 1;

    public function __construct($owner)
    {
        $this->total = 100;
        $this->lines = [];
        $this->owner = null;
        $this->count = $this->count + 1;
        $this->history = array_merge($this->history, [$owner]);
        if ($owner) {
            $this->later = 'b';
        }
        $this->shown = 2;
    }

    public function reset()
    {
        $this->lines = [];
    }
}
php
<?php
namespace Shop;

class Tag {}

class BaseCart
{
    protected $items = [];
    private $secret = 'x';
    public static $currency = 'EUR';
    protected $tagClass = Tag::class;
    protected $notes;
}

class Cart extends BaseCart
{
    protected $items = [];
    private $secret = 'x';
    public static $currency = 'EUR';
    protected $tagClass = Tag::class;
    protected $notes = 'n/a';

    private $total;
    private $lines = [];
    private $count = 0;
    private $owner;
    private $history = [];
    private $later = 'a';
    protected $shown = 1;

    public function __construct($owner)
    {
        $this->total = 100;
        $this->lines = [];
        $this->owner = null;
        $this->count = $this->count + 1;
        $this->history = array_merge($this->history, [$owner]);
        if ($owner) {
            $this->later = 'b';
        }
        $this->shown = 2;
    }

    public function reset()
    {
        $this->lines = [];
    }
}

Reported:

  • line 12: Explicit null default is redundant; remove it.
  • line 17: Default repeats the inherited value; drop the re-declaration.
  • line 20: Default repeats the inherited value; drop the re-declaration.
  • line 23: Default is always replaced by the constructor; remove it.
  • line 34: Assignment writes the property's default value; remove it.
  • line 35: Assignment writes the property's default value; remove it.

Options ​

OptionTypeDefaultEffect
REPORT_DEFAULTS_FLAWSbooltrueEnables Check 1 (N, S) and pattern O of Check 2.
REPORT_INIT_FLAWSbooltrueEnables Check 2 (W, and O when the other option is on).

Configure ​

In custos.json:

json
{
  "rules": {
    "PropertyInitializationFlaws": {
      "enabled": false,
      "options": {
        "REPORT_DEFAULTS_FLAWS": true,
        "REPORT_INIT_FLAWS": true
      }
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore PropertyInitializationFlaws

/**
 * @noinspection PropertyInitializationFlawsInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.