PHP_CodeSniffer
Checking coding standards of In-Portal by hand takes a lot of time. Gladly there is a tool out there, called PHP_CodeSniffer, that does that automatically and prints out nice report with violations. To make it even more powerful it can be connected to IDE and report errors of files being edited on the fly making developer life easier.
Right now I have created basic set of rules, that needs to be checked and found, that default rules are not so clever in understanding code as they should be. On this page I will note:
- which PHP_CodeSniffer inspections are failing
- what are exact test case
- what we can do about it
Problematic inspections
Inspection name | Error message | Example code | Problem description | Solved |
---|---|---|---|---|
Drupal_Sniffs_Formatting_SpaceOperatorSniff | A opeator statement must be followed by a single space | $this->Application =& kApplication::Instance(); | Inspections should check, that operands are separated from operators by a single space. Good $a + $b Bad $a+$b However symbols "=" and "&" are operators on it's own ("equals" and "bitwise and") and probably during syntax check inspection isn't aware, that together ("=&") they represent assigning a value by reference. | YES |
Drupal_Sniffs_Array_ArraySniff | A comma should follow the last multiline array item. Found: /*'k2',*/ | $array = Array ( 'k1', /*'k2',*/ ); | Inspection forces usage of trailing comma after last array element. However in this example commented out code is present before array declaration end, which makes inspection go crazy and not notice actual comma after 'k1' code. | YES |
PEAR_Sniffs_Functions_FunctionCallSignatureSniff | Multi-line function call not indented correctly; expected 7 spaces but found 4 | str_replace( 'one', 'two', 'three' ); | Inspection thinks, that spaces need to be used to indent stuff and not TABs. It's not an error inside an inspection, because it's PEAR standard inspection, where this exactly is needed to be done. We need to either found similar inspection, that uses TABs or create our own based on this one.
| YES |