• Home
  • Contact
code_quality.png

PHPQA Analyzer CLI tool

Jesus Manuel Olivas
July 20, 2015

One point we know need more love and atenttion at DrupalConsole project is to increase the test coverage. Even when I had some previous experience using tools like PHPUnit and Behat. I decided to start researching for material related to this topic and how to automate test execution.

On this research I found this blog post Write your git hooks in PHP and keep them under git control and this resource of PHP QA related tools The PHP Quality Assurance Toolchain that I highly recomend you to read and visit.

PHPQA a new project was born

After trying to include a variation of the code I found on the blog post mentioned above, you can see code at CodeQualityTool.php. I decided to decouple this feature and publish an advanced version of this on a separated project.

You can find the source code at github https://github.com/jmolivas/phpqa/ because you know, we all love Open Source.

Project Overview

This project aims to serve as a CLI tool to make easy the use of different PHP tools related to Quality Assurance and code analysis in PHP.

Every analyzer tool handles arguments and options using different formats, the goal of this project is to provide a single way to interact with those projects, you can also set options and arguments using a default configuration file when the project supports it.

Available Analyzers

  • This tool check syntax of PHP files faster then serial check with fancier output.

  • The PHP Coding Standards Fixer tool fixes most issues in your code when you want to follow the PHP coding standards as defined in the PSR-1 and PSR-2 documents.

    PHP-CS-Fixer

  • PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations.

    PHPCBF

    PHPCS

  • It is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.

    PHPMD

  • phploc is a tool for quickly measuring the size and analyzing the structure of a PHP project.

    PHPLOC

  • phpdcd is a Dead Code Detector (DCD) for PHP code. It scans a PHP project for all declared functions and methods and reports those as being "dead code" that are not called at least once.

    PHPDCD

  • phpcpd is a Copy/Paste Detector (CPD) for PHP code.

    PHPCPD

  • PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

    PHPUnit

Install

Cloning the project

$ git clone [email protected]:jmolivas/phpqa.git $ cd phpqa # download dependencies $ composer install # make phpqa globally accessible creating a symlink $ ln -s /path/to/phpqa/bin/phpqa /usr/local/bin/phpqa

Usage

Copy configuration files to user home directory

$ cd to/project/path $ phpqa init --project=PROJECT --global --override
Option Description
project Available values `php`, `symfony` and `drupal`.
global Copy configuration files to user home directory, instead of current working directory.
override If this option is set, files are copied using override flag.
**NOTES:**
  • Option global does not accept a value must be set as --global.
  • Option override does not accept a value must be set as --override.

Analyze a project

$ cd to/project/path $ phpqa analyze --project=PROJECT --files=FILES $ phpqa analyze --project=PROJECT --git
Option Description
project Available values php, symfony and drupal
files Files or directories to analyze.
git If this option is set, all files added to git index will be scanned. This is useful when setting executing this tool on a pre-commit git-hook.
**NOTES:**
  • Option git does not accept a value must be set as --git.
  • Options files and git can not used in combination.
  • Option project could be omitted if a phpqa.yml or phpqa.yml.dist file is available at current working directory.

If you like to know about nice to have features, please visit the project page https://github.com/jmolivas/phpqa#nice-to-have-features

This is just the beginning of the project the real goal is to provide a tool to take advantage of all those great projects and make easy to customize and execute.

This project was built to be fully customizable. I will be working on the documentation related to override default configurations, but in the meantime please please visit https://github.com/jmolivas/phpqa#override-configuration and give a try and as usual, comments and suggestions are more than welcome.

NOTE: Image used on this bog post, originally from xkcd comic Quality Code at https://xkcd.com/1513/

Ready to embrace the JAMstack revolution?

Build fast and secure sites and apps delivered by pre-rendering files and serving them directly from a CDN, removing the requirement to manage or run web servers, databases and worry about traffic spikes.
Work with us!