Commit f77a0a9b authored by josephdpurcell's avatar josephdpurcell

Add .codeclimate.yml

parent 0c5527f9
---
engines:
phpmd:
enabled: true
config:
file_extensions: "php,inc,module"
rulesets: ".phpmd.xml"
phpcodesniffer:
enabled: true
config:
file_extensions: "php,inc,module"
encoding: utf-8
standard: ".phpcs/Drupal/ruleset.xml"
ignore_warnings: true
ratings:
paths:
- "./*.module"
- "./*.install"
- "./src/**/*.php"
exclude_paths:
- .phpcs
- "**/Test/**/*"
- config
- css
- templates
- tests
<documentation title="Parameter Spacing in Function Calls">
<standard>
<![CDATA[
There must be one space between a comma and a parameter in a function call.
]]>
</standard>
<code_comparison>
<code title="Valid: spaces between parameters">
<![CDATA[
$var = foo($bar<em>, </em>$baz<em>, </em>$quux);
]]>
</code>
<code title="Invalid: no space between commas and parameters">
<![CDATA[
$var = foo($bar<em>,</em>$baz<em>,</em>$quux);
]]>
</code>
</code_comparison>
</documentation>
<documentation title="Function Calls">
<standard>
<![CDATA[
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; and no space between the last parameter, the closing parenthesis, and the semicolon.
]]>
</standard>
<code_comparison>
<code title="Valid: spaces between parameters">
<![CDATA[
$var = foo($bar, $baz, $quux);
]]>
</code>
<code title="Invalid: additional spaces used">
<![CDATA[
$var = foo<em> </em>(<em> </em>$bar, $baz, $quux<em> </em>)<em> </em>;
]]>
</code>
</code_comparison>
</documentation>
<documentation title="Default Values in Function Declarations">
<standard>
<![CDATA[
Arguments with default values go at the end of the argument list.
]]>
</standard>
<code_comparison>
<code title="Valid: argument with default value at end of declaration">
<![CDATA[
function connect($dsn, <em>$persistent = false</em>)
{
...
}
]]>
</code>
<code title="Invalid: argument with default value at start of declaration">
<![CDATA[
function connect(<em>$persistent = false</em>, $dsn)
{
...
}
]]>
</code>
</code_comparison>
</documentation>
<?php
/**
* Drupal_Sniffs_Array_ArraySniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Drupal_Sniffs_Array_ArraySniff.
*
* Checks if the array's are styled in the Drupal way.
* - Comma after the last array element
* - Indentation is 2 spaces for multi line array definitions
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Drupal_Sniffs_Array_ArraySniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_ARRAY,
T_OPEN_SHORT_ARRAY,
);
}//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();
// Support long and short syntax.
$parenthesis_opener = 'parenthesis_opener';
$parenthesis_closer = 'parenthesis_closer';
if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
$parenthesis_opener = 'bracket_opener';
$parenthesis_closer = 'bracket_closer';
}
$lastItem = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
($tokens[$stackPtr][$parenthesis_closer] - 1),
$stackPtr,
true
);
// Empty array.
if ($lastItem === $tokens[$stackPtr][$parenthesis_opener]) {
return;
}
// Inline array.
$isInlineArray = $tokens[$tokens[$stackPtr][$parenthesis_opener]]['line'] === $tokens[$tokens[$stackPtr][$parenthesis_closer]]['line'];
// Check if the last item in a multiline array has a "closing" comma.
if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false
&& $tokens[($lastItem + 1)]['code'] !== T_CLOSE_PARENTHESIS
&& $tokens[($lastItem + 1)]['code'] !== T_CLOSE_SHORT_ARRAY
) {
$data = array($tokens[$lastItem]['content']);
$fix = $phpcsFile->addFixableWarning('A comma should follow the last multiline array item. Found: %s', $lastItem, 'CommaLastItem', $data);
if ($fix === true) {
$phpcsFile->fixer->addContent($lastItem, ',');
}
return;
}
if ($isInlineArray === true) {
// Check if this array contains at least 3 elements and exceeds the 80
// character line length.
if ($tokens[$tokens[$stackPtr][$parenthesis_closer]]['column'] > 80) {
$comma1 = $phpcsFile->findNext(T_COMMA, ($stackPtr + 1), $tokens[$stackPtr][$parenthesis_closer]);
if ($comma1 !== false) {
$comma2 = $phpcsFile->findNext(T_COMMA, ($comma1 + 1), $tokens[$stackPtr][$parenthesis_closer]);
if ($comma2 !== false) {
$error = 'If the line declaring an array spans longer than 80 characters, each element should be broken into its own line';
$phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration');
}
}
}
// Only continue for multi line arrays.
return;
}
// Find the first token on this line.
$firstLineColumn = $tokens[$stackPtr]['column'];
for ($i = $stackPtr; $i >= 0; $i--) {
// If there is a PHP open tag then this must be a template file where we
// don't check indentation.
if ($tokens[$i]['code'] === T_OPEN_TAG) {
return;
}
// Record the first code token on the line.
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$firstLineColumn = $tokens[$i]['column'];
// This could be a multi line string or comment beginning with white
// spaces.
$trimmed = ltrim($tokens[$i]['content']);
if ($trimmed !== $tokens[$i]['content']) {
$firstLineColumn = ($firstLineColumn + strpos($tokens[$i]['content'], $trimmed));
}
}
// It's the start of the line, so we've found our first php token.
if ($tokens[$i]['column'] === 1) {
break;
}
}//end for
$lineStart = $stackPtr;
// Iterate over all lines of this array.
while ($lineStart < $tokens[$stackPtr][$parenthesis_closer]) {
// Find next line start.
$newLineStart = $lineStart;
$current_line = $tokens[$newLineStart]['line'];
while ($current_line >= $tokens[$newLineStart]['line']) {
$newLineStart = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
($newLineStart + 1),
($tokens[$stackPtr][$parenthesis_closer] + 1),
true
);
if ($newLineStart === false) {
break 2;
}
// Long array syntax: Skip nested arrays, they are checked in a next
// run.
if ($tokens[$newLineStart]['code'] === T_ARRAY) {
$newLineStart = $tokens[$newLineStart]['parenthesis_closer'];
$current_line = $tokens[$newLineStart]['line'];
}
// Short array syntax: Skip nested arrays, they are checked in a next
// run.
if ($tokens[$newLineStart]['code'] === T_OPEN_SHORT_ARRAY) {
$newLineStart = $tokens[$newLineStart]['bracket_closer'];
$current_line = $tokens[$newLineStart]['line'];
}
// Nested structures such as closures: skip those, they are checked
// in other sniffs. If the conditions of a token are different it
// means that it is in a different nesting level.
if ($tokens[$newLineStart]['conditions'] !== $tokens[$stackPtr]['conditions']) {
$current_line++;
}
}//end while
if ($newLineStart === $tokens[$stackPtr][$parenthesis_closer]) {
// End of the array reached.
if ($tokens[$newLineStart]['column'] !== $firstLineColumn) {
$error = 'Array closing indentation error, expected %s spaces but found %s';
$data = array(
$firstLineColumn - 1,
$tokens[$newLineStart]['column'] - 1,
);
$fix = $phpcsFile->addFixableError($error, $newLineStart, 'ArrayClosingIndentation', $data);
if ($fix === true) {
if ($tokens[$newLineStart]['column'] === 1) {
$phpcsFile->fixer->addContentBefore($newLineStart, str_repeat(' ', ($firstLineColumn - 1)));
} else {
$phpcsFile->fixer->replaceToken(($newLineStart - 1), str_repeat(' ', ($firstLineColumn - 1)));
}
}
}
break;
}
$expectedColumn = ($firstLineColumn + 2);
// If the line starts with "->" then we assume an additional level of
// indentation.
if ($tokens[$newLineStart]['code'] === T_OBJECT_OPERATOR) {
$expectedColumn += 2;
}
if ($tokens[$newLineStart]['column'] !== $expectedColumn) {
// Skip lines in nested structures such as a function call within an
// array, no defined coding standard for those.
$innerNesting = empty($tokens[$newLineStart]['nested_parenthesis']) === false
&& end($tokens[$newLineStart]['nested_parenthesis']) < $tokens[$stackPtr][$parenthesis_closer];
// Skip lines that are part of a multi-line string.
$isMultiLineString = $tokens[($newLineStart - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING
&& substr($tokens[($newLineStart - 1)]['content'], -1) === $phpcsFile->eolChar;
if ($innerNesting === false && $isMultiLineString === false) {
$error = 'Array indentation error, expected %s spaces but found %s';
$data = array(
$expectedColumn - 1,
$tokens[$newLineStart]['column'] - 1,
);
$fix = $phpcsFile->addFixableError($error, $newLineStart, 'ArrayIndentation', $data);
if ($fix === true) {
if ($tokens[$newLineStart]['column'] === 1) {
$phpcsFile->fixer->addContentBefore($newLineStart, str_repeat(' ', ($expectedColumn - 1)));
} else {
$phpcsFile->fixer->replaceToken(($newLineStart - 1), str_repeat(' ', ($expectedColumn - 1)));
}
}
}
}//end if
$lineStart = $newLineStart;
}//end while
}//end process()
}//end class
<?php
/**
* Drupal_Sniffs_CSS_ClassDefinitionNameSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Ensure there are no blank lines between the names of classes/IDs. Copied from
* Squiz_Sniffs_CSS_ClassDefinitionNameSpacingSniff because we also check for comma
* separated selectors on their own line.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Drupal_Sniffs_CSS_ClassDefinitionNameSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array('CSS');
/**
* Returns the token types that this sniff is interested in.
*
* @return int[]
*/
public function register()
{
return array(T_OPEN_CURLY_BRACKET);
}//end register()
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Do not check nested style definitions as, for example, in @media style rules.
$nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']);
if ($nested !== false) {
return;
}
// Find the first blank line before this opening brace, unless we get
// to another style definition, comment or the start of the file.
$endTokens = array(
T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
T_OPEN_TAG => T_OPEN_TAG,
);
$endTokens += PHP_CodeSniffer_Tokens::$commentTokens;
$foundContent = false;
$currentLine = $tokens[$stackPtr]['line'];
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
if (isset($endTokens[$tokens[$i]['code']]) === true) {
break;
}
// A comma must be followed by a new line character.
if ($tokens[$i]['code'] === T_COMMA
&& strpos($tokens[($i + 1)]['content'], $phpcsFile->eolChar) === false
) {
$error = 'Multiple selectors should each be on a single line';
$fix = $phpcsFile->addFixableError($error, ($i + 1), 'MultipleSelectors');
if ($fix === true) {
$phpcsFile->fixer->addNewline($i);
}
}
// Selectors must be on the same line.
if ($tokens[$i]['code'] === T_WHITESPACE
&& strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false
&& isset($endTokens[$tokens[($i - 1)]['code']]) === false
&& in_array($tokens[($i - 1)]['code'], array(T_WHITESPACE, T_COMMA)) === false
) {
$error = 'Selectors must be on a single line';
$fix = $phpcsFile->addFixableError($error, $i, 'SeletorSingleLine');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($i, str_replace($phpcsFile->eolChar, ' ', $tokens[$i]['content']));
}
}
if ($tokens[$i]['line'] === $currentLine) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$foundContent = true;
}
continue;
}
// We changed lines.
if ($foundContent === false) {
// Before we throw an error, make sure we are not looking
// at a gap before the style definition.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
if ($prev !== false
&& isset($endTokens[$tokens[$prev]['code']]) === false
) {
$error = 'Blank lines are not allowed between class names';
$fix = $phpcsFile->addFixableError($error, ($i + 1), 'BlankLinesFound');
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($i + 1), '');
}
}
break;
}
$foundContent = false;
$currentLine = $tokens[$i]['line'];
}//end for
}//end process()
}//end class
<?php
/**
* Drupal_Sniffs_CSS_ColourDefinitionSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_CSS_ColourDefinitionSniff.
*
* Ensure colours are defined in lower-case.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Drupal_Sniffs_CSS_ColourDefinitionSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array('CSS');
/**
* Returns the token types that this sniff is interested in.
*
* @return array(int)
*/
public function register()
{
return array(T_COLOUR);
}//end register()
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$colour = $tokens[$stackPtr]['content'];
$expected = strtolower($colour);
if ($colour !== $expected) {
$error = 'CSS colours must be defined in lowercase; expected %s but found %s';
$data = array(
$expected,
$colour,
);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotLower', $data);
if ($fix === true) {