If you can use constructor instead of setters, use it. These PHPStan rules will help you to find such cases.
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);
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.
composer require tomasvotruba/ctor --dev
Make use phpstan/extension-installer
to load the extension automatically.
Run PHPStan and it will automatically run the rules.
Happy coding!