Unnecessary Default Constructor
ID |
php.unnecessary_default_constructor |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Php |
Tags |
code_smell, redundant-code |
Description
Reports a public, parameterless constructor that does no work — either an empty body or a body
whose only statement is an argument-less parent::__construct() call.
Rationale
PHP instantiates a class with new whether or not the class declares a constructor, so a
parameterless constructor with an empty body adds a member to read and maintain without
changing behaviour. A body that contains only parent::__construct() with no arguments is the
same: PHP does not call the parent constructor implicitly, but a no-argument forwarding call
adds nothing the reader can act on. Removing the empty constructor reduces clutter and makes a
later, meaningful constructor easier to spot.
Constructors that declare parameters (including promoted properties), that are private or
protected to restrict instantiation, or that run any other statement are left untouched.
<?php
class Account
{
public function __construct() // FLAW — empty parameterless constructor
{
}
}
class Repository
{
public function __construct(private Connection $connection) // OK — promoted property
{
}
}