How to Ignore PHPCS Warnings
Arie Visser • February 27, 2020
phpPHP_CodeSniffer is a useful tool to check your code against a set of standards and rules. It can help in securing a certain coding standard and style within a development team, by running PHPCS as a commit hook, or during a CI/CD pipeline. In some cases though, you might need to disable PHPCS for certain parts of your code. For example, when your project contains legacy code you did not yet refactor, or when you are extending from third party objects.
With the following comments you can ignore whatever part of a file you would like.
Ignore all the sniffs for the file:
<?php
// phpcs:ignoreFile
Ignore only the current and next line:
// phpcs:ignore
public function store($myArray)
Ignore the current line:
public function store($myArray) // phpcs:ignore
Ignore a certain amount of lines in a file:
// phpcs:disable
public function store($myArray): void
{
if (count($myArray) > 3) {
// Humongous amount of legacy code you don't want to look at
}
return;
}
// phpcs:enable
With // phpcs:ignore
and // phpcs:disable
you can also specify which messages, sniffs, categories or standards you would like to disable, for example:
// phpcs:ignore Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed
$foo = [1,2,3];
If you want to read more about this subject, you can visit the wiki. Some of the examples were taken from there.