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 and StyleCI for these purposes. However if you are working on other, proprietary projects, you might need to run/configure these tools yourself. There are plenty of such tools to use with php, you can find quite comprehensive lists on other websites, for example here and here.
My tools of choice are the following:

Why I created the package?

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, 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 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.

Leave a Reply

Your email address will not be published. Required fields are marked *