IndentationSniff.php in Coder 7.2
File
coder_sniffer/Drupal/Sniffs/CSS/IndentationSniff.php
View source
<?php
class Drupal_Sniffs_CSS_IndentationSniff implements PHP_CodeSniffer_Sniff {
public $supportedTokenizers = array(
'CSS',
);
public $indent = 2;
public function register() {
return array(
T_OPEN_TAG,
);
}
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
$numTokens = count($tokens) - 2;
$currentLine = 0;
$indentLevel = 0;
$nested = false;
for ($i = 1; $i < $numTokens; $i++) {
if ($tokens[$i]['code'] === T_COMMENT) {
continue;
}
if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
$indentLevel++;
$end = $tokens[$i]['bracket_closer'];
$nested = $phpcsFile
->findNext(T_OPEN_CURLY_BRACKET, $stackPtr + 1, $end);
}
else {
if ($tokens[$i + 1]['code'] === T_CLOSE_CURLY_BRACKET) {
$indentLevel--;
}
}
if ($tokens[$i]['line'] === $currentLine) {
continue;
}
if ($tokens[$i]['code'] === T_WHITESPACE) {
$content = str_replace($phpcsFile->eolChar, '', $tokens[$i]['content']);
$foundIndent = strlen($content);
}
else {
$foundIndent = 0;
}
$expectedIndent = $indentLevel * $this->indent;
if ($expectedIndent > 0 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
if ($nested === false) {
$error = 'Blank lines are not allowed in class definitions';
$phpcsFile
->addError($error, $i, 'BlankLine');
}
}
else {
if ($foundIndent !== $expectedIndent) {
$error = 'Line indented incorrectly; expected %s spaces, found %s';
$data = array(
$expectedIndent,
$foundIndent,
);
$phpcsFile
->addError($error, $i, 'Incorrect', $data);
}
}
$currentLine = $tokens[$i]['line'];
}
}
}
Classes
Name |
Description |
Drupal_Sniffs_CSS_IndentationSniff |
Copied from Squiz_Sniffs_CSS_IndentationSniff. Unfortunately we cannot use that
class directly because it has the 4 spaces condition hardcoded. |