Rubriky
Development

PHP CI [1/2]

Describing benefits of writing clean code is far beyond the scope of this article. What I am going to describe is which tools do I usually use as a „boilerplate“ for a new PHP project to make sure my code is as clean and smell-less as possible.

PHP CodeSniffer

Most of the time, source code has a lot of „code-smells“, developers have different habbits and sometimes even our focus during the code review is not 100%. We need to make sure, that the source code is consistent, keeping certain standards etc.

For this purpose I use PHP CodeSniffer with the well known Slevomat standards set, which is based on the consistence coding standard. The installation of this tool is not difficult. First, we need to install the CodeSniffer itself:

composer require --dev squizlabs/php_codesniffer

Then make sure that Slevomat rules are also added as a dependency (they might be already required by another package, but is definitelly should be also required by your project itself). Using Composer it is done like this:

composer require --dev slevomat/coding-standard

This combination needs a little configuration. I will explain the setup I use more in another article, but the basic are described here: https://github.com/slevomat/coding-standard

PHPLint

Since most of us usually use complex IDEs as PhpStorm, Atom etc., this tools may seem a little useless today. But how do you make sure your syntax does not contain any errors in your pipeline in CI?

I personally prefer php-parallel-lint/PHP-Parallel-Lint (originally JakubOnderka/PHP-Parallel-Lint) since the setup is really easy and works quite fast.

Since I usually use composer, all I need to do is to install the package:

composer require --dev php-parallel-lint/php-parallel-lint

To run the check itself, all you need to do is to run:

vendor/bin/parallel-lint --exclude vendor .

PHPStan

PHP is not compiled language, thus many errors can occur on runtime, which might not be the most suitable time to find the error. To prevent the most of the obvious problems, there is a tool exactly for that purpose. It is called PHPStan – PHP Static Analysis Tool and if you do to know about it yet, it is definitelly time to change that.

Matching typehints, missing params, errors in dead code… all those things can PHPStan do for you. How to install that powerful tool? Easily as usual:

composer require --dev phpstan/phpstan

And how to run it? Even easier, but let’s take a look at the „level“ of the analysis. What I can recommend – the higher, the better. Level 8 will make sure you use the most strict level of safety (including null safety acces). To run the analysis like that, run:

php vendor/bin/phpstan analyse src --level 8

What else?

In the second part of the article I will briefly describe PHPUnit and s little less known (but not less important) Deptrac. We will also take a look at setting up Composer scripts to tun all the tools easily using one command.

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *