What Is PHP CS Fixer and How It Can Help You Keep Your Code Cleaner

Software Engineer, Content Creator, and Open-Source contributor
Search for a command to run...

Software Engineer, Content Creator, and Open-Source contributor
No comments yet. Be the first to comment.
In this series, you can find content on software engineering including web development and related technologies.
Idempotent HTTP methods An idempotent HTTP method is a method that can be called multiple times with the same input and produce the same result, without changing the state of the server. Idempotency essentially means that the result of a successfull...
I haven't updated my GitHub page for a while and decided to give it a new look. It turned out to be very simple, and it's good if you want your profile to stand out from the millions of others. Also, sometimes recruiters and HR professionals can find...

MVC stands for Model-View-Controller and is a software architecture pattern commonly used to develop software applications. Although originally designed for desktop computing, MVC has been widely adopted as a design for World Wide Web applications in...

Today, more and more people and companies are using TypeScript. It is among the top 10 programming languages in the PYPL index. And this is not surprising, since TypeScript allows you to catch many errors at compile time, not at runtime, that is befo...

When I started working with JavaScript many years ago, I learned that to convert a value to a string, I could use value.toString(). This method worked well and was sufficient to complete the tasks I had at the time. However, as I progressed and start...

If you are new to learning and working with Docker, you might be wondering how Docker images differ from Docker containers. Without further ado, let's dive into it. Both Images and Containers are two distinct concepts in the Docker ecosystem and we n...

PHP CS Fixer stands for PHP Coding Standards Fixer. This is a tool that fixes your code to follow standards. There are various PHP coding standards that you can follow, such as the popular PSR-1, and PSR-12 published by the PHP Framework Interoperability Group (the full list of PSR standards can be found here).
There are also other community-driven standards like the Symphony.
With PHP CS Fixer, you can follow multiple coding standards at once, or you can also define your own or your team's own coding standard through configuration. If you have ever worked with JavaScript and Eslint in particular, think of PHP CS Fixer as an alternative to Eslint for PHP.
Manually fixing coding standards in your code is a very tedious process, so PHP CS Fixer is a great solution. It helps you and your team ensure that you all follow the same rules when writing code, thus keeping it cleaner.
The recommended way to install PHP CS Fixer is to use Composer in a dedicated directory in your project, for example in the tools/php-cs-fixer.
mkdir --parents tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
For more details and other installation methods, see installation instructions.
Assuming you installed PHP CS Fixer as instructed above, you can run the following command to fix the files PHP files in the src directory:
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src
See usage, list of built-in rules, list of rule sets, and configuration file documentation for more details. If you need to apply code styles that are not supported by the tool, you can create custom rules.
However, executing a command manually does not seem to be a convenient approach. I personally prefer to run PHP CS Fixer by pressing a pre-configured keyboard shortcut in PHP Storm.
You can configure PHP CS Fixer for various code editors as described below:
As an alternative option, you can fix your code on every commit using Husky and lint-staged. Using these tools PHP CS Fixer will run on your staged files, in other words, on the files you want to commit. Here is a great article explaining how to set up these tools to work with PHP CS Fixer.
If you are new to PHP Coding Standards and want to quickly configure PHP CS Fixer, I highly recommend using PHP CS Fixer Configurator by Michele Locati. It has a pre-defined set of rules that you can select and create your own configuration.

Most rules, when clicked, will display general information about the rule, available configuration options, and usage examples. The PHP CS Fixer configuration file generated by this tool can be exported to use in your project and then imported again in case you want to do some modifications.

The end. I hope you found this information helpful, stay tuned for more content! :)