Setup Code Quality tools for PHP projects

Setup Code Quality tools for PHP projects

Install and use PHPcs, PHPstan and Pest in your PHP projects (+bonus: a Makefile to launch them)

·

3 min read

When I start a PHP project from scratch, one of the first tasks that I perform is to install and configure come tools for code quality. It involves:

  • Code Sniffer: checks your code is following some standard;

  • Phpstan: a static code analysis tool that aims to find potential bug in your code;

  • PestPHP: for executing tests.

Code Sniffer

When a team of developers, work on the same codebase, I suggest adopting, and following a coding standard. A coding standard define rules about how to format PHP code. In this way, you can achieve uniformity, readability, and maintainability within your code. You can decide which standard to follow. Typically, I adopt and follow PSR12 standard. To install the PHP package with the tool phpcs to perform the check:

composer require --dev squizlabs/php_codesniffer

Usually, I install Code Sniffer in the require-dev section, thanks to the --dev flag. After the composer command, you can launch phpcs with the standard PSR12:

vendor/bin/phpcs --standard=PSR12 src tests examples

In this example I want to check 3 directories (and subdirectories): src, tests, examples directories.

Phpstan

Phpstan is a tool that can support you and help you find some type of bugs. It checks:

  • type hints;

  • the accessibility of methods, properties, and functions;

  • the number and the type of parameters;

  • methods return type coherence;

  • variables declaration, initializations and scope;

  • casts check;

  • ... etc...

To install it:

composer require --dev phpstan/phpstan

To execute it

vendor/bin/phpstan analyse -l 6 app

Where:

  • analyse is the command for phpstan;

  • -l 6 is the level for the check;

  • app is the directory to analyze (you can specify more than 1 directory). Te rule levels are explained here: phpstan.org/user-guide/rule-levels. Usually I start from level 0, and after I fixed the issues, I execute again phpstan with the level incremented by 1. Typically, the minimum level I want to reach is 6. The more important thing is to set the level for your project, and you share it with the team. If you are working in a team, I suggest defining the phpstan.neon file that includes the directive for the execution of phpstan. So, instead of launch phpstan with parameters and options in the command line, for consistency, I recommend you to define the configuration file and push it on the repository. A very basic version of the file:

parameters:
    level: 6
    paths:
        - app

If you save it in the root of the project (naming it phpstan.neon), you can execute:

vendor/bin/phpstan

PestPHP

To install PestPHP:

composer require pestphp/pest --dev --with-all-dependencies

to setup PestPHP, I'm going to create tests directory (where to store my tests) and initialize PestPHP:

mkdir tests
./vendor/bin/pest --init

The init command it creates:

  • phpunit.xml file, under the hood, PestPHP uses PHPunit;

  • tests/Pest.php file to bootstrap correctly PestPHP;

  • tests/ExampleTest.php a first example of test.

The ExampleTest.php is very simple:

<?php

test('example', function () {
    expect(true)->toBeTrue();
});

Launch code quality via Makefile

As you can see, now you have 3 commands to launch in order to check your code. Usually, within my projects, I have a Makefile to make my life easier. I'm going to copy and paste my typical (and basic) Makefile:

.PHONY : help phpstan test phpcs
.DEFAULT_GOAL:=help

help:           ## Show this help.
    @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

phpstan: ## Execute phpstan
    vendor/bin/phpstan analyse -c ./phpstan.neon --no-progress

test: ## Execute phpunit
    vendor/bin/pest

phpcs: ## execute phpcs
    vendor/bin/phpcs --standard=PSR12 app

phpfix: ## Fix some warnings from phpcs
    vendor/bin/phpcbf --standard=PSR12  app

allcheck: phpcs phpstan test ## it performs all check (phpcs, phpstan, tests)

With this Makefile, you can execute:

make allcheck

This is my starting point, for my PHP projects. If you want to give feedbacks, you are welcome!