DEV Community

Walter Nascimento
Walter Nascimento

Posted on • Updated on

PHP_CodeSniffer: Detect code standards violations

PHP_CodeSniffer (PHPCS) is a widely used tool to ensure that PHP code follows coding standards like PSR-12, among others. In this article, learn how to install PHP_CodeSniffer locally, globally, and in Docker, set PSR-12 as the default, configure your project to report errors, automatically fix issues with PHPCBF, and use XML reports.

Installing PHP_CodeSniffer

Local Installation via Composer

To install PHP_CodeSniffer for a specific project, run the following command in your project's root directory:

composer require --dev "squizlabs/php_codesniffer=*"
Enter fullscreen mode Exit fullscreen mode

This installs PHP_CodeSniffer as a development dependency in your project.

Global Installation via Composer

To use PHP_CodeSniffer across multiple projects, you can install it globally:

composer global require "squizlabs/php_codesniffer=*"
Enter fullscreen mode Exit fullscreen mode

After global installation, ensure the directory ~/.composer/vendor/bin is in your PATH. Add the following line to your ~/.bashrc (or ~/.zshrc for Zsh users):

export PATH="$PATH:$HOME/.composer/vendor/bin"
Enter fullscreen mode Exit fullscreen mode

Then reload your shell configuration:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Installation with Docker

If you're using Docker, you can include PHP_CodeSniffer in your image. Here's an example Dockerfile:

FROM php:8.1-fpm

# Install Composer and PHP_CodeSniffer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
RUN composer global require "squizlabs/php_codesniffer=*"

# Configure the PATH
RUN echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> $HOME/.bashrc

CMD ["php-fpm"]
Enter fullscreen mode Exit fullscreen mode

This configuration ensures that PHP_CodeSniffer is globally available when you run the container.

Setting the PSR-12 Standard

PSR-12 is the recommended coding standard by PHP-FIG for PHP code formatting. To set PHP_CodeSniffer to use PSR-12, run the following command:

phpcs --standard=PSR12
Enter fullscreen mode Exit fullscreen mode

Alternatively, configure it in your phpcs.xml file:

<?xml version="1.0"?>
<ruleset name="PSR12 Coding Standard">
    <rule ref="PSR12"/>
    <file>src/</file>
</ruleset>
Enter fullscreen mode Exit fullscreen mode

Using the XML Configuration File

You can customize PHP_CodeSniffer with an XML configuration file, allowing you to disable certain checks, add features like progress bars, and color output.
Example of phpcs.xml:

<?xml version="1.0"?>
<ruleset name="Custom Coding Standards">
    <description>PHPCS Custom Ruleset Example</description>

    <!-- PSR-12 Standard -->
    <rule ref="PSR12"/>

    <!-- Color the result output -->
    <arg name="colors" />

    <!-- Show progress of the run -->
    <arg value="ps" />

    <!-- Code directory -->
    <file>src/</file>

    <!-- Ignore file and class comment errors -->
    <rule ref="Squiz.Commenting.FileComment.Missing">
        <exclude-pattern>*/src/*</exclude-pattern>
    </rule>
    <rule ref="Squiz.Commenting.ClassComment.Missing">
        <exclude-pattern>*/src/*</exclude-pattern>
    </rule>
</ruleset>
Enter fullscreen mode Exit fullscreen mode

Now, when you run the command, you'll see a progress bar and colored output in the terminal, along with using the PSR-12 standard.

Code Example: Before and After PHPCBF

Original Code (Before)

<?php
namespace MyApp;

class MyClass {
    public function myFunction() {
        $var = "Hello World!";
        echo $var;
    }
}
Enter fullscreen mode Exit fullscreen mode

After Running PHPCBF

To automatically fix issues found by PHP_CodeSniffer, use the following command:

phpcbf --standard=PSR12 src/MyClass.php
Enter fullscreen mode Exit fullscreen mode

Corrected Code (After)

<?php

namespace MyApp;

class MyClass
{
    public function myFunction()
    {
        $var = "Hello World!";
        echo $var;
    }
}

Enter fullscreen mode Exit fullscreen mode

PHPCBF automatically corrects the formatting to conform with the PSR-12 standard by adjusting indentation, spacing, and proper bracket placement.

Generating a Summary XML Report

PHP_CodeSniffer can generate a summary XML report, which is useful for CI/CD pipelines or automated code analysis.
Configuring XML Summary Reports

To configure PHP_CodeSniffer to generate a summary XML report, add the following configuration to your phpcs.xml file:

<?xml version="1.0"?>
<ruleset name="Custom Coding Standards with Summary">
    <rule ref="PSR12"/>

    <!-- Generate a summarized XML report -->
    <arg name="report" value="summary"/>
    <arg name="report-file" value="phpcs-summary.xml"/>

    <file>src/</file>
</ruleset>
Enter fullscreen mode Exit fullscreen mode

When you run phpcs, it will generate a phpcs-summary.xml file that contains a summary of the violations found.

Conclusion

PHP_CodeSniffer is a powerful tool for ensuring PHP code quality, allowing you to follow coding standards like PSR-12 and customize your checks using an XML configuration file. With it, you can automate code style correction and generate useful reports for reviews and continuous integration.


Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!
😊😊 See you! 😊😊


Support Me

Youtube - WalterNascimentoBarroso
Github - WalterNascimentoBarroso
Codepen - WalterNascimentoBarroso

Top comments (0)