Code quality tools : PHP-CS-Fixer

Salwa achek
2 min readJun 1, 2024

What's PHP-CS-Fixer ?

PHP-CS-Fixer is a command-line tool for automatically fixing PHP code standards issues. It analyzes PHP codebase and applies coding standards such as PSR-1, PSR-2, and more to the code.

In this tutorial we will try to set up PHP-CS-fixer on Visual Studio Code.

* Installation :

Editors require PHP-CS-Fixer to be pre-installed so we will do that through the command below using Composer and globally :

> composer global require friendsofphp/php-cs-fixer --dev

* Setting up PHP-CS-Fixer with Visual Studio Code

— To install the plugin:

  • Open Extensions on VS Code ( ctrl + shift + x ) and Type “ php-cs-fixer” to find the extension and Click on install to install.

— VSCode Settings:

  • Through the GUI :

In VS Code go to file >preferences >settings >extensions (ctrl + , ) and configure the extension settings.

Set the path to the PHP-CS-Fixer binary and customize rules according to your project’s needs.

The binary should be in the composer folder : /Composer/vendor/bin

You can check the composer folder path using this command:

> phpcs --config-set

Check for home path :

In our case the binary is in : C:\Users\Lenovo\AppData\Roaming\Composer\vendor\bin\php-cs-fixer

Now we can add that path to the field executablePath

  • Through the settings file :

You can change the file settings.json, for windows the file should be somewhere in :

C:\Users\ {user}\AppData\Roaming\Code\User

"php-cs-fixer.executable": "C:\\Users\\Lenovo\\AppData\\Roaming\\Composer\\vendor\\b

* Usage in VSCode

  • On-the-Fly Linting:

While you are writing your code VSCode will be in real-time checking for standards violations and giving you feedback.

  • Manual Formatting:

You can manually format the code by clicking Shift+Alt+F to manually format the entire document according to PHP-CS-Fixer rules.

  • Automatic Formatting on Save:

You can enable automatic formatting on save by adding this to your file settings.json:

"editor.formatOnSave": true

Conclusion

Using PHP-CS-Fixer helps developers to maintain the consistency of the code. so much can be done without effort. Its definitely a tool to use.

--

--

Salwa achek

A dedicated and enthusiastic backend developer with a commitment to get better everyday and a deep passion for continuous learning and self improvement.