0

My phpcodesniffer report is producing lines like the following example :

 146 | ERROR | Variable "tw_text" is not in valid camel caps format (Zend.NamingConventions.ValidVariableName.NotCamelCaps)
 148 | ERROR | Variable "tw_text" is not in valid camel caps format (Zend.NamingConventions.ValidVariableName.NotCamelCaps)
 154 | ERROR | Variable "tw_text" is not in valid camel caps format (Zend.NamingConventions.ValidVariableName.NotCamelCaps)

This reports continued usage of the same variable, for example :

$tw_text = '';
   : 
$tw_text = $_POST['text'];
   : 
$tw_text = '';

I require just the first report because the second and third are really dependant on the first.

Is there any way to achieve this?

Charles
  • 50,943
  • 13
  • 104
  • 142
crafter
  • 6,246
  • 1
  • 34
  • 46
  • In this example, each line is failing your coding test, which is why you are seeing the report. I am not sure of a way to remove these, or if you would want to since they are all valid based on your sniffs. The 2.0 version is suppose to allow fixing this type of error when it is ready, but I am not sure you want to risk hiding some rows in the output, in case you are re-factoring and miss some occurrences. – Steven Scott Feb 13 '14 at 22:54
  • There is nothing in PHPCS to do this natively because it sees each variable as a separate occurrence. The 2.0 version (alpha 1 is out now) wont fix this specific error because changing the variable names without developer intervention is incredibly high risk, so you'll need to manage these errors on your own. – Greg Sherwood Feb 14 '14 at 01:46
  • Thank you @Steven Scott and Greg Sherwood. I will look into the 2.0 branch. I get fully your comments about keeping the errors for the purposes of QA, but my use case in this case is a post-motem audit of the code. It might make more sense to change the output then to change this in PHPCS itself. – crafter Feb 14 '14 at 04:48

1 Answers1

0

You could use a simple script to filter the errors.

#!/usr/bin/env php
<?php

$seen = [];
while ($line = fgets(STDIN)) {
        $err = explode('|', $line)[2];

        if (in_array($err, $seen))
                continue;

        $seen[] = $err;

        print($line);
}
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Nice, and in my other favourite 'programming language'. The errors are not necessarily sequential in the file. I'll play with this and see how far I can get. Perhaps a combination of cut and unique to the rescue? – crafter Feb 14 '14 at 04:45
  • @crafter Right; I've updated my answer, I made it a PHP script, it's certainly possible to do this with a shell script, but this is really the point where shell scripts get hairy... – Martin Tournoij Feb 14 '14 at 14:46