Skip to content

UnusedConstructorDependencies ​

info on by default

Group: Unused · PhpStorm name: UnusedConstructorDependenciesInspection

A private property that is assigned in the constructor but never touched by any other method of the class (or its traits) is dead state — typically a leftover injected dependency. Point at the constructor assignments.

Example ​

php
<?php
trait Describes
{
    public function describe() { return $this->label; }
}

class Mailer
{
    use Describes;

    private $transport;
    private $logger;
    private $label;
    private $retries;
    private $hook;
    private $handler;
    public $onError;
    /** @Inject */
    private $clock;
    /** @var int */
    private $limit;
    protected $debug;

    public function __construct($transport, $logger, $label, $clock, $limit, $handler)
    {
        $this->transport = $transport;
        $this->logger = $logger;
        $this->logger->info('ready');
        $this->label = $label;
        $this->retries = 3;
        $this->retries = $this->retries + 1;
        $register = function () { $this->hook = true; };
        $this->onError = fn($e) => $this->handler->handle($e);
        $this->handler = $handler;
        $this->clock = $clock;
        $this->limit = $limit;
        $this->debug = false;
    }

    public function send($msg)
    {
        return $this->transport->push($msg);
    }

    public static function inspect(Mailer $other)
    {
        return function () use ($other) { return $other->hook; };
    }
}

Reported:

  • line 27: Private property is only used in the constructor; likely dead code.
  • line 30: Private property is only used in the constructor; likely dead code.
  • line 31: Private property is only used in the constructor; likely dead code.
  • line 36: Private property is only used in the constructor; likely dead code.

Configure ​

In custos.json:

json
{
  "rules": {
    "UnusedConstructorDependencies": {
      "enabled": false
    }
  }
}

Suppress ​

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

php
// @custos-ignore UnusedConstructorDependencies

/**
 * @noinspection UnusedConstructorDependenciesInspection
 */

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