Two useful tools for automatic PHP code improvement - ECS and Rector

2019-06-23 | 181 words

The first tool is Easy Coding Standard. You can install it via composer (either in your project or globally) and simply run it in the console:

vendor/bin/ecs check src --level clean-code --fix

The example above runs ECS for the src directory, using the –level option to specify what should be fixed (there are many pre-configured options) and the –fix option to actually fix the code, not just display the issues.

Another interesting tool is Rector. It is presented as a tool for upgrading or migrating between frameworks, but it can also do other things - for example, remove “dead code” or code that will never be executed. You can install it via composer and run it like this:

vendor/bin/rector process src --level dead-code

The example above runs Rector on the src directory and fixes the code. If you add the –dry-run option, the code will not be fixed and you will be able to see the results of the operation first.

To see a list of available configurations, you can run:

vendor/bin/rector sets

UPDATE: use “sets” instead of “levels” now :)