3
* Squiz_Sniffs_CSS_IndentationSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Squiz_Sniffs_CSS_IndentationSniff.
18
* Ensures styles are indented 4 spaces.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@squiz.net>
23
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
* @version Release: 1.5.4
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class Squiz_Sniffs_CSS_IndentationSniff implements PHP_CodeSniffer_Sniff
32
* A list of tokenizers this sniff supports.
36
public $supportedTokenizers = array('CSS');
39
* The number of spaces code should be indented.
47
* Returns the token types that this sniff is interested in.
51
public function register()
53
return array(T_OPEN_TAG);
59
* Processes the tokens that this sniff is interested in.
61
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
62
* @param int $stackPtr The position in the stack where
63
* the token was found.
67
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69
$tokens = $phpcsFile->getTokens();
71
$numTokens = (count($tokens) - 2);
74
for ($i = 1; $i < $numTokens; $i++) {
75
if ($tokens[$i]['code'] === T_COMMENT) {
76
// Don't check the indent of comments.
80
if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
83
// Check for nested class definitions.
84
$found = $phpcsFile->findNext(
87
$tokens[$i]['bracket_closer']
89
if ($found !== false) {
90
$nestingLevel = $indentLevel;
92
} else if ($tokens[($i + 1)]['code'] === T_CLOSE_CURLY_BRACKET) {
96
if ($tokens[$i]['column'] !== 1) {
100
// We started a new line, so check indent.
101
if ($tokens[$i]['code'] === T_WHITESPACE) {
102
$content = str_replace($phpcsFile->eolChar, '', $tokens[$i]['content']);
103
$foundIndent = strlen($content);
108
$expectedIndent = ($indentLevel * $this->indent);
109
if ($expectedIndent > 0
110
&& strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false
112
if ($nestingLevel !== $indentLevel) {
113
$error = 'Blank lines are not allowed in class definitions';
114
$phpcsFile->addError($error, $i, 'BlankLine');
116
} else if ($foundIndent !== $expectedIndent) {
117
$error = 'Line indented incorrectly; expected %s spaces, found %s';
122
$phpcsFile->addError($error, $i, 'Incorrect', $data);