Ignore specific warnings with PHP_CodeSniffer - php

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.

Related

How can I find out what rule produces an error?

I'm setting up PHP CodeSniffer as a linter for my code and I have an error which I want to ignore.
In order to do that, I should be able to put the line // phpcs:ignore Name.Of.The.Rule before the line that is an exception to that rule.
Unfortunately, I don't know how I can find which of the rules I have to ignore. Is there a way to display the rule producing the error?
For now, I searched the error message in my vendor folder, resulting in 4 different entries. I'm not sure I know which is the one called.
The error summary looks like that:
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
14 | ERROR | Method name "ActivityRules::is_after" is not in camel
| | caps format
----------------------------------------------------------------------
I would love to have something like that:
---------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------
14 | ERROR | Method name "ActivityRules::is_after" is not in camel | SomeStandard.Category
| | caps format | .NameOfTheSniff.RuleCalled
---------------------------------------------------------------------------------------------------
EDIT:
I don't need you to tell me it's PSR1.Methods.CamelCapsMethodName.NotCamelCaps, I know how to find the rule by hand the hard way, by trial and error. I'd like to know if there is an easy way to do it.
Use phpcs -s
The output of phpcs --help includes the available options:
phpcs --help
...
-s Show sniff codes in all reports
...
The 'Show sniff codes in all reports' option results in this output format:
➜ /tmp phpcs -s example.php
FILE: /private/tmp/example.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
14 | ERROR | [ ] Method name "ActivityRules::is_after" is not in camel caps format
| | (PEAR.NamingConventions.ValidFunctionName.NotCamelCaps)
----------------------------------------------------------------------
Time: 30ms; Memory: 6MB
➜ /tmp
Which is hopefully close enough to what you're looking for here.

How to catch only the PHP extensions that are necessary for the app to work

I need to catch only these PHP extensions that are necessary for the app to work. The idea is removing all the PHP extensions that are not necessary for the app. Do you guys have any idea how can I do that?
The app is on PHP 8.0.14 - Laravel 8
I am not sure what is the question.
Try this code:
$full = explode("-", "PHP 8.0.14 - Laravel 8"); // first argument is separator, in your case it's "-" sign, second argument is what needs to be separated
$extension =end($full); //gives everithing after "-" sign
$noextension = reset($full); //gives everithing before "-" sign
I think you need one of those.
Take a look at this project: https://github.com/maglnet/ComposerRequireChecker
It does what you are asking, based both in the dependencies you required through composer and on the code you wrote.
You can use it by running composer-require-checker check <path/to/composer.json>
The output will be something like this:
$ composer-require-checker check composer.json
ComposerRequireChecker 4.1.x-dev#fb2a69aa2b7307541233536f179275e99451b339
The following 2 unknown symbols were found:
+----------------------------------------------------------------------------------+--------------------+
| Unknown Symbol | Guessed Dependency |
+----------------------------------------------------------------------------------+--------------------+
| hash | ext-hash |
| Psr\Http\Server\RequestHandlerInterface | |
+----------------------------------------------------------------------------------+--------------------+
By looking at it you can tell that I must include ext-hash and psr/http-server-handler to my composer's require section.
Note that although ext-hash has been shipped with standard PHP distributions for a while, it may be a good practice to include it, so if your software is being executed in a non-standard/custom distribution.

Finding PHP CodeSniffer rules

How do I find the PHP CodeSniffer rules that I need?
For example I would like function arguments and arrays to not be spaced like this
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
Instead it should be like this:
function asd($var, $var2 = '') {
$array = [$arg, $arg2];
}
And I'd like phpcbf to be able to fix these, but I have no idea how to find what rules do that.
There is no documentation for PHPCS rules (I will get around to this one day) so the only way to figure out what rules you need is to run PHPCS over some sample code.
For this example, I've put your sample bad code into a temp.php file:
<?php
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
Then I run PHPCS over it using the included standards. I use the -s command line argument to make sure I can see the sniff codes as well. I get this output:
$ phpcs temp.php --standard=Generic,Squiz,PEAR,PSR2,Zend -s
FILE: temp.php
---------------------------------------------------------------------------------------------------------------------------------------------
FOUND 17 ERRORS AND 5 WARNINGS AFFECTING 4 LINES
---------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | [ ] The PHP open tag does not have a corresponding PHP close tag (Generic.PHP.ClosingPHPTag.NotFound)
1 | ERROR | [ ] Missing file doc comment (Squiz.Commenting.FileComment.Missing)
2 | WARNING | [ ] The method parameter $var is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
2 | WARNING | [ ] The method parameter $var2 is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
2 | WARNING | [ ] Consider putting global function "asd" in a static class (Squiz.Functions.GlobalFunction.Found)
2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing)
2 | ERROR | [ ] Missing function doc comment (Squiz.Commenting.FunctionComment.Missing)
2 | ERROR | [x] Expected 2 blank lines before function; 0 found (Squiz.WhiteSpace.FunctionSpacing.Before)
2 | ERROR | [ ] Missing function doc comment (PEAR.Commenting.FunctionComment.Missing)
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
2 | WARNING | [ ] Variable "var2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers)
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose)
2 | ERROR | [x] Opening brace should be on a new line (Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine)
2 | ERROR | [x] Opening brace should be on a new line (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine)
2 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine)
3 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
3 | ERROR | [x] Short array syntax is not allowed (Generic.Arrays.DisallowShortArraySyntax.Found)
3 | ERROR | [x] Array with multiple values cannot be declared on a single line (Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed)
3 | WARNING | [ ] Variable "arg2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers)
4 | ERROR | [x] Expected //end asd() (Squiz.Commenting.ClosingDeclarationComment.Missing)
4 | ERROR | [x] Expected 1 blank line before closing function brace; 0 found
| | (Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose)
4 | ERROR | [x] File must not end with a newline character (Generic.Files.EndFileNoNewline.Found)
---------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 12 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------------------------------------
Time: 84ms; Memory: 8Mb
Then I pick out the messages that you want to keep. Probably these two fixable errors:
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose)
So you might try including the whole Squiz.Functions.FunctionDeclarationArgumentSpacing sniff in your custom standard and see if you like the results. If not, you can put just those two messages into your standard and that will ignore the rest of the sniff.
You will notice that no errors were reported for the spaces at the start and end of your short array. This tells me that there is no sniff included with PHPCS that checks for this, so you would have to write a custom sniff if you want to enforce it.
This isn't obviously a pretty way of getting this information and docs would be far better, but it's the way I figure out which sniffs can be used to check code (and if a sniff is available) so I thought I'd post it as an answer. Hopefully it helps a little.

