Skip to content

Instantly share code, notes, and snippets.

@fedecarg
Last active December 14, 2015 15:28
Show Gist options
  • Save fedecarg/5107665 to your computer and use it in GitHub Desktop.
Save fedecarg/5107665 to your computer and use it in GitHub Desktop.

Custom CodeSniffer ruleset file

CodeSniffer allows you to create your own ruleset.xml file. Once created, a ruleset file can be used with the --standard command line argument. In the following example, CodeSniffer will use the coding standard defined in the directory MyStandard.

1. Create a temp directory and ruleset.xml file:

mkdir /tmp/MyStandard
touch /tmp/MyStandard/ruleset.xml

2. Add rules:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My Standard</description>

 <!-- Zend rules -->
 <rule ref="Zend" >
  <exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman" />
 </rule>

 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\n"/>
  </properties>
 </rule>
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="140"/>
   <property name="absoluteLineLimit" value="140"/>
  </properties>
 </rule>

 <rule ref="MyStandard.PHP.ClassComment"/>
 <rule ref="MyStandard.PHP.ClassMethodComment"/>
 <rule ref="MyStandard.PHP.FirstCharacter"/>
 <rule ref="MyStandard.PHP.ForbiddenStatements"/>
 <rule ref="MyStandard.PHP.GlobalKeyword"/>
 <rule ref="MyStandard.PHP.LintCheck"/>
 <rule ref="MyStandard.PHP.MultiplePHPTags"/>
 <rule ref="MyStandard.PHP.Reflection"/>
 <rule ref="MyStandard.PHP.SideEffects"/>
 <rule ref="MyStandard.PHP.Superglobals"/>

 <rule ref="Squiz.Commenting.FunctionCommentThrowTag"/>
 <rule ref="Squiz.Operators.ComparisonOperatorUsage"/>
 <rule ref="Squiz.Operators.ValidLogicalOperators"/>
 <rule ref="Squiz.PHP.DisallowComparisonAssignment"/>
 
 <rule ref="Generic.CodeAnalysis.EmptyStatement"/>
 <rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
 <rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
 <rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
 <rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
 <rule ref="Generic.Classes.DuplicateClassName"/>
 <rule ref="Generic.Functions.CallTimePassByReference"/>
 <rule ref="Generic.Metrics.CyclomaticComplexity"/>
 <rule ref="Generic.Metrics.NestingLevel"/>
 <rule ref="Generic.PHP.NoSilencedErrors"/>
</ruleset>

3. Run CodeSniffer:

phpcs --standard=/tmp/MyStandard/ruleset.xml /path/to/dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment