PHP Quality Tools package
Tags: article, tutorial, cleancode, codequality
Published: 2019-10-12 22:14:45

---
title: PHP Quality Tools package
published: true
date: 2019-10-12 21:44:20 UTC
tags: Article,Tutorial,cleancode,codequality
canonical_url: https://www.danielwerner.dev/php-quality-tools-package
---

## Introduction

If we’d like to write error free, robust, high quality PHP code, the static code analyzers can be of great help. If you work on open-source projects you can take the advantage of using [Scrutinizer](https://scrutinizer-ci.com/) and [StyleCI](https://styleci.io/) for these purposes. However if you are working on other, proprietary projects, you might need to run/configure these tool yourself. There are plenty of such tools to use with php, you can find quite comprehensive lists on other websites, for example [here](https://phpqa.io/index.html) and [here](https://thevaluable.dev/code-quality-check-tools-php/).   
My tools of choice are the following:

- [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer) – check the code according to the defined coding standard
- [PHP Coding Standards Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) – automatically fix some of the problems detected by the code sniffer
- [PHP Static Analysis Tool](https://github.com/phpstan/phpstan) – detects the errors in the code without actually running it
- [PHP Mess Detector](https://phpmd.org/) – detects potential problems in the code like bugs, non optimal code, etc.

## Why I created the [package](https://github.com/daniel-werner/php-quality-tools)?

As I am working on multiple projects (shorter and longer term projects also), I wanted a way to quickly set up all the above mentioned tools, with a predefined configuration. I also checked the awesome [PHP Insights package](https://phpinsights.com/), which has all the features I need. In my tests the other tools’ performance was better, and I wanted to use some more mature tools, so I decided to write my own package.

## Features

This is basically a helper package, therefore it doesn’t have much features, but it can:

- Install all the above mentioned tools as composer dependency
- Copy the predefined settings to your project. The predefined settings include PSR-2 coding standard and PHP Mess Detector settings suitable for Laravel projects
- Create composer scripts for running these tools. It is convenient to use the shorter composer commands. You don’t need to remember the syntax of each tool separately. You can also run these scripts in pre-commit hooks or on the CI server

## Installation

You can install the package via composer:

```
composer require --dev daniel-werner/php-quality-tools
```

After installing with composer, run the following command from the `root` directory of your project:

```
vendor/bin/phpqt-install
```

This will copy the default xml settings for the tools and to set up the scripts in the `composer.json`.

The install script will try to guess the source code directory in your project, if it is a Laravel application it will use the `app` directory, if it is a package it will use the `src` directory, otherwise the current directory.

You can pass the source code directory as the first argument of the install script, like this:

```
vendor/bin/phpqt-install my-app-src
```

After the installation the xml configurations can be found in your projects root directory. You can customize the phpcs and phpmd configurations by changing the settings in the xml files.

## Usage

The package defines the following scripts in the `composer.json`:

- `composer inspect`: this command runs the PHP Code Sniffer (phpcs) and the PHP Static Analysis Tool (phpstan). It will analyze your code style and run the phpstan with the default minimum level=0
- `composer inspect-fix`: this command will try to fix the problems found by the inspection by running the PHP Coding Standards Fixer (php-cs-fixer) and the PHP Code Beautifier and Fixer (phpcbf).
- `composer insights`: runs the PHP Mess Detector to find any potential issues in your code.

## Missing features/possible enhancements

After releasing the first version of the package there is plenty of room for improvements, for example:

- use [Captain Hook](https://github.com/captainhookphp/captainhook) to automatically set up pre commit hooks, therefore enforce the defined standards on the project
- improve the error handling
- support for running on windows platform

## Closing thoughts

This package makes it easier to improve the overall quality of all my PHP based projects. It is open-source and of course free of charge. Any feedback or contribution is welcome and much appreciated, the source code is on [GitHub](https://github.com/daniel-werner/php-quality-tools).

The post [PHP Quality Tools package](https://www.wernerd.info/php-quality-tools-package/) appeared first on [Daniel Werner](https://www.wernerd.info).