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.
精彩评论