开发者

Ignore specific warnings with PHP_CodeSniffer

开发者 https://www.devze.com 2023-03-28 08:27 出处:网络
I am working on a password tools module, and part of it is using base64 Encoding/Decoding. As a result I have a number of variables which include the term \'base64\' for obvious reasons. The problem i

I am working on a password tools module, and part of it is using base64 Encoding/Decoding. As a result I have a number of variables which include the term 'base64' for obvious reasons. The problem is that when I run the PHP_CodeSniffer tool, it throws warnings: "Variable "...64" contains numbers but this is discouraged".

Is there any way to tell PHP_CodeSniffer to ignore these warnings for this specific file? I'm sure there is a good reason to avoid numbers, but in this case I'd rather use 'base64' than 'baseSixtyFour'...

This is how I am running PHP_CodeSniffer:

valorin@gandalf:~/workspace/library$ phpcs --standard=ZEND ./Tools/

FILE: /home/valorin/workspace/library/Tools/Password.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 6 WARNING(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  38 | WARNING | Variable "_bEncryptionBase64" contains numbers but this is
     |         | discouraged
  94 | WARNING | Variable "_bEncryptionBase64开发者_开发技巧" contains numbers but this is
     |         | discouraged
  94 | WARNING | Variable "base64" contains numbers but this is discouraged
  95 | WARNING | Variable "base64" contains numbers but this is discouraged
 210 | WARNING | Variable "bEncryptionBase64" contains numbers but this is
     |         | discouraged
 251 | WARNING | Variable "bEncryptionBase64" contains numbers but this is
     |         | discouraged
--------------------------------------------------------------------------------

Time: 1 second, Memory: 7.50Mb


Before version 3.2.0, PHP_CodeSniffer used different syntax for ignoring parts of code from file which will be removed in version 4.0

Old syntax:

// @codingStandardsIgnoreStart

/* put your bad code here! */    

// @codingStandardsIgnoreEnd

This requires version 1.2 or later.

New syntax:

PHP_CodeSniffer is now using // phpcs:disable and // phpcs:enable comments to ignore parts of files and // phpcs:ignore to ignore one line.

Now, it is also possible to only disable or enable specific error message codes, sniffs, categories of sniffs, or entire coding standards. You should specify them after comments. If required, you can add a note explaining why sniffs are being disable and re-enabled by using the -- separator.

<?php

/* Example: Ignoring parts of file for all sniffs */
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable

/* Example: Ignoring parts of file for only specific sniffs */
// phpcs:disable Generic.Commenting.Todo.Found
$xmlPackage = new XMLPackage;
$xmlPackage['error_code'] = get_default_error_code_value();
// TODO: Add an error message here.
$xmlPackage->send();
// phpcs:enable

/* Example: Ignoring next line */
// phpcs:ignore
$foo = [1,2,3];
bar($foo, false);

/* Example: Ignoring current line */
$foo = [1,2,3]; // phpcs:ignore
bar($foo, false);

/* Example: Ignoring one line for only specific sniffs */
// phpcs:ignore Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed
$foo = [1,2,3];
bar($foo, false);

/* Example: Optional note */ 
// phpcs:disable PEAR,Squiz.Arrays -- this isn't our code
$foo = [1,2,3];
bar($foo,true);
// phpcs:enable PEAR.Functions.FunctionCallSignature -- check function calls again
bar($foo,false);
// phpcs:enable -- this is out code again, so turn everything back on

For more details see PHP_CodeSniffer's documentation.


In CodeSniffer version 1.3 you can exclude specific sniffs from specific files at the level of the ruleset.xml file.

0

精彩评论

暂无评论...
验证码 换一张
取 消