| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: ControlStructureSpacingSniff.php 274897 2009-01-29 23:39:52Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
|
|
|
19 |
*
|
|
|
20 |
* Checks that any array declarations are lower case.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
26 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
27 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
28 |
* @version Release: 1.2.1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* A list of tokenizers this sniff supports.
|
|
|
36 |
*
|
|
|
37 |
* @var array
|
|
|
38 |
*/
|
|
|
39 |
public $supportedTokenizers = array(
|
|
|
40 |
'PHP',
|
|
|
41 |
'JS',
|
|
|
42 |
);
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Returns an array of tokens this test wants to listen for.
|
|
|
47 |
*
|
|
|
48 |
* @return array
|
|
|
49 |
*/
|
|
|
50 |
public function register()
|
|
|
51 |
{
|
|
|
52 |
return array(
|
|
|
53 |
T_IF,
|
|
|
54 |
T_WHILE,
|
|
|
55 |
T_FOREACH,
|
|
|
56 |
T_FOR,
|
|
|
57 |
T_SWITCH,
|
|
|
58 |
T_DO,
|
|
|
59 |
T_ELSE,
|
|
|
60 |
T_ELSEIF,
|
|
|
61 |
);
|
|
|
62 |
|
|
|
63 |
}//end register()
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Processes this test, when one of its tokens is encountered.
|
|
|
68 |
*
|
|
|
69 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
70 |
* @param int $stackPtr The position of the current token in the
|
|
|
71 |
* stack passed in $tokens.
|
|
|
72 |
*
|
|
|
73 |
* @return void
|
|
|
74 |
*/
|
|
|
75 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
76 |
{
|
|
|
77 |
$tokens = $phpcsFile->getTokens();
|
|
|
78 |
|
|
|
79 |
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
|
|
|
80 |
return;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$scopeCloser = $tokens[$stackPtr]['scope_closer'];
|
|
|
84 |
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
|
|
|
85 |
|
|
|
86 |
$firstContent = $phpcsFile->findNext(T_WHITESPACE, ($scopeOpener + 1), null, true);
|
|
|
87 |
if ($tokens[$firstContent]['line'] !== ($tokens[$scopeOpener]['line'] + 1)) {
|
|
|
88 |
$error = 'Blank line found at start of control structure';
|
|
|
89 |
$phpcsFile->addError($error, $scopeOpener);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($scopeCloser - 1), null, true);
|
|
|
93 |
if ($tokens[$lastContent]['line'] !== ($tokens[$scopeCloser]['line'] - 1)) {
|
|
|
94 |
$errorToken = $scopeCloser;
|
|
|
95 |
for ($i = ($scopeCloser - 1); $i > $lastContent; $i--) {
|
|
|
96 |
if ($tokens[$i]['line'] < $tokens[$scopeCloser]['line']) {
|
|
|
97 |
$errorToken = $i;
|
|
|
98 |
break;
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$error = 'Blank line found at end of control structure';
|
|
|
103 |
$phpcsFile->addError($error, $errorToken);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$trailingContent = $phpcsFile->findNext(T_WHITESPACE, ($scopeCloser + 1), null, true);
|
|
|
107 |
if ($tokens[$trailingContent]['code'] === T_ELSE) {
|
|
|
108 |
if ($tokens[$stackPtr]['code'] === T_IF) {
|
|
|
109 |
// IF with ELSE.
|
|
|
110 |
return;
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
if ($tokens[$trailingContent]['code'] === T_COMMENT) {
|
|
|
115 |
if ($tokens[$trailingContent]['line'] === $tokens[$scopeCloser]['line']) {
|
|
|
116 |
if (substr($tokens[$trailingContent]['content'], 0, 5) === '//end') {
|
|
|
117 |
// There is an end comment, so we have to get the next piece
|
|
|
118 |
// of content.
|
|
|
119 |
$trailingContent = $phpcsFile->findNext(T_WHITESPACE, ($trailingContent + 1), null, true);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if ($tokens[$trailingContent]['code'] === T_BREAK) {
|
|
|
125 |
// If this BREAK is closing a CASE, we don't need the
|
|
|
126 |
// blank line after this control structure.
|
|
|
127 |
if (isset($tokens[$trailingContent]['scope_condition']) === true) {
|
|
|
128 |
$condition = $tokens[$trailingContent]['scope_condition'];
|
|
|
129 |
if ($tokens[$condition]['code'] === T_CASE || $tokens[$condition]['code'] === T_DEFAULT) {
|
|
|
130 |
return;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if ($tokens[$trailingContent]['code'] === T_CLOSE_TAG) {
|
|
|
136 |
// At the end of the script or embedded code.
|
|
|
137 |
return;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
if ($tokens[$trailingContent]['code'] === T_CLOSE_CURLY_BRACKET) {
|
|
|
141 |
// Another control structure's closing brace.
|
|
|
142 |
if (isset($tokens[$trailingContent]['scope_condition']) === true) {
|
|
|
143 |
$owner = $tokens[$trailingContent]['scope_condition'];
|
|
|
144 |
if ($tokens[$owner]['code'] === T_FUNCTION) {
|
|
|
145 |
// The next content is the closing brace of a function
|
|
|
146 |
// so normal function rules apply and we can ignore it.
|
|
|
147 |
return;
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
if ($tokens[$trailingContent]['line'] !== ($tokens[$scopeCloser]['line'] + 1)) {
|
|
|
152 |
$error = 'Blank line found after control structure';
|
|
|
153 |
$phpcsFile->addError($error, $scopeCloser);
|
|
|
154 |
}
|
|
|
155 |
} else {
|
|
|
156 |
if ($tokens[$trailingContent]['line'] === ($tokens[$scopeCloser]['line'] + 1)) {
|
|
|
157 |
$error = 'No blank line found after control structure';
|
|
|
158 |
$phpcsFile->addError($error, $scopeCloser);
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
}//end process()
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
}//end class
|
|
|
166 |
|
|
|
167 |
?>
|