PHPCS The beauty of it

Created on March 3, 2023 at 12:13 pm

PHP CodeSniffer (PHPCS) is a powerful open-source command-line interface tool that detects coding standard violations and provides automatic fixes for automatically fixable rules. Although Pint or PHP-CS-Fixer tools can also detect and fix coding violations, PHPCS is a complementary tool that provides unique, valuable code style suggestions and fixes. This article aims to convince you that adding PHPCS to your repertoire is worth considering, even if you are already using Pint or PHP-CS-Fixer.

Defining PHPCS rules across multiple projects in a team setting can be tedious, and rule drift is bound to happen between projects. It is essential to enforce consistent rules across projects without much thought or effort. In this tutorial, you’ll learn how to create an organization ruleset that you can use to quickly lint all your PHP projects.

In Part 1 of this series, you learned how to use PHPCS with Laravel projects. This tutorial will move the ruleset we created in a Laravel project to a dedicated Composer package.

Why Use PHPCS?

You might wonder, “why would I use PHPCS when we have Laravel Pint and PHP CS Fixer?” Instead of considering these tools as competitors, consider them complementary tools that provide valuable code style suggestions and fixes. PHPCS is a linter, and PHP-CS-Fixer is a fixer.

Although these tools overlap, PHP-CS-Fixer has linting capabilities using the --dry-run feature. PHPCS has a phpcbf CLI tool that automatically fixes PHPCS sniff violations. However, phpcbf does not fix every single available rule.

For example, you can configure PHPCS to detect and report line length violations. However, PHPCS cannot automatically fix these violations since the tool cannot determine how you would like to break up long lines.

Getting Started

Before we start, we need to create a new PHP Composer package to create a custom ruleset that you can reuse on all your projects. PHPCS needs to know about these rulesets, so we will use the Composer Installer package, which makes it easy to install coding standards via Composer.

First, create a new directory called phpcs, initialize Git and Composer by running the following commands:

bashCopy codemkdir phpcs
cd phpcs
git init
composer init

When you run composer init, Composer will prompt you to define your package name. At least fill out the package name, optionally the description, and finish the prompts. We will require dependencies manually, so don’t worry about installing them interactively.

To ease detecting PHPCS standards from composer packages, we need to install the Composer Installer package. Run the following command:

luaCopy codecomposer require --dev dealerdirect/phpcodesniffer-composer-installer

The Composer Installer package requires that you define the type property with phpcodesniffer-standard. The Composer Installer plugin searches for rulesets using the type property in all of your project’s installed Composer packages.

In the end, your composer.json file should look something like the following:

jsonCopy code{
    "name": "bitpressio/phpcs",
    "type": "phpcodesniffer-standard",
    "authors": [
            "name": "Paul Redmond",
            "email": "[email protected]"
    "require-dev": {
        "dealerdirect/phpcodesniffer-composer-installer": "^1.0"
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true


PHPCS is a powerful tool that can save time and ensure consistency across projects in a team setting. By creating a custom ruleset in a dedicated

Connecting to Connected... Page load complete