How can I suppress PHPCS warnings using comments?

后端 未结 4 1759
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 01:00

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<

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 01:35

    With the following comments you can ignore whatever part of a file you would like.

    Ignore all the sniffs for the file:

    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.

提交回复
热议问题