| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_CSS_ClassDefinitionNameSpacingSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: ClassDefinitionNameSpacingSniff.php 270273 2008-12-01 23:36:31Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Squiz_Sniffs_CSS_ClassDefinitionNameSpacingSniff.
|
|
|
18 |
*
|
|
|
19 |
* Ensure there are no blank lines between the names of classes/IDs.
|
|
|
20 |
*
|
|
|
21 |
* @category PHP
|
|
|
22 |
* @package PHP_CodeSniffer
|
|
|
23 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
24 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class Squiz_Sniffs_CSS_ClassDefinitionNameSpacingSniff implements PHP_CodeSniffer_Sniff
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* A list of tokenizers this sniff supports.
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
public $supportedTokenizers = array('CSS');
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Returns the token types that this sniff is interested in.
|
|
|
42 |
*
|
|
|
43 |
* @return array(int)
|
|
|
44 |
*/
|
|
|
45 |
public function register()
|
|
|
46 |
{
|
|
|
47 |
return array(T_OPEN_CURLY_BRACKET);
|
|
|
48 |
|
|
|
49 |
}//end register()
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Processes the tokens that this sniff is interested in.
|
|
|
54 |
*
|
|
|
55 |
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
|
|
|
56 |
* @param int $stackPtr The position in the stack where
|
|
|
57 |
* the token was found.
|
|
|
58 |
*
|
|
|
59 |
* @return void
|
|
|
60 |
*/
|
|
|
61 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
62 |
{
|
|
|
63 |
$tokens = $phpcsFile->getTokens();
|
|
|
64 |
|
|
|
65 |
// Find the first blank line before this openning brace, unless we get
|
|
|
66 |
// to another style definition, comment or the start of the file.
|
|
|
67 |
$endTokens = array(
|
|
|
68 |
T_CLOSE_CURLY_BRACKET,
|
|
|
69 |
T_COMMENT,
|
|
|
70 |
T_OPEN_TAG,
|
|
|
71 |
);
|
|
|
72 |
|
|
|
73 |
$foundContent = false;
|
|
|
74 |
$currentLine = $tokens[$stackPtr]['line'];
|
|
|
75 |
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
|
|
|
76 |
if (in_array($tokens[$i]['code'], $endTokens) === true) {
|
|
|
77 |
break;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if ($tokens[$i]['line'] === $currentLine) {
|
|
|
81 |
if ($tokens[$i]['code'] !== T_WHITESPACE) {
|
|
|
82 |
$foundContent = true;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
continue;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// We changed lines.
|
|
|
89 |
if ($foundContent === false) {
|
|
|
90 |
// Before we throw an error, make sure we are not looking
|
|
|
91 |
// at a gap before the style definition.
|
|
|
92 |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
|
|
|
93 |
if ($prev !== false
|
|
|
94 |
&& in_array($tokens[$prev]['code'], $endTokens) === false
|
|
|
95 |
) {
|
|
|
96 |
$error = 'Blank lines are not allowed between class names';
|
|
|
97 |
$phpcsFile->addError($error, ($i + 1));
|
|
|
98 |
}
|
|
|
99 |
break;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$foundContent = false;
|
|
|
103 |
$currentLine = $tokens[$i]['line'];
|
|
|
104 |
}//end for
|
|
|
105 |
|
|
|
106 |
}//end process()
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
}//end class
|
|
|
110 |
|
|
|
111 |
?>
|