PHP CodeSniffer Coding standard setup

Ignore-this: a7df997caaa37d97734bbf2afd969e0d

A first try to define a testable coding standard for DokuWiki. This is still
missing a few tests and also fails for a lot of things in DokuWiki.

darcs-hash:20091018172036-7ad00-7edf5cb732f82d6ad7d1112a24d0c0b8b2d6452f.gz
This commit is contained in:
Andreas Gohr 2009-10-18 19:20:36 +02:00
parent 2709b4569c
commit 673c04e7e3
3 changed files with 178 additions and 0 deletions

View file

@ -0,0 +1,79 @@
<?php
/**
* DokuWiki Coding Standard.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Andreas Gohr <andi@splitbrain.org>
*/
if (class_exists('PHP_CodeSniffer_Standards_CodingStandard', true) === false) {
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_CodingStandard not found');
}
/**
* DokuWiki Coding Standard.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Andreas Gohr <andi@splitbrain.org>
*/
class PHP_CodeSniffer_Standards_DokuWiki_DokuWikiCodingStandard extends PHP_CodeSniffer_Standards_CodingStandard {
/**
* Return a list of external sniffs to include with this standard.
*
* @return array
*/
public function getIncludedSniffs() {
return array(
'Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
'Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php',
'Generic/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php',
'Generic/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php',
'Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php',
'Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php',
'Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php',
'Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php',
'Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php',
'Generic/Sniffs/Commenting/TodoSniff.php',
'Generic/Sniffs/Files/LineEndingsSniff.php',
'Generic/Sniffs/Formatting/DisallowMultipleStatementsSniff.php',
'Generic/Sniffs/Formatting/NoSpaceAfterCastSniff.php',
'Generic/Sniffs/Metrics/NestingLevelSniff.php',
'Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php',
'Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php',
'Generic/Sniffs/PHP/LowerCaseConstantSniff.php',
'Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php',
'Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php',
'Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php',
'Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php',
'Zend/Sniffs/Files/ClosingTagSniff.php',
'PEAR/Sniffs/Functions/ValidDefaultValueSniff.php',
'Squiz/Sniffs/PHP/EvalSniff.php',
'Squiz/Sniffs/PHP/NonExecutableCodeSniff.php',
'Squiz/Sniffs/PHP/CommentedOutCodeSniff.php',
'Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php',
'Squiz/Sniffs/CSS/LowercaseStyleDefinitionSniff.php',
'Squiz/Sniffs/CSS/MissingColonSniff.php',
'Squiz/Sniffs/CSS/DisallowMultipleStyleDefinitionsSniff.php',
'Squiz/Sniffs/CSS/ColonSpacingSniff.php',
'Squiz/Sniffs/CSS/ClassDefinitionClosingBraceSpaceSniff.php',
'Squiz/Sniffs/CSS/SemicolonSpacingSniff.php',
'Squiz/Sniffs/CSS/IndentationSniff.php',
'Squiz/Sniffs/CSS/EmptyClassDefinitionSniff.php',
'Squiz/Sniffs/CSS/ClassDefinitionNameSpacingSniff.php',
'Squiz/Sniffs/CSS/EmptyStyleDefinitionSniff.php',
'Squiz/Sniffs/CSS/OpacitySniff.php',
'Squiz/Sniffs/CSS/ColourDefinitionSniff.php',
'Squiz/Sniffs/CSS/DuplicateClassDefinitionSniff.php',
'Squiz/Sniffs/CSS/ClassDefinitionOpeningBraceSpaceSniff.php',
'Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php',
);
}
}//end class

View file

@ -0,0 +1,81 @@
<?php
/**
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
*/
class DokuWiki_Sniffs_Functions_OpeningFunctionBraceSniff implements PHP_CodeSniffer_Sniff {
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return void
*/
public function register()
{
return array(T_FUNCTION);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
$openingBrace = $tokens[$stackPtr]['scope_opener'];
// The end of the function occurs at the end of the argument list. Its
// like this because some people like to break long function declarations
// over multiple lines.
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
$braceLine = $tokens[$openingBrace]['line'];
$lineDifference = ($braceLine - $functionLine);
if ($lineDifference > 0) {
$error = 'Opening brace should be on the same line as the declaration';
$phpcsFile->addError($error, $openingBrace);
return;
}
// Checks that the closing parenthesis and the opening brace are
// separated by a whitespace character.
$closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column'];
$braceColumn = $tokens[$openingBrace]['column'];
$columnDifference = ($braceColumn - $closerColumn);
if ($columnDifference > 2) {
$error = 'Expected 0 or 1 space between the closing parenthesis and the opening brace; found '.($columnDifference - 1).'.';
$phpcsFile->addError($error, $openingBrace);
return;
}
// Check that a tab was not used instead of a space.
$spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1);
$spaceContent = $tokens[$spaceTokenPtr]['content'];
if ($columnDifference == 2 && $spaceContent !== ' ') {
$error = 'Expected a none or a single space character between closing parenthesis and opening brace; found "'.$spaceContent.'".';
$phpcsFile->addError($error, $openingBrace);
return;
}
}//end process()
}//end class
?>

18
_cs/README Normal file
View file

@ -0,0 +1,18 @@
This directory contains the Coding Standard tests to be used with PHP
CodeSniffer on DokuWiki's code.
1. Install PHP CodeSniffer:
#> pear install PHP_CodeSniffer
2. Link the Coding Standard to the CodeSniffer directory:
#> ln -s /path/to/dokuwiki/_cs/DokuWiki /usr/share/pear/PHP/CodeSniffer/Standards/DokuWiki
3. Set DokuWiki to be the default standard:
#> phpcs --config-set default_standard DokuWiki
The coding standard is work in progress.