PHP CodeSniffer provides you with some useful coding standards to get you going.
This is nice, but how do you customize what's provided or set up your own standards?
There is a tutorial on how to set up your own standards, but that was too much work for me at the moment.
However, that is NOT the way to set up your own standards, in my opinion.
Instead, read this post on how to extend a standard which is similar to your standard, and then include or exclude sniffs as needed :
http://www.kingkludge.net/2009/02/codesniffer-part-3-writing-an-example-...
Here's the short way to do this :
On my Ubuntu system, go to where the standards are located in /usr/share/php/PHP/CodeSniffer/Standards. If you want a sneak peek at what we're doing, look at the Standards/Zend directory, which extend on the PEAR standards.
In CodeSniffer/Standards, make a new directory MyCompany. In the MyStd directory, do a 'cp ../Zend/ZendCodingStandard.php MyCompanyCodingStandard.php'.
Now edit MyCompanyCodingStandard.php and replace 'Zend' with 'MyCompany' and tidy up the comments. Also include the sniffs you want, and exclude those you don't.
This is what the meat of MyCompanyCodingStandards.php looked like after my edits :
<?php
/**
* MyCompany Coding Standard
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer_MyCompany
* @author David Luhman
* @version $Id: $
*/
if (class_exists('PHP_CodeSniffer_Standards_CodingStandard', true) === false) {
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_CodingStandard not found');
}
/**
* MyCompany Coding Standard.
*
* @category PHP
* @package PHP_CodeSniffer_MyCompany
* @author David Luhman
*/
class PHP_CodeSniffer_Standards_MyCompany_MyCompanyCodingStandard
extends PHP_CodeSniffer_Standards_CodingStandard
{
/**
* Return a list of external sniffs to include with this standard.
*
* The standard can include the whole standards or individual Sniffs.
*
* @return array
*/
public function getIncludedSniffs()
{
return array(
'PEAR',
'Squiz',
'Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php',
);
}//end getIncludedSniffs()
/**
* Return a list of external sniffs to exclude from this standard.
*
* Including a whole standards above, individual Sniffs can then be removed here.
*
* @return array
*/
public function getExcludedSniffs()
{
return array(
'PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php',
'PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php',
'PEAR/Sniffs/Files/LineLengthSniff.php',
'PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php',
'PEAR/Sniffs/WhiteSpace/ScopeIndentSniff.php',
'Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php',
'Squiz/Sniffs/Commenting/InlineCommentSniff.php',
'Squiz/Sniffs/Commenting/PostStatementCommentSniff.php',
'Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php',
'Squiz/Sniffs/Files/LineLengthSniff.php',
'Squiz/Sniffs/Formatting/MultipleStatementAlignmentSniff.php',
'Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php',
'Squiz/Sniffs/Operators/ComparisonOperatorUsageSniff.php',
'Squiz/Sniffs/PHP/DisallowSizeFunctionsInLoopsSniff.php',
'Squiz/Sniffs/PHP/GlobalKeywordSniff.php',
'Squiz/Sniffs/Strings/ConcatenationSpacingSniff.php',
'Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php',
'Squiz/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php',
'Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php',
'Squiz/Sniffs/WhiteSpace/ObjectOperatorSpacingSniff.php',
'Squiz/Sniffs/WhiteSpace/ObjectOperatorSpacingSniff.php',
'Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
'Squiz/Sniffs/WhiteSpace/ScopeIndentSniff.php',
'Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php',
);
}//end getExcludedSniffs()
}
?>
Now many of those sniffs may be perfectly OK, but on the code base I was working with they produced too much noise, so I just excluded them for now. As the code base improves, I hope to add some of them back in. There also may be a mutually exclusive conflict with the K&R function brace and mixing PEAR and Squiz, but hey, this is a work in progress!