How can I suppress PHPCS warnings using comments?
Asked Answered
O

4

101

My TestMyClass.php has two class definitions in the same file (unit testing class), and PHP Code Sniffer complains about each class must be in a file by itself. How can I suppress this warning?

class MyClassImpl implements MyInterface
{
    // ...
}

class MyClassTest extends \PHPUnit_Framework_TestCase
{
    // ...
}
Oho answered 16/4, 2013 at 14:9 Comment(0)
I
118

You can get PHP_CodeSniffer to ignore specific files or lines in a file using comments: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders

In this case, the error will be generated on your second class definition, so you'd have to write you second definition like this:

// @codingStandardsIgnoreStart
class MyClassTest extends \PHPUnit_Framework_TestCase
{
    // @codingStandardsIgnoreEnd
    // ...
}

But you might also choose to just ignore the whole file if it is not supposed to be checked, either using the @codingStandardsIgnoreFile comment or by specifying exclusions on the command line (see the previous link for info).

If you find that you do this a lot and you don't want to add comments to your code, you can also create your own custom coding standard. Assuming you are using the PSR2 standard right now, you'd create an XML file (e.g., mystandard.xml) and include the following content:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom coding standard.</description>
 <rule ref="PSR2" />
 <rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
   <severity>0</severity>
 </rule>
</ruleset>

Then run PHP_CodeSniffer like this: phpcs --standard=/path/to/mystandard.xml /path/to/code

Having your own ruleset lets you do a lot of things, including changing error messages, changing the severity or type of a message, including checks from other standards, and setting global ignore rules. More info here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

Illicit answered 17/4, 2013 at 23:9 Comment(3)
+1 for skipping the test files. Or maybe you could use the PHPUnitStandard for those :-)Enlil
From the documentation: You can also ignore a single line using the @codingStandardsIgnoreLine comment. This comment will ignore the line that the comment is on, and the following line.Acton
Note that the syntax has now changed. The @codingStandards notation has been deprecated and replaced with //phpcs:disable etc. The original link above still works and discusses the new syntax.Signal
G
130

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.

Greyhen answered 27/11, 2019 at 15:3 Comment(0)
I
118

You can get PHP_CodeSniffer to ignore specific files or lines in a file using comments: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders

In this case, the error will be generated on your second class definition, so you'd have to write you second definition like this:

// @codingStandardsIgnoreStart
class MyClassTest extends \PHPUnit_Framework_TestCase
{
    // @codingStandardsIgnoreEnd
    // ...
}

But you might also choose to just ignore the whole file if it is not supposed to be checked, either using the @codingStandardsIgnoreFile comment or by specifying exclusions on the command line (see the previous link for info).

If you find that you do this a lot and you don't want to add comments to your code, you can also create your own custom coding standard. Assuming you are using the PSR2 standard right now, you'd create an XML file (e.g., mystandard.xml) and include the following content:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom coding standard.</description>
 <rule ref="PSR2" />
 <rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
   <severity>0</severity>
 </rule>
</ruleset>

Then run PHP_CodeSniffer like this: phpcs --standard=/path/to/mystandard.xml /path/to/code

Having your own ruleset lets you do a lot of things, including changing error messages, changing the severity or type of a message, including checks from other standards, and setting global ignore rules. More info here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

Illicit answered 17/4, 2013 at 23:9 Comment(3)
+1 for skipping the test files. Or maybe you could use the PHPUnitStandard for those :-)Enlil
From the documentation: You can also ignore a single line using the @codingStandardsIgnoreLine comment. This comment will ignore the line that the comment is on, and the following line.Acton
Note that the syntax has now changed. The @codingStandards notation has been deprecated and replaced with //phpcs:disable etc. The original link above still works and discusses the new syntax.Signal
N
36

The @codingStandards syntax is deprecated as of version 3.2.0, use phpcs instead.

Some examples:

// phpcs:disable
// phpcs:ignore
// phpcs:enable

See this page for more information: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-parts-of-a-file

Nonflammable answered 23/4, 2018 at 13:25 Comment(0)
I
7

Altho I like the top answer though I'd also suggest this one if you don't care about specific files.

Run this command:

phpcs --config-set show_warnings 0

as seen here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#hiding-warnings-by-default

A note about setting configs this way is they will get wiped out if you delete the vendor folder or update the library and you will have to set them again.

Iliac answered 17/12, 2019 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.