Skip to content

PropertyCanBeStatic ​

info off by default

Group: Architecture · PhpStorm name: PropertyCanBeStaticInspection

A non-public instance property initialised with a sizeable array literal (several nested arrays or strings) is copied into every object. When such data is really per-class configuration it is cheaper as a static property — or, on PHP 5.6+, as a class constant.

Example ​

At PHP 8.x:

php
<?php
class Base {
    protected $routes = [];
}

class Router extends Base {
    private $verbs = ['GET', 'POST', 'PUT', 'DELETE'];
    protected $matrix = array('a' => [1], 'b' => [2], 7, 'c' => "x");
    private array $mixed = [1, 'one', 2, <<<TXT
two
TXT, 3, ['three']];

    protected $routes = ['a', 'b', 'c'];
    private $pair = ['x', 'y'];
    private $nested = [['a', 'b', 'c', 'd']];
    public $open = ['p', 'q', 'r'];
    var $legacy = ['p', 'q', 'r'];
    private static $shared = ['p', 'q', 'r'];
    private $numbers = [1, 2, 3, 4];
    const NAMES = ['p', 'q', 'r'];
}

Reported:

  • line 7: Large array default on an instance property; consider a static property or a class constant.
  • line 8: Large array default on an instance property; consider a static property or a class constant.
  • line 9: Large array default on an instance property; consider a static property or a class constant.

Configure ​

In custos.json:

json
{
  "rules": {
    "PropertyCanBeStatic": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore PropertyCanBeStatic

/**
 * @noinspection PropertyCanBeStaticInspection
 */

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