How to Ignore Line Length PHP_CodeSniffer
Asked Answered
R

4

49

I have been using PHP_CodeSniffer with jenkins, my build.xml was configured for phpcs as below

<target name="phpcs">
    <exec executable="phpcs">
        <arg line="--report=checkstyle --report-file=${basedir}/build/logs/checkstyle.xml --standard=Zend ${source}"/>
    </exec>
</target> 

And I would like to ignore the following warning

FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 117 | WARNING | Line exceeds 80 characters; contains 85 characters
--------------------------------------------------------------------------------

How could I ignore the line length warning?

Ricky answered 14/2, 2012 at 16:45 Comment(3)
Hi dextervip. Are you using a Jenkins plugin to publish your phpcs results? I would like to know about how this is set up in Jenkins. Cheers, nsAttic
@Attic I hava been using Violations puglin to publish my results. It works pretty well, Just install it and enable it in your project linking to your xml file.Ricky
Thanks @Ricky I will check that out. I just got a setup working yesterday using the 'checkstyle' plugin. It gives some nice reporting with some trend graphs etc. More info to those interested here: https://mcmap.net/q/356409/-how-can-i-convert-php-code-sniffer-xml-report-into-htmlAttic
L
71

You could create your own standard. The Zend one is quite simple (this is at /usr/share/php/PHP/CodeSniffer/Standards/Zend/ruleset.xml in my Debian install after installing it with PEAR). Create another one based on it, but ignore the line-length bit:

<?xml version="1.0"?>
<ruleset name="Custom">
 <description>Zend, but without linelength check.</description>
 <rule ref="Zend">
  <exclude name="Generic.Files.LineLength"/>
 </rule>
</ruleset>

And set --standard=/path/to/your/ruleset.xml.

Optionally, if you just want to up the char count before this is triggered, redefine the rule:

 <!-- Lines can be N chars long (warnings), errors at M chars -->
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="N"/>
   <property name="absoluteLineLimit" value="M"/>
  </properties>
 </rule>
Loser answered 14/2, 2012 at 17:35 Comment(4)
Hi Wrikken, do you know where I can find the file to edit the rule? ThanksScrapple
@DanJ.: as stated, don't edit the existing rule, extend a ruleset you like and rewrite a few rules. If you really want to get to the file: it depends, usually they're hanging around in /usr/share/php/ somewhere as in the answer.Loser
Or you can: <rule ref="Generic.Files.LineLength"> <exclude-pattern>src/Migrations</exclude-pattern> </rule> Example: PHP_CodeSniffer Annotated RulesetSensuality
Thank you, I had overridden PSR12, and it worked perfectly. Created a file with its own directory ~/.config/composer/vendor/squizlabs/php_codesniffer/src/Standards/PSR12C/ruleset.xml and start using it. It works well in sublime linter & phpcs plugings. <?xml version="1.0"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PSR12C" xsi:noNamespaceSchemaLocation="../../../phpcs.xsd"> <description>The PSR-12 coding standard with line length customization.</description> <rule ref="PSR12"> <exclude name="Generic.Files.LineLength"/> </rule> </ruleset>Amphiarthrosis
F
23

An alternative way of ignoring the message Line exceeds x characters is to use the --exclude flag to exclude the rule.

vendor/bin/phpcs --standard=PSR2  --exclude=Generic.Files.LineLength app/

In order to find the rule name to exclude, find your corresponding ruleset in following directory:

vendor/squizlabs/php_codesniffer/src/Standards/<coding standard>/ruleset.xml

The rule name will be in the ref node:

 <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="0"/>
        </properties>
 </rule>

It's quicker & less cumbersome than creating a separate ruleset.

Facelifting answered 23/8, 2019 at 13:47 Comment(0)
O
5
  1. Find file CodeSniffer/Standards/PEAR/ruleset.xml – on mac/linux you can search in terminal:

    locate PEAR/ruleset.xml or sudo find / -name "ruleset.xml"

  2. Then you need to find the following lines in the ruleset.xml:

    <!-- Lines can be 85 chars long, but never show errors --> <rule ref="Generic.Files.LineLength"> <properties> <property name="lineLimit" value="85"/> <property name="absoluteLineLimit" value="0"/> </properties> </rule>

  3. Just change the number 85 (max length of the line) to what you want.

Notice that the phpc's default coding standard is the PEAR standard. So you need to edit ruleset.xml at this location: CodeSniffer/Standards/PEAR/ruleset.xml

Octagon answered 3/8, 2015 at 20:23 Comment(0)
P
4

If you don't want to type every time whole command with parameters --standard=PSR2 --exclude=Generic.Files.LineLength app/ you can create file phpcs.xml in your main directory with overriden rule setting.

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">

    <rule ref="PSR2" /> <!-- ruleset standard -->
    <rule ref="Generic.Files.LineLength"> <!-- rule to override -->
        <properties>
            <property name="lineLimit" value="150"/> <!-- maximum line length -->
        </properties>
    </rule>
    <file>app</file> <!-- directory you want to analyze -->
    <arg name="encoding" value="utf-8"/>
</ruleset>

You then need to type only following command:

vendor/bin/phpcs
Pella answered 4/1, 2021 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.