Baseline tool for PHP_CodeSniffer

Overview

PHP_CodeSniffer Baseliner

This tool enables you to integrate PHP_CodeSniffer into an existing project by automatically adding phpcs:ignore and phpcs:disable/phpcs:enable instructions throughout the codebase as a baseline. This allows you to make PHP_CodeSniffer pass without changing any source code, making it possible to use PHP_CodeSniffer in e.g. continuous integration pipelines or git hooks. This way, you can enforce that all new code adheres to your coding standard without touching the existing code.

Installation

Require the package with composer:

composer require --dev isaac/php-code-sniffer-baseliner

It is also possible to install this package as a global composer dependency.

Usage

In order to add phpcs:ignore and phpcs:disable/phpcs:enable instructions throughout your project, run:

vendor/bin/phpcs-baseliner create-baseline

How does it work?

First, the tool runs vendor/bin/phpcs and captures the report. Based on the report output, it will add // phpcs:ignore instructions to the source code for each violation. It will only ignore the sniffs that actually are violated. In rare cases, adding these instructions could introduce new violations. Therefore, this process is repeated until no violations are reported by phpcs.

Example

Let's say we want to enforce declare(strict_types = 1); statements and native property type hints using PHP_CodeSniffer. The Slevomat Coding Standard has sniffs for this: SlevomatCodingStandard.TypeHints.DeclareStrictTypes and SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint. We install Slevomat Coding Standard and add the sniffs to our ruleset in phpcs.xml.

If we now run vendor/bin/phpcs-baseliner create-baseline in our project, it will add ignore instructions in all files not containing declare(strict_types = 1); statements or native property type declarations:

- <?php
+ <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes -- baseline
  
  class Foo {
+     // phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint -- baseline
      private $bar;
  }

In some cases, it is not possible to insert a // phpcs:ignore instruction directly above the violated line (e.g. multi-line strings). In those cases, // phpcs:disable and // phpcs:enable instructions are added:

  <?php

  class Foo {
+     // phpcs:disable Generic.Files.LineLength.TooLong -- baseline  
      public const BAR = '
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas malesuada, lectus vitae vestibulum vulputate, mi morbi.';
+     // phpcs:enable Generic.Files.LineLength.TooLong -- baseline
  }

Features

  • Automatic indentation
  • Ignoring a group of multiple exclusions per line, e.g. // phpcs:ignore Generic.Files.LineLength.TooLong, Generic.Arrays.DisallowLongArraySyntax -- baseline
  • Merging new instructions with existing instructions
  • Messages of existing instructions are merged as wel: // phpcs:ignore Generic.Files.LineLength.TooLong, Generic.Arrays.DisallowLongArraySyntax -- existing message; baseline
  • Using phpcs:disable/phpcs:enable when inserting phpcs:ignore is not possible (i.e. for multi-line strings, including HEREDOCs and NOWDOCs)
  • Adding a star prefix when a violation is found within a comment block with stars, e.g.:
    /*
     * phpcs:ignore Generic.Files.LineLength.TooLong
     * Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas malesuada, lectus vitae vestibulum vulputate, mi morbi.
     */
  • All features are unit tested, see the AddBaselineProcessorTestDataProvider class for an extensive test data set.

Roadmap

  • Support processing files that do not start with <?php on the first line.
  • Support processing files that contain ?>.
  • Support ignoring violations on the first line of a file that end with a multi-line string, example:
    <?php echo 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas malesuada, lectus vitae vestibulum vulputate, mi
    morbi.';
    ?>
  • Support detection of and merging with older types of ignore instructions, such as @phpcsSuppress.
Comments
Releases(v2.3.1)
Owner
ISAAC
Turning .complexity into profitability
ISAAC
PHP Compatibility check for PHP_CodeSniffer

PHP Compatibility Coding Standard for PHP CodeSniffer This is a set of sniffs for PHP CodeSniffer that checks for PHP cross-version compatibility. It

PHPCompatibility 1.9k Dec 28, 2022
Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs

Slevomat Coding Standard Slevomat Coding Standard for PHP_CodeSniffer provides sniffs that fall into three categories: Functional - improving the safe

