Prefer constructor over always called setters

Installs: 146

Dependents: 0

Suggesters: 0

Security: 0

Stars: 9

Watchers: 0

Forks: 0

Open Issues: 0

Type:phpstan-extension

0.0.6 2025-07-12 06:32 UTC

This package is auto-updated.

Last update: 2025-07-12 06:32:54 UTC


README

Downloads

If you can use constructor instead of setters, use it. These PHPStan rules will help you to find such cases.


What It Does?

This tool collects instances of new object() followed by a series of method calls on the same object:

$human = new Human();
$human->setName('Tomas');
$human->setAge(35);

...and suggests turning them into constructor arguments:

$human = new Human('Tomas', 35);

Why?

Such chained setters often indicate implicit required dependencies. By moving them to the constructor, you make the object state explicit, safer, and easier to reason about — and even easier to test.


Installation

composer require tomasvotruba/ctor --dev

Usage

Make use phpstan/extension-installer to load the extension automatically.

Run PHPStan and it will automatically run the rules.


Happy coding!