Search code examples
phpphpcodesniffer

How to correctly change line character limit configuration in PHP CodeSniffer?


So I want to change the values for $lineLimit and $absoluteLineLimit that PHP CodeSniffer uses when checking files. After some googling I tried to make my own standard to facilitate this but I haven't got it working. Working on Ubuntu 12.04.4LTS and in the system directory for PHP CodeSniffer I put in my folder MyStandard and in the subdirectory Sniffs I have what is to be my personal Sniff to change the line limit called LineLimit120Sniff.php

The code is as follows

class LineLimit120Sniff extends Generic_Sniffs_Files_LineLengthSniff
{
    public $lineLimit = 100;
    public $absoluteLineLimit = 120;
}

When I try to run phpcs --standard=MyStandard <file> I get the this as output

PHP Notice: Undefined offset: 3 in /usr/share/php/PHP/CodeSniffer.php on line 1189

PHP Fatal error: Cannot redeclare class LineLimit120Sniff in /usr/share/php/PHP/CodeSniffer/Standards/MyStandard/Sniffs/LineLimit120Sniff.php on line 8

I don't understand, how is the class being redeclared..? What am I doing wrong? Or alternatively, is there an easier way to change the $lineLimit setting?


Solution

  • The easy way is just create a ruleset.xml in a location you like and insert the text below into it.

    <?xml version="1.0"?>
    <ruleset name="MyStandard">
        <description>This standard changes the line length</description>
    
        <!-- Insert all sniff from PSR2 standard -->
        <rule ref="PSR2"/>
    
        <rule ref="Generic.Files.LineLength">
            <properties>
                <property name="lineLimit" value="100"/>
                <property name="absoluteLineLimit" value="120"/>
            </properties>
         </rule>
    </ruleset>
    

    Use the standard by

    phpcs --standard=/path/to/ruleset.xml test.php