How to remove the extra line when using PHP SplFileObject and READ_CSV flag?

When iterating over a csv file using PHP SplFileObject and the READ_CSV flag I get an extra row with a null value. Is there a way to remove this line automatically?
$file = new SplFileObject(__DIR__.'/technologies.csv', 'r');
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
var_dump($row);
}
This will produce a row with a null value.
...
array(1) {
[0] =>
NULL
}
You want to also set the SplFileObject::SKIP_EMPTY flag, and for that to work also the SplFileObject::READ_AHEAD flag.
The SplFileObject::SKIP_EMPTY flag does what it says on the tin: it skips empty lines. A trailing newline in your file counts as an empty line.
(Aside: SplFileObject::READ_AHEAD is needed so that SplFileObject::SKIP_EMPTY can do its job. The next line needs to be read, internally, so that PHP can determine if it is empty or not.)
So, your code will look like (wrapped on to multiple lines so you can read it):
$file->setFlags(SplFileObject::READ_CSV |
SplFileObject::SKIP_EMPTY |
SplFileObject::READ_AHEAD);
I am using PHP 8.0.18 & I had to add one more flag in order to skip the empty lines, ie SplFileObject::DROP_NEW_LINE. The final code looks like;
$file->setFlags(
SplFileObject::READ_CSV |
SplFileObject::SKIP_EMPTY |
SplFileObject::DROP_NEW_LINE |
SplFileObject::READ_AHEAD
);

extending phpcodesniffer to filter report based on error codes

I am trying to extend PHPCodeSniffer.What I am trying to achive is to filter the report using error codes.
To explain this lets say I have an error message like "error code : 630 , function is not compatible"
When I run PHPCS from command line , I shoudl be able to pass an argument "error code" so that the report is filtered based on it.(only show result for error code say 630)
e.g.
$ phpcs --standard=mystanderd /path/to/code/myfile.php --errorcode=603
and output will be
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | 603 | function is not compatible
20 | ERROR | 603 | function is not compatible
51 | ERROR | 603 | function is not compatible
88 | ERROR | 603 | function is not compatible
--------------------------------------------------------------------------------
what is the best way to achive it ? as far as what I have understood we can filter only based on seviority as it have inbuilt support.
I would like to avoid modifying the core of PHPCodeSniffer. What I am thinking to do is to write a wrapper script which will accept the argument from CLI and execute PHPCS the capture the o/p and manipulate it before throwing out to the console.However, I don't think it is a best solution.
a bash script utilising grep and wc comes to mind.
You could also use a PHP script like this (let's say this is called my_wrapper.php):
<?php
$legal_codes = array(
'603' => true
);
$f = fopen('php://stdin', 'r');
while ($line = fgets($f)) {
if (preg_match("/^\s*(\d+)\s*\|\s*([A-Z]+)\s*\|\s*(\d+)\s*\|\s*(.*)/", $line, $match)) {
$code = trim($match[3]);
if (!isset($legal_codes[$code])) {
continue;
}
}
echo $line;
}
?>
Which when called like this:
php my_wrapper.php < cs_out.txt
With cs_out.txt like this:
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | 601 | function is not compatible
20 | ERROR | 602 | function is not compatible
51 | ERROR | 603 | function is not compatible
88 | ERROR | 604 | function is not compatible
--------------------------------------------------------------------------------
Produces output like this:
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
51 | ERROR | 603 | function is not compatible
--------------------------------------------------------------------------------
Making the keys of the $legal_codes array specifiable via command line parameter to my_wrapper.php is left as an exercise for the reader.

Categories