Improving PHP Code Quality with PHING, PHPCS, PHPCPD, and Phan

This article explains how to set up a PHP project with Composer, PHING, and a suite of quality‑checking tools—including PHPCS for coding standards, PHPCPD for duplicate detection, and Phan for deep static analysis—to automatically enforce code quality and reduce manual review effort.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Improving PHP Code Quality with PHING, PHPCS, PHPCPD, and Phan

In this guide we assume familiarity with PHP 7.1, Composer, and PSR‑4 autoloading, and we demonstrate how to create a build.xml file for PHING to run quality checks.

Prerequisite : Install PHING as a development dependency. $ php composer.phar require --dev phing/phing Create a basic build.xml file in the project root.

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="run">
</project>

1. Static code analysis : Use PHING to run tools automatically.

2. Code style checking with PHP_CodeSniffer (PHPCS). Install and add a target to build.xml:

$ php composer.phar require --dev squizlabs/php_codesniffer
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="run">
  <target name="phpcs" description="Check code style with PHP_CodeSniffer">
    <exec executable="bin/phpcs" passthru="true" checkreturn="true">
      <arg line="--standard=PSR1,PSR2 -extensions=php src"/>
    </exec>
  </target>
  <target name="run" depends="phpcs"/>
</project>

Running ./bin/phing will now report any PSR‑1/PSR‑2 violations.

3. Duplicate code detection using PHPCPD. Install and add a target:

$ php composer.phar require --dev sebastian/phpcpd
<target name="phpcpd" description="Generate pmd-cpd.xml using PHPCPD">
  <exec executable="bin/phpcpd" passthru="true"/>
</target>
<target name="run" depends="phpcs,phpcpd"/>

PHPCPD will list cloned code fragments and duplicated lines.

4. Deep static analysis with Phan. Install Phan and its configuration:

$ php composer.phar require --dev phan/phan
<?php
return [
  'target_php_version' => '7.1',
  'directory_list' => ['src', 'vendor/symfony/console'],
  'exclude_analysis_directory_list' => ['vendor/'],
];

Add a Phan target to build.xml:

<target name="phan" description="Check code with phan">
  <exec executable="bin/phan" passthru="true" checkreturn="true"/>
</target>
<target name="run" depends="phpcs,phpcpd,phan"/>

Running the build will now also report type mismatches and other static analysis errors, e.g., incorrect @phpdoc types.

Conclusion : By integrating PHING with PHPCS, PHPCPD, and Phan, you obtain three fully automated tools that enforce coding standards, detect duplicate code, and perform deep static analysis, dramatically reducing manual code‑review time and improving runtime reliability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

code qualityPHPstatic analysisPHINGPhanPHPCPDphpcs
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.