Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_CSS_ClassDefinitionNameSpacingSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Ensure there are no blank lines between the names of classes/IDs. Copied from
|
Chris@0
|
12 * Squiz_Sniffs_CSS_ClassDefinitionNameSpacingSniff because we also check for comma
|
Chris@0
|
13 * separated selectors on their own line.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @category PHP
|
Chris@0
|
16 * @package PHP_CodeSniffer
|
Chris@0
|
17 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
18 */
|
Chris@0
|
19 class Drupal_Sniffs_CSS_ClassDefinitionNameSpacingSniff implements PHP_CodeSniffer_Sniff
|
Chris@0
|
20 {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * A list of tokenizers this sniff supports.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var array
|
Chris@0
|
26 */
|
Chris@0
|
27 public $supportedTokenizers = array('CSS');
|
Chris@0
|
28
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Returns the token types that this sniff is interested in.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @return int[]
|
Chris@0
|
34 */
|
Chris@0
|
35 public function register()
|
Chris@0
|
36 {
|
Chris@0
|
37 return array(T_OPEN_CURLY_BRACKET);
|
Chris@0
|
38
|
Chris@0
|
39 }//end register()
|
Chris@0
|
40
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Processes the tokens that this sniff is interested in.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
|
Chris@0
|
46 * @param int $stackPtr The position in the stack where
|
Chris@0
|
47 * the token was found.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return void
|
Chris@0
|
50 */
|
Chris@0
|
51 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
52 {
|
Chris@0
|
53 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
54
|
Chris@0
|
55 // Do not check nested style definitions as, for example, in @media style rules.
|
Chris@0
|
56 $nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']);
|
Chris@0
|
57 if ($nested !== false) {
|
Chris@0
|
58 return;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 // Find the first blank line before this opening brace, unless we get
|
Chris@0
|
62 // to another style definition, comment or the start of the file.
|
Chris@0
|
63 $endTokens = array(
|
Chris@0
|
64 T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
|
Chris@0
|
65 T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
|
Chris@0
|
66 T_OPEN_TAG => T_OPEN_TAG,
|
Chris@0
|
67 );
|
Chris@0
|
68 $endTokens += PHP_CodeSniffer_Tokens::$commentTokens;
|
Chris@0
|
69
|
Chris@0
|
70 $foundContent = false;
|
Chris@0
|
71 $currentLine = $tokens[$stackPtr]['line'];
|
Chris@0
|
72 for ($i = ($stackPtr - 1); $i >= 0; $i--) {
|
Chris@0
|
73 if (isset($endTokens[$tokens[$i]['code']]) === true) {
|
Chris@0
|
74 break;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 // A comma must be followed by a new line character.
|
Chris@0
|
78 if ($tokens[$i]['code'] === T_COMMA
|
Chris@0
|
79 && strpos($tokens[($i + 1)]['content'], $phpcsFile->eolChar) === false
|
Chris@0
|
80 ) {
|
Chris@0
|
81 $error = 'Multiple selectors should each be on a single line';
|
Chris@0
|
82 $fix = $phpcsFile->addFixableError($error, ($i + 1), 'MultipleSelectors');
|
Chris@0
|
83 if ($fix === true) {
|
Chris@0
|
84 $phpcsFile->fixer->addNewline($i);
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 // Selectors must be on the same line.
|
Chris@0
|
89 if ($tokens[$i]['code'] === T_WHITESPACE
|
Chris@0
|
90 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false
|
Chris@0
|
91 && isset($endTokens[$tokens[($i - 1)]['code']]) === false
|
Chris@0
|
92 && in_array($tokens[($i - 1)]['code'], array(T_WHITESPACE, T_COMMA)) === false
|
Chris@0
|
93 ) {
|
Chris@0
|
94 $error = 'Selectors must be on a single line';
|
Chris@0
|
95 $fix = $phpcsFile->addFixableError($error, $i, 'SeletorSingleLine');
|
Chris@0
|
96 if ($fix === true) {
|
Chris@0
|
97 $phpcsFile->fixer->replaceToken($i, str_replace($phpcsFile->eolChar, ' ', $tokens[$i]['content']));
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 if ($tokens[$i]['line'] === $currentLine) {
|
Chris@0
|
102 if ($tokens[$i]['code'] !== T_WHITESPACE) {
|
Chris@0
|
103 $foundContent = true;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 continue;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 // We changed lines.
|
Chris@0
|
110 if ($foundContent === false) {
|
Chris@0
|
111 // Before we throw an error, make sure we are not looking
|
Chris@0
|
112 // at a gap before the style definition.
|
Chris@0
|
113 $prev = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
|
Chris@0
|
114 if ($prev !== false
|
Chris@0
|
115 && isset($endTokens[$tokens[$prev]['code']]) === false
|
Chris@0
|
116 ) {
|
Chris@0
|
117 $error = 'Blank lines are not allowed between class names';
|
Chris@0
|
118 $fix = $phpcsFile->addFixableError($error, ($i + 1), 'BlankLinesFound');
|
Chris@0
|
119 if ($fix === true) {
|
Chris@0
|
120 $phpcsFile->fixer->replaceToken(($i + 1), '');
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 break;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 $foundContent = false;
|
Chris@0
|
128 $currentLine = $tokens[$i]['line'];
|
Chris@0
|
129 }//end for
|
Chris@0
|
130
|
Chris@0
|
131 }//end process()
|
Chris@0
|
132
|
Chris@0
|
133
|
Chris@0
|
134 }//end class
|