https://redberry.international/wp-content/uploads/2022/04/Linting-A-Laravel-Project-Using-PHP-CS-Fixer.png
Developers, deliberately or unknowingly, build their coding standards in the process of project development. A coding standard is a style that is applied to your whole codebase to shape a coding discipline and make the codebase more consistent and readable.
Why is it so important to implement a coding style?
The first and foremost essential factor is related to the discipline of coding. Shaping discipline and creating a coding style makes it easier not only just to understand the existing code but also makes it easier to develop the codebase further.
On top of that, discipline with the coding style is also crucial for effective collaboration within the development team. This attitude makes teamwork much more straightforward and efficient. Besides, that way, the new developers will be able to comprehend the code easily. It will also help developers not to get upset with individual code style preferences.
However, we should also note that maintaining the coding standards with bare hands might be a highly complex task requiring time and effort. Therefore, there is a Linter concept in almost every programming language. With this article, we will dig down to understand Linting and how you can apply it to your Laravel Project by using PHP CS Fixer. So let’s get started.
What is Linter?
Linter is a common tool that helps us develop a specific coding style and find errors.
In most cases, Linters are used in conjunction with the IDE. Linters are activated once a specific event fires and suggest a correction. You can also run Linter using a CLI to highlight and/or fix the errors in your codebase.
Does PHP have a Linter?
The short answer is Yes.
Nowadays, Laravel is the most popular framework in the PHP world. However, Laravel itself frequently uses the components of the other very popular framework – Symfony. The founder of Symfony, Fabien Potencier, created a PHP Linter called CS Fixer. So let’s talk more about it.
How Can we Install CS Fixer in the Laravel Project?
There are several ways to install CS Fixer:
- Install globally using Composer
- Install globally without using Composer
- Install per project, using the Composer
In the following example, we will discuss how we can set up PHP CS Fixer in a per-project approach and connect it to VSCode and PHPStorm. Connecting to the other IDEs is a similar process.
First of all, we need to go to our Laravel project in the terminal and install PHP CS Fixer with Composer by running the following command:
composer require friendsofphp/php-cs-fixer --dev
Then if we see executable files in our project /vendor/bin directory we will see that one of them is php-cs-fixer.
If we run this command in the root folder of our project:
./vendor/bin/php-cs-fixer
We will see that CSFixer has been installed successfully and will display the following:
We made sure that PHP CS Fixer really works 🙏
CS Fixer in the VSCode
Let’s move to the VSCode.
First, we will need to install the appropriate extension for CS Fixer in VSCode.
Pay attention to the fact that the author of this extension should be Junstyle.
Then we should create a .vscode folder in our project and a settings.json file in it where we will set configurations:
There are three things we need to configure:
- Run PHP Formatter on save
- Tell VSCode the address of php-cs-fixer (/vendor/bin/php-cs-fixer) executable
- Tell from where the php-cs-fixer configuration file should be read
Consequently, JSON configuration will look like this:
The only thing left is to create a .php-cs-fixer.php file in the root folder. We can set up formatting rules with that configuration file. Here is an example rule configuration file we use for some of our projects: PHP CS Fixer Config File.
CS Fixer in the PHPStorm
Open the desired project in PHPStorm and go into the PHPStorm settings:
Then we should find the Quality Tools under PHP; this is where the PHP CS Fixer section should be:
Click the button with the three points and point out our CS Fixer binary file address which is located in the vendor/bin folder:
Click Validate and make sure that everything works fine.
Then we should move to CS Fixer Inspection and set several parameters:
We should mark PHP CS Fixer validation. Severity should be set to Error, and we should choose Custom in the Ruleset. This means that we need to give PHP CS Fixer our rule file which defines how our codebase should be formatted.
Before we configure which file should be used as a Ruleset by PHPStorm, we should create a .php-cs-fixer.php file in our Root folder. Here is an example configuration file we use for some of our projects: PHP CS Fixer Config File.
Then we should navigate to Inspections and find our newly created configuration file in PHPStorm:
Then we can save our configuration, which means that PHPStorm will already start analyzing our codebase and format it according to our ruleset.
However, this is still not enough. For the development process to be smooth, it would be better for PHP CS Fixer to make autocorrections. This is why we need to add the File Watcher.
For this we should navigate to File Watchers in the PHPStorm settings.
Now we should choose Custom in Templates and write a new Watcher configuration.
Name: PHP CS Fixer
File Type: PHP
Program: [Your Project Path]/vendor/bin/php-cs-fixer
Arguments: fix –config=$ProjectFileDir$/.php-cs-fixer.php –cache-file=$ProjectFileDir$/.php-cs-fixer.cache $FileDir$/$FileName$
Also, we should turn off the first two checkboxes in the Advanced Options:
Click the OK button, and the Watcher File will be added.
After that, our codebase will be auto-formatted using PHP CS Fixer every time we save the files.
The first time you CMD+S, PHPStorm will alert you to approve running the Watcher.
Click Trust Project and Run once you see the Alert box.
And that’s it 😉
Laravel News Links