Slevomat 1.2k Jan 5, 2023
phpcs-security-audit is a set of PHP_CodeSniffer rules that finds vulnerabilities and weaknesses related to security in PHP code

phpcs-security-audit v3 About phpcs-security-audit is a set of PHP_CodeSniffer rules that finds vulnerabilities and weaknesses related to security in

Floe design + technologies 655 Jan 3, 2023
A tool to automatically fix PHP Coding Standards issues

PHP Coding Standards Fixer The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP codi

null 11.6k Jan 3, 2023
PHP Static Analysis Tool - discover bugs in your code without running it!

PHPStan - PHP Static Analysis Tool PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even b

PHPStan 11.6k Dec 30, 2022
A PHP code-quality tool

GrumPHP Sick and tired of defending code quality over and over again? GrumPHP will do it for you! This composer plugin will register some git hooks in

PHPro 3.9k Jan 1, 2023
Beautiful and understandable static analysis tool for PHP

PhpMetrics PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report. Documentation | Twitter | Contributing

PhpMetrics 2.3k Dec 22, 2022
A tool for quickly measuring the size of a PHP project.

PHPLOC phploc is a tool for quickly measuring the size and analyzing the structure of a PHP project. Installation This tool is distributed as a PHP Ar

Sebastian Bergmann 2.3k Jan 4, 2023
PHPMD 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 frontend application for the raw metrics stream measured by PHP Depend.

PHPMD PHPMD 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

PHP Mess Detector 2.1k Jan 8, 2023
A command line refactoring tool for PHP

PHP Refactoring Browser Note: This software is under development and in alpha state. Refactorings do not contain all necessary pre-conditions and migh

QafooLabs 562 Dec 30, 2022
Tool helping us to analyze software projects

Qafoo Quality Analyzer This software is a tool to visualize metrics and source code. We use this software for Code Reviews together with our customers

Qafoo GmbH 494 Dec 29, 2022
A static php code analysis tool using the Graph Theory

Mondrian Ok guyz, you have a master degree in Graph Theory, you follow Law of Demeter and you live on S.O.L.I.D principles ? Let's have some Fun ! (^ω

Florent Genette 391 Nov 30, 2022
PHPCheckstyle is an open-source tool that helps PHP programmers adhere to certain coding conventions.

PHPCheckstyle Overview PHPCheckstyle is an open-source tool that helps PHP programmers adhere to certain coding conventions. The tools checks the inpu

PHPCheckstyle 157 Dec 5, 2022
PHP completion, refactoring, introspection tool and language server.

Phpactor This project aims to provide heavy-lifting refactoring and introspection tools which can be used standalone or as the backend for a text edit

Phpactor 882 Jan 1, 2023
PHP Architecture Tester - Easy to use architectural testing tool for PHP :heavy_check_mark:

Easy to use architecture testing tool for PHP Introduction ?? PHP Architecture Tester is a static analysis tool to verify architectural requirements.

Carlos A Sastre 765 Dec 30, 2022
A static analysis tool for finding errors in PHP applications

Psalm Psalm is a static analysis tool for finding errors in PHP applications. Installation To get started, check out the installation guide. Live Demo

Vimeo 5k Jan 2, 2023
A web tool to explore the ASTs generated by PHP-Parser.

phpast.com A web tool to explore the ASTs generated by PHP-Parser. About This web tool provides a GUI for exploring the AST of your PHP code. You can

Ryan Chandler 23 Nov 29, 2022
All In 1 Spam Tool For Termux Users Subscribe Us (Noob Hackers) some shit heads are trying to abuse this script so don't worry about them ...let them hallucinate ...but you are free to use this script

ABOUT TOOL : SPAMX is a all in one Bombing+Spam tool from this tool you can send anonymous messages to your target without showing your real number an

N17R0 449 Jan 7, 2023
Deptrac is a static code analysis tool for PHP that helps you communicate, visualize and enforce architectural decisions in your projects

Deptrac is a static code analysis tool for PHP that helps you communicate, visualize and enforce architectural decisions in your projects. You can freely define your architectural layers over classes and which rules should apply to them.

QOSSMIC GmbH 2.2k Dec 30, 2022