tomasvotruba / ctor
Prefer constructor over always called setters
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 146
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 0
Forks: 0
Open Issues: 0
Type:phpstan-extension
Requires
- php: ^7.4|^8.0
- phpstan/phpstan: ^2.1.14
- webmozart/assert: ^1.11
README
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!