Skip to content

ClassOverridesFieldOfSuperClass ​

warning off by default

Group: Architecture · PhpStorm name: ClassOverridesFieldOfSuperClassInspection

Re-declaring a property that an ancestor already declares is usually accidental: for protected/public properties it merely duplicates the declaration (often with a diverging default), and for an ancestor's private property it creates a second, unrelated property with the same name.

Example ​

Default options:

php
<?php
trait Tracks { private $trail; }

class Model {
    use Tracks;
    private $secret;
    protected $table;
    protected $visible;
    public $id;
    protected static $registry;
    const KIND = 'model';
}
class Record extends Model {
    protected $dirty;
}

/** @property string $virtual */
class Invoice extends Record {
    protected $table = 'invoices';
    protected $dirty;
    public $id;
    public $secret;
    private $trail;

    public $visible;
    protected static $registry;
    const KIND = 'invoice';
}

class InvoiceTest extends Record {
    protected $table;
}

class Entity { protected $uuid; }
class Order extends Entity {
    /** @Column(type="guid") */
    protected $uuid;
}

class Locked {
    protected $state;
    final protected function __construct($seed = 1) {}
}
class Door extends Locked {
    protected $state;
}

Reported:

  • line 19: Property 'table' is already declared in \Model; drop this re-declaration.
  • line 20: Property 'dirty' is already declared in \Record; drop this re-declaration.
  • line 21: Property 'id' is already declared in \Model; drop this re-declaration.
  • line 22: \Model already has a private property with this name; consider a different name.
  • line 23: \Tracks already has a private property with this name; consider a different name.

Options ​

OptionTypeDefaultEffect
REPORT_PRIVATE_REDEFINITIONbooltrueAlso report re-declarations of an ancestor's (or ancestor trait's) private property (D5).

Configure ​

In custos.json:

json
{
  "rules": {
    "ClassOverridesFieldOfSuperClass": {
      "enabled": true,
      "options": {
        "REPORT_PRIVATE_REDEFINITION": true
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore ClassOverridesFieldOfSuperClass

/**
 * @noinspection ClassOverridesFieldOfSuperClassInspection
 */

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