@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | /** |
33 | 33 | * Returns an array of tokens this test wants to listen for. |
34 | 34 | * |
35 | - * @return array |
|
35 | + * @return integer[] |
|
36 | 36 | */ |
37 | 37 | public function register() |
38 | 38 | { |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | * @param int $stackPtr The position of the current token in |
192 | 192 | * the stack passed in $tokens. |
193 | 193 | * |
194 | - * @return void |
|
194 | + * @return boolean |
|
195 | 195 | */ |
196 | 196 | private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
197 | 197 | { |
@@ -29,188 +29,188 @@ |
||
29 | 29 | { |
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * Returns an array of tokens this test wants to listen for. |
|
34 | - * |
|
35 | - * @return array |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - return array(T_USE); |
|
40 | - |
|
41 | - }//end register() |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Processes this test, when one of its tokens is encountered. |
|
46 | - * |
|
47 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
48 | - * @param int $stackPtr The position of the current token in |
|
49 | - * the stack passed in $tokens. |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
54 | - { |
|
55 | - if ($this->_shouldIgnoreUse($phpcsFile, $stackPtr) === true) { |
|
56 | - return; |
|
57 | - } |
|
58 | - |
|
59 | - $tokens = $phpcsFile->getTokens(); |
|
60 | - |
|
61 | - // One space after the use keyword. |
|
62 | - if ($tokens[($stackPtr + 1)]['content'] !== ' ') { |
|
63 | - $error = 'There must be a single space after the USE keyword'; |
|
64 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse'); |
|
65 | - if ($fix === true) { |
|
66 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - // Only one USE declaration allowed per statement. |
|
71 | - $next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP), ($stackPtr + 1)); |
|
72 | - if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
73 | - $error = 'There must be one USE keyword per declaration'; |
|
74 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations'); |
|
75 | - if ($fix === true) { |
|
76 | - if ($tokens[$next]['code'] === T_COMMA) { |
|
77 | - $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use '); |
|
78 | - } else { |
|
79 | - $baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr))); |
|
80 | - $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1)); |
|
81 | - |
|
82 | - $phpcsFile->fixer->beginChangeset(); |
|
83 | - |
|
84 | - // Remove base use statement. |
|
85 | - for ($i = $stackPtr; $i <= $next; $i++) { |
|
86 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
87 | - } |
|
88 | - |
|
89 | - // Convert grouped use statements into full use statements. |
|
90 | - do { |
|
91 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), $closingCurly, true); |
|
92 | - |
|
93 | - $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true); |
|
94 | - for ($i = ($whitespace + 1); $i < $next; $i++) { |
|
95 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
96 | - } |
|
97 | - |
|
98 | - if ($tokens[$next]['code'] === T_CONST || $tokens[$next]['code'] === T_FUNCTION) { |
|
99 | - $phpcsFile->fixer->addContentBefore($next, 'use '); |
|
100 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), $closingCurly, true); |
|
101 | - $phpcsFile->fixer->addContentBefore($next, str_replace('use ', '', $baseUse)); |
|
102 | - } else { |
|
103 | - $phpcsFile->fixer->addContentBefore($next, $baseUse); |
|
104 | - } |
|
105 | - |
|
106 | - $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); |
|
107 | - if ($next !== false) { |
|
108 | - $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar); |
|
109 | - } |
|
110 | - } while ($next !== false); |
|
111 | - |
|
112 | - $phpcsFile->fixer->replaceToken($closingCurly, ''); |
|
113 | - |
|
114 | - // Remove any trailing whitespace. |
|
115 | - $next = $phpcsFile->findNext(T_SEMICOLON, $closingCurly); |
|
116 | - $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingCurly - 1), null, true); |
|
117 | - for ($i = ($whitespace + 1); $i < $next; $i++) { |
|
118 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
119 | - } |
|
120 | - |
|
121 | - $phpcsFile->fixer->endChangeset(); |
|
122 | - }//end if |
|
123 | - }//end if |
|
124 | - }//end if |
|
125 | - |
|
126 | - // Make sure this USE comes after the first namespace declaration. |
|
127 | - $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); |
|
128 | - if ($prev !== false) { |
|
129 | - $first = $phpcsFile->findNext(T_NAMESPACE, 1); |
|
130 | - if ($prev !== $first) { |
|
131 | - $error = 'USE declarations must go after the first namespace declaration'; |
|
132 | - $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - // Only interested in the last USE statement from here onwards. |
|
137 | - $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); |
|
138 | - while ($this->_shouldIgnoreUse($phpcsFile, $nextUse) === true) { |
|
139 | - $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1)); |
|
140 | - if ($nextUse === false) { |
|
141 | - break; |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - if ($nextUse !== false) { |
|
146 | - return; |
|
147 | - } |
|
148 | - |
|
149 | - $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
|
150 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); |
|
151 | - |
|
152 | - if ($tokens[$next]['code'] === T_CLOSE_TAG) { |
|
153 | - return; |
|
154 | - } |
|
155 | - |
|
156 | - $diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); |
|
157 | - if ($diff !== 1) { |
|
158 | - if ($diff < 0) { |
|
159 | - $diff = 0; |
|
160 | - } |
|
161 | - |
|
162 | - $error = 'There must be one blank line after the last USE statement; %s found;'; |
|
163 | - $data = array($diff); |
|
164 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data); |
|
165 | - if ($fix === true) { |
|
166 | - if ($diff === 0) { |
|
167 | - $phpcsFile->fixer->addNewline($end); |
|
168 | - } else { |
|
169 | - $phpcsFile->fixer->beginChangeset(); |
|
170 | - for ($i = ($end + 1); $i < $next; $i++) { |
|
171 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
172 | - break; |
|
173 | - } |
|
174 | - |
|
175 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
176 | - } |
|
177 | - |
|
178 | - $phpcsFile->fixer->addNewline($end); |
|
179 | - $phpcsFile->fixer->endChangeset(); |
|
180 | - } |
|
181 | - } |
|
182 | - }//end if |
|
183 | - |
|
184 | - }//end process() |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * Check if this use statement is part of the namespace block. |
|
189 | - * |
|
190 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
191 | - * @param int $stackPtr The position of the current token in |
|
192 | - * the stack passed in $tokens. |
|
193 | - * |
|
194 | - * @return void |
|
195 | - */ |
|
196 | - private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
197 | - { |
|
198 | - $tokens = $phpcsFile->getTokens(); |
|
199 | - |
|
200 | - // Ignore USE keywords inside closures. |
|
201 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
202 | - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
203 | - return true; |
|
204 | - } |
|
205 | - |
|
206 | - // Ignore USE keywords for traits. |
|
207 | - if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) { |
|
208 | - return true; |
|
209 | - } |
|
210 | - |
|
211 | - return false; |
|
212 | - |
|
213 | - }//end _shouldIgnoreUse() |
|
32 | + /** |
|
33 | + * Returns an array of tokens this test wants to listen for. |
|
34 | + * |
|
35 | + * @return array |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + return array(T_USE); |
|
40 | + |
|
41 | + }//end register() |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Processes this test, when one of its tokens is encountered. |
|
46 | + * |
|
47 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
48 | + * @param int $stackPtr The position of the current token in |
|
49 | + * the stack passed in $tokens. |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
54 | + { |
|
55 | + if ($this->_shouldIgnoreUse($phpcsFile, $stackPtr) === true) { |
|
56 | + return; |
|
57 | + } |
|
58 | + |
|
59 | + $tokens = $phpcsFile->getTokens(); |
|
60 | + |
|
61 | + // One space after the use keyword. |
|
62 | + if ($tokens[($stackPtr + 1)]['content'] !== ' ') { |
|
63 | + $error = 'There must be a single space after the USE keyword'; |
|
64 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse'); |
|
65 | + if ($fix === true) { |
|
66 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + // Only one USE declaration allowed per statement. |
|
71 | + $next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP), ($stackPtr + 1)); |
|
72 | + if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
73 | + $error = 'There must be one USE keyword per declaration'; |
|
74 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations'); |
|
75 | + if ($fix === true) { |
|
76 | + if ($tokens[$next]['code'] === T_COMMA) { |
|
77 | + $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use '); |
|
78 | + } else { |
|
79 | + $baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr))); |
|
80 | + $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1)); |
|
81 | + |
|
82 | + $phpcsFile->fixer->beginChangeset(); |
|
83 | + |
|
84 | + // Remove base use statement. |
|
85 | + for ($i = $stackPtr; $i <= $next; $i++) { |
|
86 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
87 | + } |
|
88 | + |
|
89 | + // Convert grouped use statements into full use statements. |
|
90 | + do { |
|
91 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), $closingCurly, true); |
|
92 | + |
|
93 | + $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true); |
|
94 | + for ($i = ($whitespace + 1); $i < $next; $i++) { |
|
95 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
96 | + } |
|
97 | + |
|
98 | + if ($tokens[$next]['code'] === T_CONST || $tokens[$next]['code'] === T_FUNCTION) { |
|
99 | + $phpcsFile->fixer->addContentBefore($next, 'use '); |
|
100 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), $closingCurly, true); |
|
101 | + $phpcsFile->fixer->addContentBefore($next, str_replace('use ', '', $baseUse)); |
|
102 | + } else { |
|
103 | + $phpcsFile->fixer->addContentBefore($next, $baseUse); |
|
104 | + } |
|
105 | + |
|
106 | + $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); |
|
107 | + if ($next !== false) { |
|
108 | + $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar); |
|
109 | + } |
|
110 | + } while ($next !== false); |
|
111 | + |
|
112 | + $phpcsFile->fixer->replaceToken($closingCurly, ''); |
|
113 | + |
|
114 | + // Remove any trailing whitespace. |
|
115 | + $next = $phpcsFile->findNext(T_SEMICOLON, $closingCurly); |
|
116 | + $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingCurly - 1), null, true); |
|
117 | + for ($i = ($whitespace + 1); $i < $next; $i++) { |
|
118 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
119 | + } |
|
120 | + |
|
121 | + $phpcsFile->fixer->endChangeset(); |
|
122 | + }//end if |
|
123 | + }//end if |
|
124 | + }//end if |
|
125 | + |
|
126 | + // Make sure this USE comes after the first namespace declaration. |
|
127 | + $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); |
|
128 | + if ($prev !== false) { |
|
129 | + $first = $phpcsFile->findNext(T_NAMESPACE, 1); |
|
130 | + if ($prev !== $first) { |
|
131 | + $error = 'USE declarations must go after the first namespace declaration'; |
|
132 | + $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + // Only interested in the last USE statement from here onwards. |
|
137 | + $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); |
|
138 | + while ($this->_shouldIgnoreUse($phpcsFile, $nextUse) === true) { |
|
139 | + $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1)); |
|
140 | + if ($nextUse === false) { |
|
141 | + break; |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + if ($nextUse !== false) { |
|
146 | + return; |
|
147 | + } |
|
148 | + |
|
149 | + $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
|
150 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); |
|
151 | + |
|
152 | + if ($tokens[$next]['code'] === T_CLOSE_TAG) { |
|
153 | + return; |
|
154 | + } |
|
155 | + |
|
156 | + $diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); |
|
157 | + if ($diff !== 1) { |
|
158 | + if ($diff < 0) { |
|
159 | + $diff = 0; |
|
160 | + } |
|
161 | + |
|
162 | + $error = 'There must be one blank line after the last USE statement; %s found;'; |
|
163 | + $data = array($diff); |
|
164 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data); |
|
165 | + if ($fix === true) { |
|
166 | + if ($diff === 0) { |
|
167 | + $phpcsFile->fixer->addNewline($end); |
|
168 | + } else { |
|
169 | + $phpcsFile->fixer->beginChangeset(); |
|
170 | + for ($i = ($end + 1); $i < $next; $i++) { |
|
171 | + if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
172 | + break; |
|
173 | + } |
|
174 | + |
|
175 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
176 | + } |
|
177 | + |
|
178 | + $phpcsFile->fixer->addNewline($end); |
|
179 | + $phpcsFile->fixer->endChangeset(); |
|
180 | + } |
|
181 | + } |
|
182 | + }//end if |
|
183 | + |
|
184 | + }//end process() |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * Check if this use statement is part of the namespace block. |
|
189 | + * |
|
190 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
191 | + * @param int $stackPtr The position of the current token in |
|
192 | + * the stack passed in $tokens. |
|
193 | + * |
|
194 | + * @return void |
|
195 | + */ |
|
196 | + private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
197 | + { |
|
198 | + $tokens = $phpcsFile->getTokens(); |
|
199 | + |
|
200 | + // Ignore USE keywords inside closures. |
|
201 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
202 | + if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
203 | + return true; |
|
204 | + } |
|
205 | + |
|
206 | + // Ignore USE keywords for traits. |
|
207 | + if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) { |
|
208 | + return true; |
|
209 | + } |
|
210 | + |
|
211 | + return false; |
|
212 | + |
|
213 | + }//end _shouldIgnoreUse() |
|
214 | 214 | |
215 | 215 | |
216 | 216 | }//end class |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | */ |
37 | 37 | public function register() |
38 | 38 | { |
39 | - return array(T_USE); |
|
39 | + return array( T_USE ); |
|
40 | 40 | |
41 | 41 | }//end register() |
42 | 42 | |
@@ -50,72 +50,72 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @return void |
52 | 52 | */ |
53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
53 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
54 | 54 | { |
55 | - if ($this->_shouldIgnoreUse($phpcsFile, $stackPtr) === true) { |
|
55 | + if ( $this->_shouldIgnoreUse( $phpcsFile, $stackPtr ) === true ) { |
|
56 | 56 | return; |
57 | 57 | } |
58 | 58 | |
59 | 59 | $tokens = $phpcsFile->getTokens(); |
60 | 60 | |
61 | 61 | // One space after the use keyword. |
62 | - if ($tokens[($stackPtr + 1)]['content'] !== ' ') { |
|
62 | + if ( $tokens[ ( $stackPtr + 1 ) ][ 'content' ] !== ' ' ) { |
|
63 | 63 | $error = 'There must be a single space after the USE keyword'; |
64 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse'); |
|
65 | - if ($fix === true) { |
|
66 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
64 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceAfterUse' ); |
|
65 | + if ( $fix === true ) { |
|
66 | + $phpcsFile->fixer->replaceToken( ( $stackPtr + 1 ), ' ' ); |
|
67 | 67 | } |
68 | 68 | } |
69 | 69 | |
70 | 70 | // Only one USE declaration allowed per statement. |
71 | - $next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP), ($stackPtr + 1)); |
|
72 | - if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
71 | + $next = $phpcsFile->findNext( array( T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP ), ( $stackPtr + 1 ) ); |
|
72 | + if ( $tokens[ $next ][ 'code' ] !== T_SEMICOLON ) { |
|
73 | 73 | $error = 'There must be one USE keyword per declaration'; |
74 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations'); |
|
75 | - if ($fix === true) { |
|
76 | - if ($tokens[$next]['code'] === T_COMMA) { |
|
77 | - $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use '); |
|
74 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'MultipleDeclarations' ); |
|
75 | + if ( $fix === true ) { |
|
76 | + if ( $tokens[ $next ][ 'code' ] === T_COMMA ) { |
|
77 | + $phpcsFile->fixer->replaceToken( $next, ';' . $phpcsFile->eolChar . 'use ' ); |
|
78 | 78 | } else { |
79 | - $baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr))); |
|
80 | - $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1)); |
|
79 | + $baseUse = rtrim( $phpcsFile->getTokensAsString( $stackPtr, ( $next - $stackPtr ) ) ); |
|
80 | + $closingCurly = $phpcsFile->findNext( T_CLOSE_USE_GROUP, ( $next + 1 ) ); |
|
81 | 81 | |
82 | 82 | $phpcsFile->fixer->beginChangeset(); |
83 | 83 | |
84 | 84 | // Remove base use statement. |
85 | - for ($i = $stackPtr; $i <= $next; $i++) { |
|
86 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
85 | + for ( $i = $stackPtr; $i <= $next; $i++ ) { |
|
86 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | // Convert grouped use statements into full use statements. |
90 | 90 | do { |
91 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), $closingCurly, true); |
|
91 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), $closingCurly, true ); |
|
92 | 92 | |
93 | - $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true); |
|
94 | - for ($i = ($whitespace + 1); $i < $next; $i++) { |
|
95 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
93 | + $whitespace = $phpcsFile->findPrevious( T_WHITESPACE, ( $next - 1 ), null, true ); |
|
94 | + for ( $i = ( $whitespace + 1 ); $i < $next; $i++ ) { |
|
95 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
96 | 96 | } |
97 | 97 | |
98 | - if ($tokens[$next]['code'] === T_CONST || $tokens[$next]['code'] === T_FUNCTION) { |
|
99 | - $phpcsFile->fixer->addContentBefore($next, 'use '); |
|
100 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), $closingCurly, true); |
|
101 | - $phpcsFile->fixer->addContentBefore($next, str_replace('use ', '', $baseUse)); |
|
98 | + if ( $tokens[ $next ][ 'code' ] === T_CONST || $tokens[ $next ][ 'code' ] === T_FUNCTION ) { |
|
99 | + $phpcsFile->fixer->addContentBefore( $next, 'use ' ); |
|
100 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), $closingCurly, true ); |
|
101 | + $phpcsFile->fixer->addContentBefore( $next, str_replace( 'use ', '', $baseUse ) ); |
|
102 | 102 | } else { |
103 | - $phpcsFile->fixer->addContentBefore($next, $baseUse); |
|
103 | + $phpcsFile->fixer->addContentBefore( $next, $baseUse ); |
|
104 | 104 | } |
105 | 105 | |
106 | - $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); |
|
107 | - if ($next !== false) { |
|
108 | - $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar); |
|
106 | + $next = $phpcsFile->findNext( T_COMMA, ( $next + 1 ), $closingCurly ); |
|
107 | + if ( $next !== false ) { |
|
108 | + $phpcsFile->fixer->replaceToken( $next, ';' . $phpcsFile->eolChar ); |
|
109 | 109 | } |
110 | - } while ($next !== false); |
|
110 | + } while ( $next !== false ); |
|
111 | 111 | |
112 | - $phpcsFile->fixer->replaceToken($closingCurly, ''); |
|
112 | + $phpcsFile->fixer->replaceToken( $closingCurly, '' ); |
|
113 | 113 | |
114 | 114 | // Remove any trailing whitespace. |
115 | - $next = $phpcsFile->findNext(T_SEMICOLON, $closingCurly); |
|
116 | - $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingCurly - 1), null, true); |
|
117 | - for ($i = ($whitespace + 1); $i < $next; $i++) { |
|
118 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
115 | + $next = $phpcsFile->findNext( T_SEMICOLON, $closingCurly ); |
|
116 | + $whitespace = $phpcsFile->findPrevious( T_WHITESPACE, ( $closingCurly - 1 ), null, true ); |
|
117 | + for ( $i = ( $whitespace + 1 ); $i < $next; $i++ ) { |
|
118 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | $phpcsFile->fixer->endChangeset(); |
@@ -124,58 +124,58 @@ discard block |
||
124 | 124 | }//end if |
125 | 125 | |
126 | 126 | // Make sure this USE comes after the first namespace declaration. |
127 | - $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); |
|
128 | - if ($prev !== false) { |
|
129 | - $first = $phpcsFile->findNext(T_NAMESPACE, 1); |
|
130 | - if ($prev !== $first) { |
|
127 | + $prev = $phpcsFile->findPrevious( T_NAMESPACE, ( $stackPtr - 1 ) ); |
|
128 | + if ( $prev !== false ) { |
|
129 | + $first = $phpcsFile->findNext( T_NAMESPACE, 1 ); |
|
130 | + if ( $prev !== $first ) { |
|
131 | 131 | $error = 'USE declarations must go after the first namespace declaration'; |
132 | - $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace'); |
|
132 | + $phpcsFile->addError( $error, $stackPtr, 'UseAfterNamespace' ); |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | |
136 | 136 | // Only interested in the last USE statement from here onwards. |
137 | - $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1)); |
|
138 | - while ($this->_shouldIgnoreUse($phpcsFile, $nextUse) === true) { |
|
139 | - $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1)); |
|
140 | - if ($nextUse === false) { |
|
137 | + $nextUse = $phpcsFile->findNext( T_USE, ( $stackPtr + 1 ) ); |
|
138 | + while ( $this->_shouldIgnoreUse( $phpcsFile, $nextUse ) === true ) { |
|
139 | + $nextUse = $phpcsFile->findNext( T_USE, ( $nextUse + 1 ) ); |
|
140 | + if ( $nextUse === false ) { |
|
141 | 141 | break; |
142 | 142 | } |
143 | 143 | } |
144 | 144 | |
145 | - if ($nextUse !== false) { |
|
145 | + if ( $nextUse !== false ) { |
|
146 | 146 | return; |
147 | 147 | } |
148 | 148 | |
149 | - $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
|
150 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true); |
|
149 | + $end = $phpcsFile->findNext( T_SEMICOLON, ( $stackPtr + 1 ) ); |
|
150 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $end + 1 ), null, true ); |
|
151 | 151 | |
152 | - if ($tokens[$next]['code'] === T_CLOSE_TAG) { |
|
152 | + if ( $tokens[ $next ][ 'code' ] === T_CLOSE_TAG ) { |
|
153 | 153 | return; |
154 | 154 | } |
155 | 155 | |
156 | - $diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1); |
|
157 | - if ($diff !== 1) { |
|
158 | - if ($diff < 0) { |
|
156 | + $diff = ( $tokens[ $next ][ 'line' ] - $tokens[ $end ][ 'line' ] - 1 ); |
|
157 | + if ( $diff !== 1 ) { |
|
158 | + if ( $diff < 0 ) { |
|
159 | 159 | $diff = 0; |
160 | 160 | } |
161 | 161 | |
162 | 162 | $error = 'There must be one blank line after the last USE statement; %s found;'; |
163 | - $data = array($diff); |
|
164 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data); |
|
165 | - if ($fix === true) { |
|
166 | - if ($diff === 0) { |
|
167 | - $phpcsFile->fixer->addNewline($end); |
|
163 | + $data = array( $diff ); |
|
164 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceAfterLastUse', $data ); |
|
165 | + if ( $fix === true ) { |
|
166 | + if ( $diff === 0 ) { |
|
167 | + $phpcsFile->fixer->addNewline( $end ); |
|
168 | 168 | } else { |
169 | 169 | $phpcsFile->fixer->beginChangeset(); |
170 | - for ($i = ($end + 1); $i < $next; $i++) { |
|
171 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
170 | + for ( $i = ( $end + 1 ); $i < $next; $i++ ) { |
|
171 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $next ][ 'line' ] ) { |
|
172 | 172 | break; |
173 | 173 | } |
174 | 174 | |
175 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
175 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
176 | 176 | } |
177 | 177 | |
178 | - $phpcsFile->fixer->addNewline($end); |
|
178 | + $phpcsFile->fixer->addNewline( $end ); |
|
179 | 179 | $phpcsFile->fixer->endChangeset(); |
180 | 180 | } |
181 | 181 | } |
@@ -193,18 +193,18 @@ discard block |
||
193 | 193 | * |
194 | 194 | * @return void |
195 | 195 | */ |
196 | - private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
196 | + private function _shouldIgnoreUse( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
197 | 197 | { |
198 | 198 | $tokens = $phpcsFile->getTokens(); |
199 | 199 | |
200 | 200 | // Ignore USE keywords inside closures. |
201 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
202 | - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
201 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
202 | + if ( $tokens[ $next ][ 'code' ] === T_OPEN_PARENTHESIS ) { |
|
203 | 203 | return true; |
204 | 204 | } |
205 | 205 | |
206 | 206 | // Ignore USE keywords for traits. |
207 | - if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) { |
|
207 | + if ( $phpcsFile->hasCondition( $stackPtr, array( T_CLASS, T_TRAIT ) ) === true ) { |
|
208 | 208 | return true; |
209 | 209 | } |
210 | 210 |
@@ -25,8 +25,7 @@ discard block |
||
25 | 25 | * @version Release: @package_version@ |
26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
27 | 27 | */ |
28 | -class PSR2_Sniffs_Namespaces_UseDeclarationSniff implements PHP_CodeSniffer_Sniff |
|
29 | -{ |
|
28 | +class PSR2_Sniffs_Namespaces_UseDeclarationSniff implements PHP_CodeSniffer_Sniff { |
|
30 | 29 | |
31 | 30 | |
32 | 31 | /** |
@@ -34,8 +33,7 @@ discard block |
||
34 | 33 | * |
35 | 34 | * @return array |
36 | 35 | */ |
37 | - public function register() |
|
38 | - { |
|
36 | + public function register() { |
|
39 | 37 | return array(T_USE); |
40 | 38 | |
41 | 39 | }//end register() |
@@ -50,8 +48,7 @@ discard block |
||
50 | 48 | * |
51 | 49 | * @return void |
52 | 50 | */ |
53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
54 | - { |
|
51 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
55 | 52 | if ($this->_shouldIgnoreUse($phpcsFile, $stackPtr) === true) { |
56 | 53 | return; |
57 | 54 | } |
@@ -193,8 +190,7 @@ discard block |
||
193 | 190 | * |
194 | 191 | * @return void |
195 | 192 | */ |
196 | - private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
197 | - { |
|
193 | + private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
198 | 194 | $tokens = $phpcsFile->getTokens(); |
199 | 195 | |
200 | 196 | // Ignore USE keywords inside closures. |
@@ -34,7 +34,7 @@ |
||
34 | 34 | /** |
35 | 35 | * Returns an array of tokens this test wants to listen for. |
36 | 36 | * |
37 | - * @return array |
|
37 | + * @return string[] |
|
38 | 38 | */ |
39 | 39 | public function register() |
40 | 40 | { |
@@ -31,89 +31,89 @@ |
||
31 | 31 | { |
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * Returns an array of tokens this test wants to listen for. |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function register() |
|
40 | - { |
|
41 | - return array( |
|
42 | - T_OPEN_SQUARE_BRACKET, |
|
43 | - T_CLOSE_SQUARE_BRACKET, |
|
44 | - ); |
|
34 | + /** |
|
35 | + * Returns an array of tokens this test wants to listen for. |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function register() |
|
40 | + { |
|
41 | + return array( |
|
42 | + T_OPEN_SQUARE_BRACKET, |
|
43 | + T_CLOSE_SQUARE_BRACKET, |
|
44 | + ); |
|
45 | 45 | |
46 | - }//end register() |
|
46 | + }//end register() |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Processes this sniff, when one of its tokens is encountered. |
|
51 | - * |
|
52 | - * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. |
|
53 | - * @param int $stackPtr The position of the current token in the |
|
54 | - * stack passed in $tokens. |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | - { |
|
60 | - $tokens = $phpcsFile->getTokens(); |
|
49 | + /** |
|
50 | + * Processes this sniff, when one of its tokens is encountered. |
|
51 | + * |
|
52 | + * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. |
|
53 | + * @param int $stackPtr The position of the current token in the |
|
54 | + * stack passed in $tokens. |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | + { |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | 61 | |
62 | - // PHP 5.4 introduced a shorthand array declaration syntax, so we need |
|
63 | - // to ignore the these type of array declarations because this sniff is |
|
64 | - // only dealing with array usage. |
|
65 | - if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
66 | - $openBracket = $stackPtr; |
|
67 | - } else { |
|
68 | - if (isset($tokens[$stackPtr]['bracket_opener']) === false) { |
|
69 | - return; |
|
70 | - } |
|
62 | + // PHP 5.4 introduced a shorthand array declaration syntax, so we need |
|
63 | + // to ignore the these type of array declarations because this sniff is |
|
64 | + // only dealing with array usage. |
|
65 | + if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
66 | + $openBracket = $stackPtr; |
|
67 | + } else { |
|
68 | + if (isset($tokens[$stackPtr]['bracket_opener']) === false) { |
|
69 | + return; |
|
70 | + } |
|
71 | 71 | |
72 | - $openBracket = $tokens[$stackPtr]['bracket_opener']; |
|
73 | - } |
|
72 | + $openBracket = $tokens[$stackPtr]['bracket_opener']; |
|
73 | + } |
|
74 | 74 | |
75 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket - 1), null, true); |
|
76 | - if ($tokens[$prev]['code'] === T_EQUAL) { |
|
77 | - return; |
|
78 | - } |
|
75 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket - 1), null, true); |
|
76 | + if ($tokens[$prev]['code'] === T_EQUAL) { |
|
77 | + return; |
|
78 | + } |
|
79 | 79 | |
80 | - // Square brackets can not have a space before them. |
|
81 | - $prevType = $tokens[($stackPtr - 1)]['code']; |
|
82 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$prevType]) === true) { |
|
83 | - $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true); |
|
84 | - $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content']; |
|
85 | - $found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content']; |
|
86 | - $error = 'Space found before square bracket; expected "%s" but found "%s"'; |
|
87 | - $data = array( |
|
88 | - $expected, |
|
89 | - $found, |
|
90 | - ); |
|
91 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
92 | - if ($fix === true) { |
|
93 | - $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
94 | - } |
|
95 | - } |
|
80 | + // Square brackets can not have a space before them. |
|
81 | + $prevType = $tokens[($stackPtr - 1)]['code']; |
|
82 | + if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$prevType]) === true) { |
|
83 | + $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true); |
|
84 | + $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content']; |
|
85 | + $found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content']; |
|
86 | + $error = 'Space found before square bracket; expected "%s" but found "%s"'; |
|
87 | + $data = array( |
|
88 | + $expected, |
|
89 | + $found, |
|
90 | + ); |
|
91 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
92 | + if ($fix === true) { |
|
93 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
94 | + } |
|
95 | + } |
|
96 | 96 | |
97 | - // Open square brackets can't ever have spaces after them. |
|
98 | - if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
99 | - $nextType = $tokens[($stackPtr + 1)]['code']; |
|
100 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$nextType]) === true) { |
|
101 | - $nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 2), null, true); |
|
102 | - $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content']; |
|
103 | - $found = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1)); |
|
104 | - $error = 'Space found after square bracket; expected "%s" but found "%s"'; |
|
105 | - $data = array( |
|
106 | - $expected, |
|
107 | - $found, |
|
108 | - ); |
|
109 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
110 | - if ($fix === true) { |
|
111 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
112 | - } |
|
113 | - } |
|
114 | - } |
|
97 | + // Open square brackets can't ever have spaces after them. |
|
98 | + if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
99 | + $nextType = $tokens[($stackPtr + 1)]['code']; |
|
100 | + if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$nextType]) === true) { |
|
101 | + $nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 2), null, true); |
|
102 | + $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content']; |
|
103 | + $found = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1)); |
|
104 | + $error = 'Space found after square bracket; expected "%s" but found "%s"'; |
|
105 | + $data = array( |
|
106 | + $expected, |
|
107 | + $found, |
|
108 | + ); |
|
109 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
110 | + if ($fix === true) { |
|
111 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
112 | + } |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | - }//end process() |
|
116 | + }//end process() |
|
117 | 117 | |
118 | 118 | |
119 | 119 | }//end class |
@@ -55,60 +55,60 @@ |
||
55 | 55 | * |
56 | 56 | * @return void |
57 | 57 | */ |
58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
58 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
59 | 59 | { |
60 | 60 | $tokens = $phpcsFile->getTokens(); |
61 | 61 | |
62 | 62 | // PHP 5.4 introduced a shorthand array declaration syntax, so we need |
63 | 63 | // to ignore the these type of array declarations because this sniff is |
64 | 64 | // only dealing with array usage. |
65 | - if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
65 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_OPEN_SQUARE_BRACKET ) { |
|
66 | 66 | $openBracket = $stackPtr; |
67 | 67 | } else { |
68 | - if (isset($tokens[$stackPtr]['bracket_opener']) === false) { |
|
68 | + if ( isset( $tokens[ $stackPtr ][ 'bracket_opener' ] ) === false ) { |
|
69 | 69 | return; |
70 | 70 | } |
71 | 71 | |
72 | - $openBracket = $tokens[$stackPtr]['bracket_opener']; |
|
72 | + $openBracket = $tokens[ $stackPtr ][ 'bracket_opener' ]; |
|
73 | 73 | } |
74 | 74 | |
75 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket - 1), null, true); |
|
76 | - if ($tokens[$prev]['code'] === T_EQUAL) { |
|
75 | + $prev = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $openBracket - 1 ), null, true ); |
|
76 | + if ( $tokens[ $prev ][ 'code' ] === T_EQUAL ) { |
|
77 | 77 | return; |
78 | 78 | } |
79 | 79 | |
80 | 80 | // Square brackets can not have a space before them. |
81 | - $prevType = $tokens[($stackPtr - 1)]['code']; |
|
82 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$prevType]) === true) { |
|
83 | - $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true); |
|
84 | - $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content']; |
|
85 | - $found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content']; |
|
81 | + $prevType = $tokens[ ( $stackPtr - 1 ) ][ 'code' ]; |
|
82 | + if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $prevType ] ) === true ) { |
|
83 | + $nonSpace = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr - 2 ), null, true ); |
|
84 | + $expected = $tokens[ $nonSpace ][ 'content' ] . $tokens[ $stackPtr ][ 'content' ]; |
|
85 | + $found = $phpcsFile->getTokensAsString( $nonSpace, ( $stackPtr - $nonSpace ) ) . $tokens[ $stackPtr ][ 'content' ]; |
|
86 | 86 | $error = 'Space found before square bracket; expected "%s" but found "%s"'; |
87 | 87 | $data = array( |
88 | 88 | $expected, |
89 | 89 | $found, |
90 | 90 | ); |
91 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
92 | - if ($fix === true) { |
|
93 | - $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
91 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceBeforeBracket', $data ); |
|
92 | + if ( $fix === true ) { |
|
93 | + $phpcsFile->fixer->replaceToken( ( $stackPtr - 1 ), '' ); |
|
94 | 94 | } |
95 | 95 | } |
96 | 96 | |
97 | 97 | // Open square brackets can't ever have spaces after them. |
98 | - if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
99 | - $nextType = $tokens[($stackPtr + 1)]['code']; |
|
100 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$nextType]) === true) { |
|
101 | - $nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 2), null, true); |
|
102 | - $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content']; |
|
103 | - $found = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1)); |
|
98 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_OPEN_SQUARE_BRACKET ) { |
|
99 | + $nextType = $tokens[ ( $stackPtr + 1 ) ][ 'code' ]; |
|
100 | + if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $nextType ] ) === true ) { |
|
101 | + $nonSpace = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 2 ), null, true ); |
|
102 | + $expected = $tokens[ $stackPtr ][ 'content' ] . $tokens[ $nonSpace ][ 'content' ]; |
|
103 | + $found = $phpcsFile->getTokensAsString( $stackPtr, ( $nonSpace - $stackPtr + 1 ) ); |
|
104 | 104 | $error = 'Space found after square bracket; expected "%s" but found "%s"'; |
105 | 105 | $data = array( |
106 | 106 | $expected, |
107 | 107 | $found, |
108 | 108 | ); |
109 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
110 | - if ($fix === true) { |
|
111 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
109 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceAfterBracket', $data ); |
|
110 | + if ( $fix === true ) { |
|
111 | + $phpcsFile->fixer->replaceToken( ( $stackPtr + 1 ), '' ); |
|
112 | 112 | } |
113 | 113 | } |
114 | 114 | } |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * @version Release: @package_version@ |
28 | 28 | * @link http://pear.php.net/package/PHP_CodeSniffer |
29 | 29 | */ |
30 | -class Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff implements PHP_CodeSniffer_Sniff |
|
31 | -{ |
|
30 | +class Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff implements PHP_CodeSniffer_Sniff { |
|
32 | 31 | |
33 | 32 | |
34 | 33 | /** |
@@ -36,8 +35,7 @@ discard block |
||
36 | 35 | * |
37 | 36 | * @return array |
38 | 37 | */ |
39 | - public function register() |
|
40 | - { |
|
38 | + public function register() { |
|
41 | 39 | return array( |
42 | 40 | T_OPEN_SQUARE_BRACKET, |
43 | 41 | T_CLOSE_SQUARE_BRACKET, |
@@ -55,8 +53,7 @@ discard block |
||
55 | 53 | * |
56 | 54 | * @return void |
57 | 55 | */ |
58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | - { |
|
56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
60 | 57 | $tokens = $phpcsFile->getTokens(); |
61 | 58 | |
62 | 59 | // PHP 5.4 introduced a shorthand array declaration syntax, so we need |
@@ -34,7 +34,7 @@ |
||
34 | 34 | /** |
35 | 35 | * Returns an array of tokens this test wants to listen for. |
36 | 36 | * |
37 | - * @return array |
|
37 | + * @return integer[] |
|
38 | 38 | */ |
39 | 39 | public function register() |
40 | 40 | { |
@@ -31,69 +31,69 @@ |
||
31 | 31 | { |
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * Returns an array of tokens this test wants to listen for. |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function register() |
|
40 | - { |
|
41 | - return array( |
|
42 | - T_CLASS, |
|
43 | - T_INTERFACE, |
|
44 | - ); |
|
34 | + /** |
|
35 | + * Returns an array of tokens this test wants to listen for. |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function register() |
|
40 | + { |
|
41 | + return array( |
|
42 | + T_CLASS, |
|
43 | + T_INTERFACE, |
|
44 | + ); |
|
45 | 45 | |
46 | - }//end register() |
|
46 | + }//end register() |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Processes this test, when one of its tokens is encountered. |
|
51 | - * |
|
52 | - * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. |
|
53 | - * @param int $stackPtr The position of the current token in the |
|
54 | - * stack passed in $tokens. |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | - { |
|
60 | - $tokens = $phpcsFile->getTokens(); |
|
49 | + /** |
|
50 | + * Processes this test, when one of its tokens is encountered. |
|
51 | + * |
|
52 | + * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. |
|
53 | + * @param int $stackPtr The position of the current token in the |
|
54 | + * stack passed in $tokens. |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | + { |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | 61 | |
62 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
63 | - $error = 'Possible parse error: %s missing opening or closing brace'; |
|
64 | - $data = array($tokens[$stackPtr]['content']); |
|
65 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
66 | - return; |
|
67 | - } |
|
62 | + if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
63 | + $error = 'Possible parse error: %s missing opening or closing brace'; |
|
64 | + $data = array($tokens[$stackPtr]['content']); |
|
65 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
66 | + return; |
|
67 | + } |
|
68 | 68 | |
69 | - // Determine the name of the class or interface. Note that we cannot |
|
70 | - // simply look for the first T_STRING because a class name |
|
71 | - // starting with the number will be multiple tokens. |
|
72 | - $opener = $tokens[$stackPtr]['scope_opener']; |
|
73 | - $nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true); |
|
74 | - $nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener); |
|
75 | - if ($nameEnd === false) { |
|
76 | - $name = $tokens[$nameStart]['content']; |
|
77 | - } else { |
|
78 | - $name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart))); |
|
79 | - } |
|
69 | + // Determine the name of the class or interface. Note that we cannot |
|
70 | + // simply look for the first T_STRING because a class name |
|
71 | + // starting with the number will be multiple tokens. |
|
72 | + $opener = $tokens[$stackPtr]['scope_opener']; |
|
73 | + $nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true); |
|
74 | + $nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener); |
|
75 | + if ($nameEnd === false) { |
|
76 | + $name = $tokens[$nameStart]['content']; |
|
77 | + } else { |
|
78 | + $name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart))); |
|
79 | + } |
|
80 | 80 | |
81 | - // Check for camel caps format. |
|
82 | - $valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false); |
|
83 | - if ($valid === false) { |
|
84 | - $type = ucfirst($tokens[$stackPtr]['content']); |
|
85 | - $error = '%s name "%s" is not in camel caps format'; |
|
86 | - $data = array( |
|
87 | - $type, |
|
88 | - $name, |
|
89 | - ); |
|
90 | - $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); |
|
91 | - $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no'); |
|
92 | - } else { |
|
93 | - $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes'); |
|
94 | - } |
|
81 | + // Check for camel caps format. |
|
82 | + $valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false); |
|
83 | + if ($valid === false) { |
|
84 | + $type = ucfirst($tokens[$stackPtr]['content']); |
|
85 | + $error = '%s name "%s" is not in camel caps format'; |
|
86 | + $data = array( |
|
87 | + $type, |
|
88 | + $name, |
|
89 | + ); |
|
90 | + $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); |
|
91 | + $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no'); |
|
92 | + } else { |
|
93 | + $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes'); |
|
94 | + } |
|
95 | 95 | |
96 | - }//end process() |
|
96 | + }//end process() |
|
97 | 97 | |
98 | 98 | |
99 | 99 | }//end class |
@@ -55,42 +55,42 @@ |
||
55 | 55 | * |
56 | 56 | * @return void |
57 | 57 | */ |
58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
58 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
59 | 59 | { |
60 | 60 | $tokens = $phpcsFile->getTokens(); |
61 | 61 | |
62 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
62 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ] ) === false ) { |
|
63 | 63 | $error = 'Possible parse error: %s missing opening or closing brace'; |
64 | - $data = array($tokens[$stackPtr]['content']); |
|
65 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
64 | + $data = array( $tokens[ $stackPtr ][ 'content' ] ); |
|
65 | + $phpcsFile->addWarning( $error, $stackPtr, 'MissingBrace', $data ); |
|
66 | 66 | return; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Determine the name of the class or interface. Note that we cannot |
70 | 70 | // simply look for the first T_STRING because a class name |
71 | 71 | // starting with the number will be multiple tokens. |
72 | - $opener = $tokens[$stackPtr]['scope_opener']; |
|
73 | - $nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true); |
|
74 | - $nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener); |
|
75 | - if ($nameEnd === false) { |
|
76 | - $name = $tokens[$nameStart]['content']; |
|
72 | + $opener = $tokens[ $stackPtr ][ 'scope_opener' ]; |
|
73 | + $nameStart = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), $opener, true ); |
|
74 | + $nameEnd = $phpcsFile->findNext( T_WHITESPACE, $nameStart, $opener ); |
|
75 | + if ( $nameEnd === false ) { |
|
76 | + $name = $tokens[ $nameStart ][ 'content' ]; |
|
77 | 77 | } else { |
78 | - $name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart))); |
|
78 | + $name = trim( $phpcsFile->getTokensAsString( $nameStart, ( $nameEnd - $nameStart ) ) ); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | // Check for camel caps format. |
82 | - $valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false); |
|
83 | - if ($valid === false) { |
|
84 | - $type = ucfirst($tokens[$stackPtr]['content']); |
|
82 | + $valid = PHP_CodeSniffer::isCamelCaps( $name, true, true, false ); |
|
83 | + if ( $valid === false ) { |
|
84 | + $type = ucfirst( $tokens[ $stackPtr ][ 'content' ] ); |
|
85 | 85 | $error = '%s name "%s" is not in camel caps format'; |
86 | 86 | $data = array( |
87 | 87 | $type, |
88 | 88 | $name, |
89 | 89 | ); |
90 | - $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); |
|
91 | - $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no'); |
|
90 | + $phpcsFile->addError( $error, $stackPtr, 'NotCamelCaps', $data ); |
|
91 | + $phpcsFile->recordMetric( $stackPtr, 'CamelCase class name', 'no' ); |
|
92 | 92 | } else { |
93 | - $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes'); |
|
93 | + $phpcsFile->recordMetric( $stackPtr, 'CamelCase class name', 'yes' ); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | }//end process() |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * @version Release: @package_version@ |
28 | 28 | * @link http://pear.php.net/package/PHP_CodeSniffer |
29 | 29 | */ |
30 | -class Squiz_Sniffs_Classes_ValidClassNameSniff implements PHP_CodeSniffer_Sniff |
|
31 | -{ |
|
30 | +class Squiz_Sniffs_Classes_ValidClassNameSniff implements PHP_CodeSniffer_Sniff { |
|
32 | 31 | |
33 | 32 | |
34 | 33 | /** |
@@ -36,8 +35,7 @@ discard block |
||
36 | 35 | * |
37 | 36 | * @return array |
38 | 37 | */ |
39 | - public function register() |
|
40 | - { |
|
38 | + public function register() { |
|
41 | 39 | return array( |
42 | 40 | T_CLASS, |
43 | 41 | T_INTERFACE, |
@@ -55,8 +53,7 @@ discard block |
||
55 | 53 | * |
56 | 54 | * @return void |
57 | 55 | */ |
58 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | - { |
|
56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
60 | 57 | $tokens = $phpcsFile->getTokens(); |
61 | 58 | |
62 | 59 | if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
@@ -39,7 +39,7 @@ |
||
39 | 39 | /** |
40 | 40 | * Returns an array of tokens this test wants to listen for. |
41 | 41 | * |
42 | - * @return array |
|
42 | + * @return integer[] |
|
43 | 43 | */ |
44 | 44 | public function register() |
45 | 45 | { |
@@ -36,62 +36,62 @@ |
||
36 | 36 | { |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * Returns an array of tokens this test wants to listen for. |
|
41 | - * |
|
42 | - * @return array |
|
43 | - */ |
|
44 | - public function register() |
|
45 | - { |
|
46 | - return array(T_CLASS); |
|
39 | + /** |
|
40 | + * Returns an array of tokens this test wants to listen for. |
|
41 | + * |
|
42 | + * @return array |
|
43 | + */ |
|
44 | + public function register() |
|
45 | + { |
|
46 | + return array(T_CLASS); |
|
47 | 47 | |
48 | - }//end register() |
|
48 | + }//end register() |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * Processes this test, when one of its tokens is encountered. |
|
53 | - * |
|
54 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
55 | - * @param int $stackPtr The position of the current token |
|
56 | - * in the stack passed in $tokens. |
|
57 | - * |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | - { |
|
62 | - $tokens = $phpcsFile->getTokens(); |
|
63 | - $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
|
64 | - $find[] = T_WHITESPACE; |
|
51 | + /** |
|
52 | + * Processes this test, when one of its tokens is encountered. |
|
53 | + * |
|
54 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
55 | + * @param int $stackPtr The position of the current token |
|
56 | + * in the stack passed in $tokens. |
|
57 | + * |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | + { |
|
62 | + $tokens = $phpcsFile->getTokens(); |
|
63 | + $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
|
64 | + $find[] = T_WHITESPACE; |
|
65 | 65 | |
66 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
67 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
68 | - && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
69 | - ) { |
|
70 | - $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing'); |
|
71 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no'); |
|
72 | - return; |
|
73 | - } |
|
66 | + $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
67 | + if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
68 | + && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
69 | + ) { |
|
70 | + $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing'); |
|
71 | + $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no'); |
|
72 | + return; |
|
73 | + } |
|
74 | 74 | |
75 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes'); |
|
75 | + $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes'); |
|
76 | 76 | |
77 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
78 | - $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle'); |
|
79 | - return; |
|
80 | - } |
|
77 | + if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
78 | + $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle'); |
|
79 | + return; |
|
80 | + } |
|
81 | 81 | |
82 | - if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
|
83 | - $error = 'There must be no blank lines after the class comment'; |
|
84 | - $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
|
85 | - } |
|
82 | + if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
|
83 | + $error = 'There must be no blank lines after the class comment'; |
|
84 | + $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
|
85 | + } |
|
86 | 86 | |
87 | - $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
88 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
89 | - $error = '%s tag is not allowed in class comment'; |
|
90 | - $data = array($tokens[$tag]['content']); |
|
91 | - $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data); |
|
92 | - } |
|
87 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
88 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
89 | + $error = '%s tag is not allowed in class comment'; |
|
90 | + $data = array($tokens[$tag]['content']); |
|
91 | + $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data); |
|
92 | + } |
|
93 | 93 | |
94 | - }//end process() |
|
94 | + }//end process() |
|
95 | 95 | |
96 | 96 | |
97 | 97 | }//end class |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public function register() |
45 | 45 | { |
46 | - return array(T_CLASS); |
|
46 | + return array( T_CLASS ); |
|
47 | 47 | |
48 | 48 | }//end register() |
49 | 49 | |
@@ -57,38 +57,38 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @return void |
59 | 59 | */ |
60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
60 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
61 | 61 | { |
62 | 62 | $tokens = $phpcsFile->getTokens(); |
63 | 63 | $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
64 | - $find[] = T_WHITESPACE; |
|
64 | + $find[ ] = T_WHITESPACE; |
|
65 | 65 | |
66 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
67 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
68 | - && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
66 | + $commentEnd = $phpcsFile->findPrevious( $find, ( $stackPtr - 1 ), null, true ); |
|
67 | + if ( $tokens[ $commentEnd ][ 'code' ] !== T_DOC_COMMENT_CLOSE_TAG |
|
68 | + && $tokens[ $commentEnd ][ 'code' ] !== T_COMMENT |
|
69 | 69 | ) { |
70 | - $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing'); |
|
71 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no'); |
|
70 | + $phpcsFile->addError( 'Missing class doc comment', $stackPtr, 'Missing' ); |
|
71 | + $phpcsFile->recordMetric( $stackPtr, 'Class has doc comment', 'no' ); |
|
72 | 72 | return; |
73 | 73 | } |
74 | 74 | |
75 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes'); |
|
75 | + $phpcsFile->recordMetric( $stackPtr, 'Class has doc comment', 'yes' ); |
|
76 | 76 | |
77 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
78 | - $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle'); |
|
77 | + if ( $tokens[ $commentEnd ][ 'code' ] === T_COMMENT ) { |
|
78 | + $phpcsFile->addError( 'You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle' ); |
|
79 | 79 | return; |
80 | 80 | } |
81 | 81 | |
82 | - if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
|
82 | + if ( $tokens[ $commentEnd ][ 'line' ] !== ( $tokens[ $stackPtr ][ 'line' ] - 1 ) ) { |
|
83 | 83 | $error = 'There must be no blank lines after the class comment'; |
84 | - $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
|
84 | + $phpcsFile->addError( $error, $commentEnd, 'SpacingAfter' ); |
|
85 | 85 | } |
86 | 86 | |
87 | - $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
88 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
87 | + $commentStart = $tokens[ $commentEnd ][ 'comment_opener' ]; |
|
88 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $tag ) { |
|
89 | 89 | $error = '%s tag is not allowed in class comment'; |
90 | - $data = array($tokens[$tag]['content']); |
|
91 | - $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data); |
|
90 | + $data = array( $tokens[ $tag ][ 'content' ] ); |
|
91 | + $phpcsFile->addWarning( $error, $tag, 'TagNotAllowed', $data ); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | }//end process() |
@@ -32,8 +32,7 @@ discard block |
||
32 | 32 | * @version Release: @package_version@ |
33 | 33 | * @link http://pear.php.net/package/PHP_CodeSniffer |
34 | 34 | */ |
35 | -class Squiz_Sniffs_Commenting_ClassCommentSniff implements PHP_CodeSniffer_Sniff |
|
36 | -{ |
|
35 | +class Squiz_Sniffs_Commenting_ClassCommentSniff implements PHP_CodeSniffer_Sniff { |
|
37 | 36 | |
38 | 37 | |
39 | 38 | /** |
@@ -41,8 +40,7 @@ discard block |
||
41 | 40 | * |
42 | 41 | * @return array |
43 | 42 | */ |
44 | - public function register() |
|
45 | - { |
|
43 | + public function register() { |
|
46 | 44 | return array(T_CLASS); |
47 | 45 | |
48 | 46 | }//end register() |
@@ -57,8 +55,7 @@ discard block |
||
57 | 55 | * |
58 | 56 | * @return void |
59 | 57 | */ |
60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | - { |
|
58 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
62 | 59 | $tokens = $phpcsFile->getTokens(); |
63 | 60 | $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
64 | 61 | $find[] = T_WHITESPACE; |
@@ -34,7 +34,7 @@ |
||
34 | 34 | /** |
35 | 35 | * Returns an array of tokens this test wants to listen for. |
36 | 36 | * |
37 | - * @return array |
|
37 | + * @return integer[] |
|
38 | 38 | */ |
39 | 39 | public function register() |
40 | 40 | { |
@@ -31,119 +31,119 @@ |
||
31 | 31 | { |
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * Returns an array of tokens this test wants to listen for. |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function register() |
|
40 | - { |
|
41 | - return array( |
|
42 | - T_FUNCTION, |
|
43 | - T_CLASS, |
|
44 | - T_INTERFACE, |
|
45 | - ); |
|
46 | - |
|
47 | - }//end register() |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Processes this test, when one of its tokens is encountered. |
|
52 | - * |
|
53 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
54 | - * @param int $stackPtr The position of the current token in the |
|
55 | - * stack passed in $tokens.. |
|
56 | - * |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
60 | - { |
|
61 | - $tokens = $phpcsFile->getTokens(); |
|
62 | - |
|
63 | - if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
64 | - $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
65 | - |
|
66 | - // Abstract methods do not require a closing comment. |
|
67 | - if ($methodProps['is_abstract'] === true) { |
|
68 | - return; |
|
69 | - } |
|
70 | - |
|
71 | - // Closures do not require a closing comment. |
|
72 | - if ($methodProps['is_closure'] === true) { |
|
73 | - return; |
|
74 | - } |
|
75 | - |
|
76 | - // If this function is in an interface then we don't require |
|
77 | - // a closing comment. |
|
78 | - if ($phpcsFile->hasCondition($stackPtr, T_INTERFACE) === true) { |
|
79 | - return; |
|
80 | - } |
|
81 | - |
|
82 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
83 | - $error = 'Possible parse error: non-abstract method defined as abstract'; |
|
84 | - $phpcsFile->addWarning($error, $stackPtr, 'Abstract'); |
|
85 | - return; |
|
86 | - } |
|
87 | - |
|
88 | - $decName = $phpcsFile->getDeclarationName($stackPtr); |
|
89 | - $comment = '//end '.$decName.'()'; |
|
90 | - } else if ($tokens[$stackPtr]['code'] === T_CLASS) { |
|
91 | - $comment = '//end class'; |
|
92 | - } else { |
|
93 | - $comment = '//end interface'; |
|
94 | - }//end if |
|
95 | - |
|
96 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
97 | - $error = 'Possible parse error: %s missing opening or closing brace'; |
|
98 | - $data = array($tokens[$stackPtr]['content']); |
|
99 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
100 | - return; |
|
101 | - } |
|
102 | - |
|
103 | - $closingBracket = $tokens[$stackPtr]['scope_closer']; |
|
104 | - |
|
105 | - if ($closingBracket === null) { |
|
106 | - // Possible inline structure. Other tests will handle it. |
|
107 | - return; |
|
108 | - } |
|
109 | - |
|
110 | - $error = 'Expected '.$comment; |
|
111 | - if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) { |
|
112 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true); |
|
113 | - if (rtrim($tokens[$next]['content']) === $comment) { |
|
114 | - // The comment isn't really missing; it is just in the wrong place. |
|
115 | - $fix = $phpcsFile->addFixableError($error.' directly after closing brace', $closingBracket, 'Misplaced'); |
|
116 | - if ($fix === true) { |
|
117 | - $phpcsFile->fixer->beginChangeset(); |
|
118 | - for ($i = ($closingBracket + 1); $i < $next; $i++) { |
|
119 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
120 | - } |
|
121 | - |
|
122 | - // Just in case, because indentation fixes can add indents onto |
|
123 | - // these comments and cause us to be unable to fix them. |
|
124 | - $phpcsFile->fixer->replaceToken($next, $comment.$phpcsFile->eolChar); |
|
125 | - $phpcsFile->fixer->endChangeset(); |
|
126 | - } |
|
127 | - } else { |
|
128 | - $fix = $phpcsFile->addFixableError($error, $closingBracket, 'Missing'); |
|
129 | - if ($fix === true) { |
|
130 | - $phpcsFile->fixer->replaceToken($closingBracket, '}'.$comment.$phpcsFile->eolChar); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - return; |
|
135 | - }//end if |
|
136 | - |
|
137 | - if (rtrim($tokens[($closingBracket + 1)]['content']) !== $comment) { |
|
138 | - $fix = $phpcsFile->addFixableError($error, $closingBracket, 'Incorrect'); |
|
139 | - if ($fix === true) { |
|
140 | - $phpcsFile->fixer->replaceToken(($closingBracket + 1), $comment.$phpcsFile->eolChar); |
|
141 | - } |
|
142 | - |
|
143 | - return; |
|
144 | - } |
|
145 | - |
|
146 | - }//end process() |
|
34 | + /** |
|
35 | + * Returns an array of tokens this test wants to listen for. |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function register() |
|
40 | + { |
|
41 | + return array( |
|
42 | + T_FUNCTION, |
|
43 | + T_CLASS, |
|
44 | + T_INTERFACE, |
|
45 | + ); |
|
46 | + |
|
47 | + }//end register() |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Processes this test, when one of its tokens is encountered. |
|
52 | + * |
|
53 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
54 | + * @param int $stackPtr The position of the current token in the |
|
55 | + * stack passed in $tokens.. |
|
56 | + * |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
60 | + { |
|
61 | + $tokens = $phpcsFile->getTokens(); |
|
62 | + |
|
63 | + if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
64 | + $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
65 | + |
|
66 | + // Abstract methods do not require a closing comment. |
|
67 | + if ($methodProps['is_abstract'] === true) { |
|
68 | + return; |
|
69 | + } |
|
70 | + |
|
71 | + // Closures do not require a closing comment. |
|
72 | + if ($methodProps['is_closure'] === true) { |
|
73 | + return; |
|
74 | + } |
|
75 | + |
|
76 | + // If this function is in an interface then we don't require |
|
77 | + // a closing comment. |
|
78 | + if ($phpcsFile->hasCondition($stackPtr, T_INTERFACE) === true) { |
|
79 | + return; |
|
80 | + } |
|
81 | + |
|
82 | + if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
83 | + $error = 'Possible parse error: non-abstract method defined as abstract'; |
|
84 | + $phpcsFile->addWarning($error, $stackPtr, 'Abstract'); |
|
85 | + return; |
|
86 | + } |
|
87 | + |
|
88 | + $decName = $phpcsFile->getDeclarationName($stackPtr); |
|
89 | + $comment = '//end '.$decName.'()'; |
|
90 | + } else if ($tokens[$stackPtr]['code'] === T_CLASS) { |
|
91 | + $comment = '//end class'; |
|
92 | + } else { |
|
93 | + $comment = '//end interface'; |
|
94 | + }//end if |
|
95 | + |
|
96 | + if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
97 | + $error = 'Possible parse error: %s missing opening or closing brace'; |
|
98 | + $data = array($tokens[$stackPtr]['content']); |
|
99 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
100 | + return; |
|
101 | + } |
|
102 | + |
|
103 | + $closingBracket = $tokens[$stackPtr]['scope_closer']; |
|
104 | + |
|
105 | + if ($closingBracket === null) { |
|
106 | + // Possible inline structure. Other tests will handle it. |
|
107 | + return; |
|
108 | + } |
|
109 | + |
|
110 | + $error = 'Expected '.$comment; |
|
111 | + if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) { |
|
112 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true); |
|
113 | + if (rtrim($tokens[$next]['content']) === $comment) { |
|
114 | + // The comment isn't really missing; it is just in the wrong place. |
|
115 | + $fix = $phpcsFile->addFixableError($error.' directly after closing brace', $closingBracket, 'Misplaced'); |
|
116 | + if ($fix === true) { |
|
117 | + $phpcsFile->fixer->beginChangeset(); |
|
118 | + for ($i = ($closingBracket + 1); $i < $next; $i++) { |
|
119 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
120 | + } |
|
121 | + |
|
122 | + // Just in case, because indentation fixes can add indents onto |
|
123 | + // these comments and cause us to be unable to fix them. |
|
124 | + $phpcsFile->fixer->replaceToken($next, $comment.$phpcsFile->eolChar); |
|
125 | + $phpcsFile->fixer->endChangeset(); |
|
126 | + } |
|
127 | + } else { |
|
128 | + $fix = $phpcsFile->addFixableError($error, $closingBracket, 'Missing'); |
|
129 | + if ($fix === true) { |
|
130 | + $phpcsFile->fixer->replaceToken($closingBracket, '}'.$comment.$phpcsFile->eolChar); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + return; |
|
135 | + }//end if |
|
136 | + |
|
137 | + if (rtrim($tokens[($closingBracket + 1)]['content']) !== $comment) { |
|
138 | + $fix = $phpcsFile->addFixableError($error, $closingBracket, 'Incorrect'); |
|
139 | + if ($fix === true) { |
|
140 | + $phpcsFile->fixer->replaceToken(($closingBracket + 1), $comment.$phpcsFile->eolChar); |
|
141 | + } |
|
142 | + |
|
143 | + return; |
|
144 | + } |
|
145 | + |
|
146 | + }//end process() |
|
147 | 147 | |
148 | 148 | |
149 | 149 | }//end class |
@@ -56,88 +56,88 @@ |
||
56 | 56 | * |
57 | 57 | * @return void |
58 | 58 | */ |
59 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
59 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
60 | 60 | { |
61 | 61 | $tokens = $phpcsFile->getTokens(); |
62 | 62 | |
63 | - if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
64 | - $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
63 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_FUNCTION ) { |
|
64 | + $methodProps = $phpcsFile->getMethodProperties( $stackPtr ); |
|
65 | 65 | |
66 | 66 | // Abstract methods do not require a closing comment. |
67 | - if ($methodProps['is_abstract'] === true) { |
|
67 | + if ( $methodProps[ 'is_abstract' ] === true ) { |
|
68 | 68 | return; |
69 | 69 | } |
70 | 70 | |
71 | 71 | // Closures do not require a closing comment. |
72 | - if ($methodProps['is_closure'] === true) { |
|
72 | + if ( $methodProps[ 'is_closure' ] === true ) { |
|
73 | 73 | return; |
74 | 74 | } |
75 | 75 | |
76 | 76 | // If this function is in an interface then we don't require |
77 | 77 | // a closing comment. |
78 | - if ($phpcsFile->hasCondition($stackPtr, T_INTERFACE) === true) { |
|
78 | + if ( $phpcsFile->hasCondition( $stackPtr, T_INTERFACE ) === true ) { |
|
79 | 79 | return; |
80 | 80 | } |
81 | 81 | |
82 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
82 | + if ( isset( $tokens[ $stackPtr ][ 'scope_closer' ] ) === false ) { |
|
83 | 83 | $error = 'Possible parse error: non-abstract method defined as abstract'; |
84 | - $phpcsFile->addWarning($error, $stackPtr, 'Abstract'); |
|
84 | + $phpcsFile->addWarning( $error, $stackPtr, 'Abstract' ); |
|
85 | 85 | return; |
86 | 86 | } |
87 | 87 | |
88 | - $decName = $phpcsFile->getDeclarationName($stackPtr); |
|
89 | - $comment = '//end '.$decName.'()'; |
|
90 | - } else if ($tokens[$stackPtr]['code'] === T_CLASS) { |
|
88 | + $decName = $phpcsFile->getDeclarationName( $stackPtr ); |
|
89 | + $comment = '//end ' . $decName . '()'; |
|
90 | + } else if ( $tokens[ $stackPtr ][ 'code' ] === T_CLASS ) { |
|
91 | 91 | $comment = '//end class'; |
92 | 92 | } else { |
93 | 93 | $comment = '//end interface'; |
94 | 94 | }//end if |
95 | 95 | |
96 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
96 | + if ( isset( $tokens[ $stackPtr ][ 'scope_closer' ] ) === false ) { |
|
97 | 97 | $error = 'Possible parse error: %s missing opening or closing brace'; |
98 | - $data = array($tokens[$stackPtr]['content']); |
|
99 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
98 | + $data = array( $tokens[ $stackPtr ][ 'content' ] ); |
|
99 | + $phpcsFile->addWarning( $error, $stackPtr, 'MissingBrace', $data ); |
|
100 | 100 | return; |
101 | 101 | } |
102 | 102 | |
103 | - $closingBracket = $tokens[$stackPtr]['scope_closer']; |
|
103 | + $closingBracket = $tokens[ $stackPtr ][ 'scope_closer' ]; |
|
104 | 104 | |
105 | - if ($closingBracket === null) { |
|
105 | + if ( $closingBracket === null ) { |
|
106 | 106 | // Possible inline structure. Other tests will handle it. |
107 | 107 | return; |
108 | 108 | } |
109 | 109 | |
110 | - $error = 'Expected '.$comment; |
|
111 | - if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) { |
|
112 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true); |
|
113 | - if (rtrim($tokens[$next]['content']) === $comment) { |
|
110 | + $error = 'Expected ' . $comment; |
|
111 | + if ( isset( $tokens[ ( $closingBracket + 1 ) ] ) === false || $tokens[ ( $closingBracket + 1 ) ][ 'code' ] !== T_COMMENT ) { |
|
112 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $closingBracket + 1 ), null, true ); |
|
113 | + if ( rtrim( $tokens[ $next ][ 'content' ] ) === $comment ) { |
|
114 | 114 | // The comment isn't really missing; it is just in the wrong place. |
115 | - $fix = $phpcsFile->addFixableError($error.' directly after closing brace', $closingBracket, 'Misplaced'); |
|
116 | - if ($fix === true) { |
|
115 | + $fix = $phpcsFile->addFixableError( $error . ' directly after closing brace', $closingBracket, 'Misplaced' ); |
|
116 | + if ( $fix === true ) { |
|
117 | 117 | $phpcsFile->fixer->beginChangeset(); |
118 | - for ($i = ($closingBracket + 1); $i < $next; $i++) { |
|
119 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
118 | + for ( $i = ( $closingBracket + 1 ); $i < $next; $i++ ) { |
|
119 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | // Just in case, because indentation fixes can add indents onto |
123 | 123 | // these comments and cause us to be unable to fix them. |
124 | - $phpcsFile->fixer->replaceToken($next, $comment.$phpcsFile->eolChar); |
|
124 | + $phpcsFile->fixer->replaceToken( $next, $comment . $phpcsFile->eolChar ); |
|
125 | 125 | $phpcsFile->fixer->endChangeset(); |
126 | 126 | } |
127 | 127 | } else { |
128 | - $fix = $phpcsFile->addFixableError($error, $closingBracket, 'Missing'); |
|
129 | - if ($fix === true) { |
|
130 | - $phpcsFile->fixer->replaceToken($closingBracket, '}'.$comment.$phpcsFile->eolChar); |
|
128 | + $fix = $phpcsFile->addFixableError( $error, $closingBracket, 'Missing' ); |
|
129 | + if ( $fix === true ) { |
|
130 | + $phpcsFile->fixer->replaceToken( $closingBracket, '}' . $comment . $phpcsFile->eolChar ); |
|
131 | 131 | } |
132 | 132 | } |
133 | 133 | |
134 | 134 | return; |
135 | 135 | }//end if |
136 | 136 | |
137 | - if (rtrim($tokens[($closingBracket + 1)]['content']) !== $comment) { |
|
138 | - $fix = $phpcsFile->addFixableError($error, $closingBracket, 'Incorrect'); |
|
139 | - if ($fix === true) { |
|
140 | - $phpcsFile->fixer->replaceToken(($closingBracket + 1), $comment.$phpcsFile->eolChar); |
|
137 | + if ( rtrim( $tokens[ ( $closingBracket + 1 ) ][ 'content' ] ) !== $comment ) { |
|
138 | + $fix = $phpcsFile->addFixableError( $error, $closingBracket, 'Incorrect' ); |
|
139 | + if ( $fix === true ) { |
|
140 | + $phpcsFile->fixer->replaceToken( ( $closingBracket + 1 ), $comment . $phpcsFile->eolChar ); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | return; |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * @version Release: @package_version@ |
28 | 28 | * @link http://pear.php.net/package/PHP_CodeSniffer |
29 | 29 | */ |
30 | -class Squiz_Sniffs_Commenting_ClosingDeclarationCommentSniff implements PHP_CodeSniffer_Sniff |
|
31 | -{ |
|
30 | +class Squiz_Sniffs_Commenting_ClosingDeclarationCommentSniff implements PHP_CodeSniffer_Sniff { |
|
32 | 31 | |
33 | 32 | |
34 | 33 | /** |
@@ -36,8 +35,7 @@ discard block |
||
36 | 35 | * |
37 | 36 | * @return array |
38 | 37 | */ |
39 | - public function register() |
|
40 | - { |
|
38 | + public function register() { |
|
41 | 39 | return array( |
42 | 40 | T_FUNCTION, |
43 | 41 | T_CLASS, |
@@ -56,8 +54,7 @@ discard block |
||
56 | 54 | * |
57 | 55 | * @return void |
58 | 56 | */ |
59 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
60 | - { |
|
57 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
61 | 58 | $tokens = $phpcsFile->getTokens(); |
62 | 59 | |
63 | 60 | if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
@@ -43,7 +43,7 @@ |
||
43 | 43 | /** |
44 | 44 | * Returns an array of tokens this test wants to listen for. |
45 | 45 | * |
46 | - * @return array |
|
46 | + * @return integer[] |
|
47 | 47 | */ |
48 | 48 | public function register() |
49 | 49 | { |
@@ -29,194 +29,194 @@ |
||
29 | 29 | class Squiz_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * A list of tokenizers this sniff supports. |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - public $supportedTokenizers = array( |
|
38 | - 'PHP', |
|
39 | - 'JS', |
|
40 | - ); |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns an array of tokens this test wants to listen for. |
|
45 | - * |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function register() |
|
49 | - { |
|
50 | - return array(T_OPEN_TAG); |
|
51 | - |
|
52 | - }//end register() |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Processes this test, when one of its tokens is encountered. |
|
57 | - * |
|
58 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
59 | - * @param int $stackPtr The position of the current token |
|
60 | - * in the stack passed in $tokens. |
|
61 | - * |
|
62 | - * @return int |
|
63 | - */ |
|
64 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
65 | - { |
|
66 | - $this->currentFile = $phpcsFile; |
|
67 | - |
|
68 | - $tokens = $phpcsFile->getTokens(); |
|
69 | - $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
70 | - |
|
71 | - if ($tokens[$commentStart]['code'] === T_COMMENT) { |
|
72 | - $phpcsFile->addError('You must use "/**" style comments for a file comment', $commentStart, 'WrongStyle'); |
|
73 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); |
|
74 | - return ($phpcsFile->numTokens + 1); |
|
75 | - } else if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) { |
|
76 | - $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing'); |
|
77 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no'); |
|
78 | - return ($phpcsFile->numTokens + 1); |
|
79 | - } |
|
80 | - |
|
81 | - $commentEnd = $tokens[$commentStart]['comment_closer']; |
|
82 | - |
|
83 | - $nextToken = $phpcsFile->findNext( |
|
84 | - T_WHITESPACE, |
|
85 | - ($commentEnd + 1), |
|
86 | - null, |
|
87 | - true |
|
88 | - ); |
|
89 | - |
|
90 | - $ignore = array( |
|
91 | - T_CLASS, |
|
92 | - T_INTERFACE, |
|
93 | - T_TRAIT, |
|
94 | - T_FUNCTION, |
|
95 | - T_CLOSURE, |
|
96 | - T_PUBLIC, |
|
97 | - T_PRIVATE, |
|
98 | - T_PROTECTED, |
|
99 | - T_FINAL, |
|
100 | - T_STATIC, |
|
101 | - T_ABSTRACT, |
|
102 | - T_CONST, |
|
103 | - T_PROPERTY, |
|
104 | - T_INCLUDE, |
|
105 | - T_INCLUDE_ONCE, |
|
106 | - T_REQUIRE, |
|
107 | - T_REQUIRE_ONCE, |
|
108 | - ); |
|
109 | - |
|
110 | - if (in_array($tokens[$nextToken]['code'], $ignore) === true) { |
|
111 | - $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing'); |
|
112 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no'); |
|
113 | - return ($phpcsFile->numTokens + 1); |
|
114 | - } |
|
115 | - |
|
116 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); |
|
117 | - |
|
118 | - // No blank line between the open tag and the file comment. |
|
119 | - if ($tokens[$commentStart]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
|
120 | - $error = 'There must be no blank lines before the file comment'; |
|
121 | - $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen'); |
|
122 | - } |
|
123 | - |
|
124 | - // Exactly one blank line after the file comment. |
|
125 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); |
|
126 | - if ($tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) { |
|
127 | - $error = 'There must be exactly one blank line after the file comment'; |
|
128 | - $phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment'); |
|
129 | - } |
|
130 | - |
|
131 | - // Required tags in correct order. |
|
132 | - $required = array( |
|
133 | - '@package' => true, |
|
134 | - '@subpackage' => true, |
|
135 | - '@author' => true, |
|
136 | - '@copyright' => true, |
|
137 | - ); |
|
138 | - |
|
139 | - $foundTags = array(); |
|
140 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
141 | - $name = $tokens[$tag]['content']; |
|
142 | - $isRequired = isset($required[$name]); |
|
143 | - |
|
144 | - if ($isRequired === true && in_array($name, $foundTags) === true) { |
|
145 | - $error = 'Only one %s tag is allowed in a file comment'; |
|
146 | - $data = array($name); |
|
147 | - $phpcsFile->addError($error, $tag, 'Duplicate'.ucfirst(substr($name, 1)).'Tag', $data); |
|
148 | - } |
|
149 | - |
|
150 | - $foundTags[] = $name; |
|
151 | - |
|
152 | - if ($isRequired === false) { |
|
153 | - continue; |
|
154 | - } |
|
155 | - |
|
156 | - $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
157 | - if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
|
158 | - $error = 'Content missing for %s tag in file comment'; |
|
159 | - $data = array($name); |
|
160 | - $phpcsFile->addError($error, $tag, 'Empty'.ucfirst(substr($name, 1)).'Tag', $data); |
|
161 | - continue; |
|
162 | - } |
|
163 | - |
|
164 | - if ($name === '@author') { |
|
165 | - if ($tokens[$string]['content'] !== 'Squiz Pty Ltd <[email protected]>') { |
|
166 | - $error = 'Expected "Squiz Pty Ltd <[email protected]>" for author tag'; |
|
167 | - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectAuthor'); |
|
168 | - if ($fix === true) { |
|
169 | - $expected = 'Squiz Pty Ltd <[email protected]>'; |
|
170 | - $phpcsFile->fixer->replaceToken($string, $expected); |
|
171 | - } |
|
172 | - } |
|
173 | - } else if ($name === '@copyright') { |
|
174 | - if (preg_match('/^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$/', $tokens[$string]['content']) === 0) { |
|
175 | - $error = 'Expected "xxxx-xxxx Squiz Pty Ltd (ABN 77 084 670 600)" for copyright declaration'; |
|
176 | - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyright'); |
|
177 | - if ($fix === true) { |
|
178 | - $matches = array(); |
|
179 | - preg_match('/^(([0-9]{4})(-[0-9]{4})?)?.*$/', $tokens[$string]['content'], $matches); |
|
180 | - if (isset($matches[1]) === false) { |
|
181 | - $matches[1] = date('Y'); |
|
182 | - } |
|
183 | - |
|
184 | - $expected = $matches[1].' Squiz Pty Ltd (ABN 77 084 670 600)'; |
|
185 | - $phpcsFile->fixer->replaceToken($string, $expected); |
|
186 | - } |
|
187 | - } |
|
188 | - }//end if |
|
189 | - }//end foreach |
|
190 | - |
|
191 | - // Check if the tags are in the correct position. |
|
192 | - $pos = 0; |
|
193 | - foreach ($required as $tag => $true) { |
|
194 | - if (in_array($tag, $foundTags) === false) { |
|
195 | - $error = 'Missing %s tag in file comment'; |
|
196 | - $data = array($tag); |
|
197 | - $phpcsFile->addError($error, $commentEnd, 'Missing'.ucfirst(substr($tag, 1)).'Tag', $data); |
|
198 | - } |
|
199 | - |
|
200 | - if (isset($foundTags[$pos]) === false) { |
|
201 | - break; |
|
202 | - } |
|
203 | - |
|
204 | - if ($foundTags[$pos] !== $tag) { |
|
205 | - $error = 'The tag in position %s should be the %s tag'; |
|
206 | - $data = array( |
|
207 | - ($pos + 1), |
|
208 | - $tag, |
|
209 | - ); |
|
210 | - $phpcsFile->addError($error, $tokens[$commentStart]['comment_tags'][$pos], ucfirst(substr($tag, 1)).'TagOrder', $data); |
|
211 | - } |
|
212 | - |
|
213 | - $pos++; |
|
214 | - }//end foreach |
|
215 | - |
|
216 | - // Ignore the rest of the file. |
|
217 | - return ($phpcsFile->numTokens + 1); |
|
218 | - |
|
219 | - }//end process() |
|
32 | + /** |
|
33 | + * A list of tokenizers this sniff supports. |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + public $supportedTokenizers = array( |
|
38 | + 'PHP', |
|
39 | + 'JS', |
|
40 | + ); |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns an array of tokens this test wants to listen for. |
|
45 | + * |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function register() |
|
49 | + { |
|
50 | + return array(T_OPEN_TAG); |
|
51 | + |
|
52 | + }//end register() |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Processes this test, when one of its tokens is encountered. |
|
57 | + * |
|
58 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
59 | + * @param int $stackPtr The position of the current token |
|
60 | + * in the stack passed in $tokens. |
|
61 | + * |
|
62 | + * @return int |
|
63 | + */ |
|
64 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
65 | + { |
|
66 | + $this->currentFile = $phpcsFile; |
|
67 | + |
|
68 | + $tokens = $phpcsFile->getTokens(); |
|
69 | + $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
70 | + |
|
71 | + if ($tokens[$commentStart]['code'] === T_COMMENT) { |
|
72 | + $phpcsFile->addError('You must use "/**" style comments for a file comment', $commentStart, 'WrongStyle'); |
|
73 | + $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); |
|
74 | + return ($phpcsFile->numTokens + 1); |
|
75 | + } else if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) { |
|
76 | + $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing'); |
|
77 | + $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no'); |
|
78 | + return ($phpcsFile->numTokens + 1); |
|
79 | + } |
|
80 | + |
|
81 | + $commentEnd = $tokens[$commentStart]['comment_closer']; |
|
82 | + |
|
83 | + $nextToken = $phpcsFile->findNext( |
|
84 | + T_WHITESPACE, |
|
85 | + ($commentEnd + 1), |
|
86 | + null, |
|
87 | + true |
|
88 | + ); |
|
89 | + |
|
90 | + $ignore = array( |
|
91 | + T_CLASS, |
|
92 | + T_INTERFACE, |
|
93 | + T_TRAIT, |
|
94 | + T_FUNCTION, |
|
95 | + T_CLOSURE, |
|
96 | + T_PUBLIC, |
|
97 | + T_PRIVATE, |
|
98 | + T_PROTECTED, |
|
99 | + T_FINAL, |
|
100 | + T_STATIC, |
|
101 | + T_ABSTRACT, |
|
102 | + T_CONST, |
|
103 | + T_PROPERTY, |
|
104 | + T_INCLUDE, |
|
105 | + T_INCLUDE_ONCE, |
|
106 | + T_REQUIRE, |
|
107 | + T_REQUIRE_ONCE, |
|
108 | + ); |
|
109 | + |
|
110 | + if (in_array($tokens[$nextToken]['code'], $ignore) === true) { |
|
111 | + $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing'); |
|
112 | + $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no'); |
|
113 | + return ($phpcsFile->numTokens + 1); |
|
114 | + } |
|
115 | + |
|
116 | + $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); |
|
117 | + |
|
118 | + // No blank line between the open tag and the file comment. |
|
119 | + if ($tokens[$commentStart]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
|
120 | + $error = 'There must be no blank lines before the file comment'; |
|
121 | + $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen'); |
|
122 | + } |
|
123 | + |
|
124 | + // Exactly one blank line after the file comment. |
|
125 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); |
|
126 | + if ($tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) { |
|
127 | + $error = 'There must be exactly one blank line after the file comment'; |
|
128 | + $phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment'); |
|
129 | + } |
|
130 | + |
|
131 | + // Required tags in correct order. |
|
132 | + $required = array( |
|
133 | + '@package' => true, |
|
134 | + '@subpackage' => true, |
|
135 | + '@author' => true, |
|
136 | + '@copyright' => true, |
|
137 | + ); |
|
138 | + |
|
139 | + $foundTags = array(); |
|
140 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
141 | + $name = $tokens[$tag]['content']; |
|
142 | + $isRequired = isset($required[$name]); |
|
143 | + |
|
144 | + if ($isRequired === true && in_array($name, $foundTags) === true) { |
|
145 | + $error = 'Only one %s tag is allowed in a file comment'; |
|
146 | + $data = array($name); |
|
147 | + $phpcsFile->addError($error, $tag, 'Duplicate'.ucfirst(substr($name, 1)).'Tag', $data); |
|
148 | + } |
|
149 | + |
|
150 | + $foundTags[] = $name; |
|
151 | + |
|
152 | + if ($isRequired === false) { |
|
153 | + continue; |
|
154 | + } |
|
155 | + |
|
156 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
157 | + if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
|
158 | + $error = 'Content missing for %s tag in file comment'; |
|
159 | + $data = array($name); |
|
160 | + $phpcsFile->addError($error, $tag, 'Empty'.ucfirst(substr($name, 1)).'Tag', $data); |
|
161 | + continue; |
|
162 | + } |
|
163 | + |
|
164 | + if ($name === '@author') { |
|
165 | + if ($tokens[$string]['content'] !== 'Squiz Pty Ltd <[email protected]>') { |
|
166 | + $error = 'Expected "Squiz Pty Ltd <[email protected]>" for author tag'; |
|
167 | + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectAuthor'); |
|
168 | + if ($fix === true) { |
|
169 | + $expected = 'Squiz Pty Ltd <[email protected]>'; |
|
170 | + $phpcsFile->fixer->replaceToken($string, $expected); |
|
171 | + } |
|
172 | + } |
|
173 | + } else if ($name === '@copyright') { |
|
174 | + if (preg_match('/^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$/', $tokens[$string]['content']) === 0) { |
|
175 | + $error = 'Expected "xxxx-xxxx Squiz Pty Ltd (ABN 77 084 670 600)" for copyright declaration'; |
|
176 | + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyright'); |
|
177 | + if ($fix === true) { |
|
178 | + $matches = array(); |
|
179 | + preg_match('/^(([0-9]{4})(-[0-9]{4})?)?.*$/', $tokens[$string]['content'], $matches); |
|
180 | + if (isset($matches[1]) === false) { |
|
181 | + $matches[1] = date('Y'); |
|
182 | + } |
|
183 | + |
|
184 | + $expected = $matches[1].' Squiz Pty Ltd (ABN 77 084 670 600)'; |
|
185 | + $phpcsFile->fixer->replaceToken($string, $expected); |
|
186 | + } |
|
187 | + } |
|
188 | + }//end if |
|
189 | + }//end foreach |
|
190 | + |
|
191 | + // Check if the tags are in the correct position. |
|
192 | + $pos = 0; |
|
193 | + foreach ($required as $tag => $true) { |
|
194 | + if (in_array($tag, $foundTags) === false) { |
|
195 | + $error = 'Missing %s tag in file comment'; |
|
196 | + $data = array($tag); |
|
197 | + $phpcsFile->addError($error, $commentEnd, 'Missing'.ucfirst(substr($tag, 1)).'Tag', $data); |
|
198 | + } |
|
199 | + |
|
200 | + if (isset($foundTags[$pos]) === false) { |
|
201 | + break; |
|
202 | + } |
|
203 | + |
|
204 | + if ($foundTags[$pos] !== $tag) { |
|
205 | + $error = 'The tag in position %s should be the %s tag'; |
|
206 | + $data = array( |
|
207 | + ($pos + 1), |
|
208 | + $tag, |
|
209 | + ); |
|
210 | + $phpcsFile->addError($error, $tokens[$commentStart]['comment_tags'][$pos], ucfirst(substr($tag, 1)).'TagOrder', $data); |
|
211 | + } |
|
212 | + |
|
213 | + $pos++; |
|
214 | + }//end foreach |
|
215 | + |
|
216 | + // Ignore the rest of the file. |
|
217 | + return ($phpcsFile->numTokens + 1); |
|
218 | + |
|
219 | + }//end process() |
|
220 | 220 | |
221 | 221 | |
222 | 222 | }//end class |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public function register() |
49 | 49 | { |
50 | - return array(T_OPEN_TAG); |
|
50 | + return array( T_OPEN_TAG ); |
|
51 | 51 | |
52 | 52 | }//end register() |
53 | 53 | |
@@ -61,28 +61,28 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @return int |
63 | 63 | */ |
64 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
64 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
65 | 65 | { |
66 | 66 | $this->currentFile = $phpcsFile; |
67 | 67 | |
68 | 68 | $tokens = $phpcsFile->getTokens(); |
69 | - $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
70 | - |
|
71 | - if ($tokens[$commentStart]['code'] === T_COMMENT) { |
|
72 | - $phpcsFile->addError('You must use "/**" style comments for a file comment', $commentStart, 'WrongStyle'); |
|
73 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); |
|
74 | - return ($phpcsFile->numTokens + 1); |
|
75 | - } else if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) { |
|
76 | - $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing'); |
|
77 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no'); |
|
78 | - return ($phpcsFile->numTokens + 1); |
|
69 | + $commentStart = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
70 | + |
|
71 | + if ( $tokens[ $commentStart ][ 'code' ] === T_COMMENT ) { |
|
72 | + $phpcsFile->addError( 'You must use "/**" style comments for a file comment', $commentStart, 'WrongStyle' ); |
|
73 | + $phpcsFile->recordMetric( $stackPtr, 'File has doc comment', 'yes' ); |
|
74 | + return ( $phpcsFile->numTokens + 1 ); |
|
75 | + } else if ( $commentStart === false || $tokens[ $commentStart ][ 'code' ] !== T_DOC_COMMENT_OPEN_TAG ) { |
|
76 | + $phpcsFile->addError( 'Missing file doc comment', $stackPtr, 'Missing' ); |
|
77 | + $phpcsFile->recordMetric( $stackPtr, 'File has doc comment', 'no' ); |
|
78 | + return ( $phpcsFile->numTokens + 1 ); |
|
79 | 79 | } |
80 | 80 | |
81 | - $commentEnd = $tokens[$commentStart]['comment_closer']; |
|
81 | + $commentEnd = $tokens[ $commentStart ][ 'comment_closer' ]; |
|
82 | 82 | |
83 | 83 | $nextToken = $phpcsFile->findNext( |
84 | 84 | T_WHITESPACE, |
85 | - ($commentEnd + 1), |
|
85 | + ( $commentEnd + 1 ), |
|
86 | 86 | null, |
87 | 87 | true |
88 | 88 | ); |
@@ -107,25 +107,25 @@ discard block |
||
107 | 107 | T_REQUIRE_ONCE, |
108 | 108 | ); |
109 | 109 | |
110 | - if (in_array($tokens[$nextToken]['code'], $ignore) === true) { |
|
111 | - $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing'); |
|
112 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no'); |
|
113 | - return ($phpcsFile->numTokens + 1); |
|
110 | + if ( in_array( $tokens[ $nextToken ][ 'code' ], $ignore ) === true ) { |
|
111 | + $phpcsFile->addError( 'Missing file doc comment', $stackPtr, 'Missing' ); |
|
112 | + $phpcsFile->recordMetric( $stackPtr, 'File has doc comment', 'no' ); |
|
113 | + return ( $phpcsFile->numTokens + 1 ); |
|
114 | 114 | } |
115 | 115 | |
116 | - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); |
|
116 | + $phpcsFile->recordMetric( $stackPtr, 'File has doc comment', 'yes' ); |
|
117 | 117 | |
118 | 118 | // No blank line between the open tag and the file comment. |
119 | - if ($tokens[$commentStart]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
|
119 | + if ( $tokens[ $commentStart ][ 'line' ] > ( $tokens[ $stackPtr ][ 'line' ] + 1 ) ) { |
|
120 | 120 | $error = 'There must be no blank lines before the file comment'; |
121 | - $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen'); |
|
121 | + $phpcsFile->addError( $error, $stackPtr, 'SpacingAfterOpen' ); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | // Exactly one blank line after the file comment. |
125 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); |
|
126 | - if ($tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) { |
|
125 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $commentEnd + 1 ), null, true ); |
|
126 | + if ( $tokens[ $next ][ 'line' ] !== ( $tokens[ $commentEnd ][ 'line' ] + 2 ) ) { |
|
127 | 127 | $error = 'There must be exactly one blank line after the file comment'; |
128 | - $phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment'); |
|
128 | + $phpcsFile->addError( $error, $commentEnd, 'SpacingAfterComment' ); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | // Required tags in correct order. |
@@ -137,52 +137,52 @@ discard block |
||
137 | 137 | ); |
138 | 138 | |
139 | 139 | $foundTags = array(); |
140 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
141 | - $name = $tokens[$tag]['content']; |
|
142 | - $isRequired = isset($required[$name]); |
|
140 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $tag ) { |
|
141 | + $name = $tokens[ $tag ][ 'content' ]; |
|
142 | + $isRequired = isset( $required[ $name ] ); |
|
143 | 143 | |
144 | - if ($isRequired === true && in_array($name, $foundTags) === true) { |
|
144 | + if ( $isRequired === true && in_array( $name, $foundTags ) === true ) { |
|
145 | 145 | $error = 'Only one %s tag is allowed in a file comment'; |
146 | - $data = array($name); |
|
147 | - $phpcsFile->addError($error, $tag, 'Duplicate'.ucfirst(substr($name, 1)).'Tag', $data); |
|
146 | + $data = array( $name ); |
|
147 | + $phpcsFile->addError( $error, $tag, 'Duplicate' . ucfirst( substr( $name, 1 ) ) . 'Tag', $data ); |
|
148 | 148 | } |
149 | 149 | |
150 | - $foundTags[] = $name; |
|
150 | + $foundTags[ ] = $name; |
|
151 | 151 | |
152 | - if ($isRequired === false) { |
|
152 | + if ( $isRequired === false ) { |
|
153 | 153 | continue; |
154 | 154 | } |
155 | 155 | |
156 | - $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
157 | - if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
|
156 | + $string = $phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $commentEnd ); |
|
157 | + if ( $string === false || $tokens[ $string ][ 'line' ] !== $tokens[ $tag ][ 'line' ] ) { |
|
158 | 158 | $error = 'Content missing for %s tag in file comment'; |
159 | - $data = array($name); |
|
160 | - $phpcsFile->addError($error, $tag, 'Empty'.ucfirst(substr($name, 1)).'Tag', $data); |
|
159 | + $data = array( $name ); |
|
160 | + $phpcsFile->addError( $error, $tag, 'Empty' . ucfirst( substr( $name, 1 ) ) . 'Tag', $data ); |
|
161 | 161 | continue; |
162 | 162 | } |
163 | 163 | |
164 | - if ($name === '@author') { |
|
165 | - if ($tokens[$string]['content'] !== 'Squiz Pty Ltd <[email protected]>') { |
|
164 | + if ( $name === '@author' ) { |
|
165 | + if ( $tokens[ $string ][ 'content' ] !== 'Squiz Pty Ltd <[email protected]>' ) { |
|
166 | 166 | $error = 'Expected "Squiz Pty Ltd <[email protected]>" for author tag'; |
167 | - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectAuthor'); |
|
168 | - if ($fix === true) { |
|
167 | + $fix = $phpcsFile->addFixableError( $error, $tag, 'IncorrectAuthor' ); |
|
168 | + if ( $fix === true ) { |
|
169 | 169 | $expected = 'Squiz Pty Ltd <[email protected]>'; |
170 | - $phpcsFile->fixer->replaceToken($string, $expected); |
|
170 | + $phpcsFile->fixer->replaceToken( $string, $expected ); |
|
171 | 171 | } |
172 | 172 | } |
173 | - } else if ($name === '@copyright') { |
|
174 | - if (preg_match('/^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$/', $tokens[$string]['content']) === 0) { |
|
173 | + } else if ( $name === '@copyright' ) { |
|
174 | + if ( preg_match( '/^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$/', $tokens[ $string ][ 'content' ] ) === 0 ) { |
|
175 | 175 | $error = 'Expected "xxxx-xxxx Squiz Pty Ltd (ABN 77 084 670 600)" for copyright declaration'; |
176 | - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyright'); |
|
177 | - if ($fix === true) { |
|
176 | + $fix = $phpcsFile->addFixableError( $error, $tag, 'IncorrectCopyright' ); |
|
177 | + if ( $fix === true ) { |
|
178 | 178 | $matches = array(); |
179 | - preg_match('/^(([0-9]{4})(-[0-9]{4})?)?.*$/', $tokens[$string]['content'], $matches); |
|
180 | - if (isset($matches[1]) === false) { |
|
181 | - $matches[1] = date('Y'); |
|
179 | + preg_match( '/^(([0-9]{4})(-[0-9]{4})?)?.*$/', $tokens[ $string ][ 'content' ], $matches ); |
|
180 | + if ( isset( $matches[ 1 ] ) === false ) { |
|
181 | + $matches[ 1 ] = date( 'Y' ); |
|
182 | 182 | } |
183 | 183 | |
184 | - $expected = $matches[1].' Squiz Pty Ltd (ABN 77 084 670 600)'; |
|
185 | - $phpcsFile->fixer->replaceToken($string, $expected); |
|
184 | + $expected = $matches[ 1 ] . ' Squiz Pty Ltd (ABN 77 084 670 600)'; |
|
185 | + $phpcsFile->fixer->replaceToken( $string, $expected ); |
|
186 | 186 | } |
187 | 187 | } |
188 | 188 | }//end if |
@@ -190,31 +190,31 @@ discard block |
||
190 | 190 | |
191 | 191 | // Check if the tags are in the correct position. |
192 | 192 | $pos = 0; |
193 | - foreach ($required as $tag => $true) { |
|
194 | - if (in_array($tag, $foundTags) === false) { |
|
193 | + foreach ( $required as $tag => $true ) { |
|
194 | + if ( in_array( $tag, $foundTags ) === false ) { |
|
195 | 195 | $error = 'Missing %s tag in file comment'; |
196 | - $data = array($tag); |
|
197 | - $phpcsFile->addError($error, $commentEnd, 'Missing'.ucfirst(substr($tag, 1)).'Tag', $data); |
|
196 | + $data = array( $tag ); |
|
197 | + $phpcsFile->addError( $error, $commentEnd, 'Missing' . ucfirst( substr( $tag, 1 ) ) . 'Tag', $data ); |
|
198 | 198 | } |
199 | 199 | |
200 | - if (isset($foundTags[$pos]) === false) { |
|
200 | + if ( isset( $foundTags[ $pos ] ) === false ) { |
|
201 | 201 | break; |
202 | 202 | } |
203 | 203 | |
204 | - if ($foundTags[$pos] !== $tag) { |
|
204 | + if ( $foundTags[ $pos ] !== $tag ) { |
|
205 | 205 | $error = 'The tag in position %s should be the %s tag'; |
206 | 206 | $data = array( |
207 | - ($pos + 1), |
|
207 | + ( $pos + 1 ), |
|
208 | 208 | $tag, |
209 | 209 | ); |
210 | - $phpcsFile->addError($error, $tokens[$commentStart]['comment_tags'][$pos], ucfirst(substr($tag, 1)).'TagOrder', $data); |
|
210 | + $phpcsFile->addError( $error, $tokens[ $commentStart ][ 'comment_tags' ][ $pos ], ucfirst( substr( $tag, 1 ) ) . 'TagOrder', $data ); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | $pos++; |
214 | 214 | }//end foreach |
215 | 215 | |
216 | 216 | // Ignore the rest of the file. |
217 | - return ($phpcsFile->numTokens + 1); |
|
217 | + return ( $phpcsFile->numTokens + 1 ); |
|
218 | 218 | |
219 | 219 | }//end process() |
220 | 220 |
@@ -26,8 +26,7 @@ discard block |
||
26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
27 | 27 | */ |
28 | 28 | |
29 | -class Squiz_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff |
|
30 | -{ |
|
29 | +class Squiz_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff { |
|
31 | 30 | |
32 | 31 | /** |
33 | 32 | * A list of tokenizers this sniff supports. |
@@ -45,8 +44,7 @@ discard block |
||
45 | 44 | * |
46 | 45 | * @return array |
47 | 46 | */ |
48 | - public function register() |
|
49 | - { |
|
47 | + public function register() { |
|
50 | 48 | return array(T_OPEN_TAG); |
51 | 49 | |
52 | 50 | }//end register() |
@@ -61,8 +59,7 @@ discard block |
||
61 | 59 | * |
62 | 60 | * @return int |
63 | 61 | */ |
64 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
65 | - { |
|
62 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
66 | 63 | $this->currentFile = $phpcsFile; |
67 | 64 | |
68 | 65 | $tokens = $phpcsFile->getTokens(); |
@@ -34,7 +34,7 @@ |
||
34 | 34 | /** |
35 | 35 | * Returns an array of tokens this test wants to listen for. |
36 | 36 | * |
37 | - * @return array |
|
37 | + * @return string[] |
|
38 | 38 | */ |
39 | 39 | public function register() |
40 | 40 | { |
@@ -31,98 +31,98 @@ |
||
31 | 31 | { |
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * Returns an array of tokens this test wants to listen for. |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function register() |
|
40 | - { |
|
41 | - return array(T_INLINE_THEN); |
|
42 | - |
|
43 | - }//end register() |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * Processes this sniff, when one of its tokens is encountered. |
|
48 | - * |
|
49 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
50 | - * @param int $stackPtr The position of the current token in the |
|
51 | - * stack passed in $tokens. |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
56 | - { |
|
57 | - $tokens = $phpcsFile->getTokens(); |
|
58 | - |
|
59 | - $openBracket = null; |
|
60 | - $closeBracket = null; |
|
61 | - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
62 | - $parens = $tokens[$stackPtr]['nested_parenthesis']; |
|
63 | - $openBracket = array_pop($parens); |
|
64 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
65 | - } |
|
66 | - |
|
67 | - // Find the beginning of the statement. If we don't find a |
|
68 | - // semicolon (end of statement) or comma (end of array value) |
|
69 | - // then assume the content before the closing parenthesis is the end. |
|
70 | - $else = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1)); |
|
71 | - $statementEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), ($else + 1), $closeBracket); |
|
72 | - if ($statementEnd === false) { |
|
73 | - $statementEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
|
74 | - } |
|
75 | - |
|
76 | - // Make sure it's all on the same line. |
|
77 | - if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) { |
|
78 | - $error = 'Inline shorthand IF statement must be declared on a single line'; |
|
79 | - $phpcsFile->addError($error, $stackPtr, 'NotSingleLine'); |
|
80 | - return; |
|
81 | - } |
|
82 | - |
|
83 | - // Make sure there are spaces around the question mark. |
|
84 | - $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
85 | - $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
86 | - if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) { |
|
87 | - $error = 'Inline shorthand IF statement requires brackets around comparison'; |
|
88 | - $phpcsFile->addError($error, $stackPtr, 'NoBrackets'); |
|
89 | - return; |
|
90 | - } |
|
91 | - |
|
92 | - $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
|
93 | - if ($spaceBefore !== 1) { |
|
94 | - $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found'; |
|
95 | - $data = array($spaceBefore); |
|
96 | - $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data); |
|
97 | - } |
|
98 | - |
|
99 | - $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1)); |
|
100 | - if ($spaceAfter !== 1) { |
|
101 | - $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found'; |
|
102 | - $data = array($spaceAfter); |
|
103 | - $phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data); |
|
104 | - } |
|
105 | - |
|
106 | - // Make sure the ELSE has the correct spacing. |
|
107 | - $inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false); |
|
108 | - $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true); |
|
109 | - $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true); |
|
110 | - |
|
111 | - $spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
|
112 | - if ($spaceBefore !== 1) { |
|
113 | - $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found'; |
|
114 | - $data = array($spaceBefore); |
|
115 | - $phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data); |
|
116 | - } |
|
117 | - |
|
118 | - $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1)); |
|
119 | - if ($spaceAfter !== 1) { |
|
120 | - $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found'; |
|
121 | - $data = array($spaceAfter); |
|
122 | - $phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data); |
|
123 | - } |
|
124 | - |
|
125 | - }//end process() |
|
34 | + /** |
|
35 | + * Returns an array of tokens this test wants to listen for. |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function register() |
|
40 | + { |
|
41 | + return array(T_INLINE_THEN); |
|
42 | + |
|
43 | + }//end register() |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * Processes this sniff, when one of its tokens is encountered. |
|
48 | + * |
|
49 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
50 | + * @param int $stackPtr The position of the current token in the |
|
51 | + * stack passed in $tokens. |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
56 | + { |
|
57 | + $tokens = $phpcsFile->getTokens(); |
|
58 | + |
|
59 | + $openBracket = null; |
|
60 | + $closeBracket = null; |
|
61 | + if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
62 | + $parens = $tokens[$stackPtr]['nested_parenthesis']; |
|
63 | + $openBracket = array_pop($parens); |
|
64 | + $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
65 | + } |
|
66 | + |
|
67 | + // Find the beginning of the statement. If we don't find a |
|
68 | + // semicolon (end of statement) or comma (end of array value) |
|
69 | + // then assume the content before the closing parenthesis is the end. |
|
70 | + $else = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1)); |
|
71 | + $statementEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), ($else + 1), $closeBracket); |
|
72 | + if ($statementEnd === false) { |
|
73 | + $statementEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
|
74 | + } |
|
75 | + |
|
76 | + // Make sure it's all on the same line. |
|
77 | + if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) { |
|
78 | + $error = 'Inline shorthand IF statement must be declared on a single line'; |
|
79 | + $phpcsFile->addError($error, $stackPtr, 'NotSingleLine'); |
|
80 | + return; |
|
81 | + } |
|
82 | + |
|
83 | + // Make sure there are spaces around the question mark. |
|
84 | + $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
85 | + $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
86 | + if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) { |
|
87 | + $error = 'Inline shorthand IF statement requires brackets around comparison'; |
|
88 | + $phpcsFile->addError($error, $stackPtr, 'NoBrackets'); |
|
89 | + return; |
|
90 | + } |
|
91 | + |
|
92 | + $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
|
93 | + if ($spaceBefore !== 1) { |
|
94 | + $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found'; |
|
95 | + $data = array($spaceBefore); |
|
96 | + $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data); |
|
97 | + } |
|
98 | + |
|
99 | + $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1)); |
|
100 | + if ($spaceAfter !== 1) { |
|
101 | + $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found'; |
|
102 | + $data = array($spaceAfter); |
|
103 | + $phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data); |
|
104 | + } |
|
105 | + |
|
106 | + // Make sure the ELSE has the correct spacing. |
|
107 | + $inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false); |
|
108 | + $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true); |
|
109 | + $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true); |
|
110 | + |
|
111 | + $spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
|
112 | + if ($spaceBefore !== 1) { |
|
113 | + $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found'; |
|
114 | + $data = array($spaceBefore); |
|
115 | + $phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data); |
|
116 | + } |
|
117 | + |
|
118 | + $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1)); |
|
119 | + if ($spaceAfter !== 1) { |
|
120 | + $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found'; |
|
121 | + $data = array($spaceAfter); |
|
122 | + $phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data); |
|
123 | + } |
|
124 | + |
|
125 | + }//end process() |
|
126 | 126 | |
127 | 127 | |
128 | 128 | }//end class |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | */ |
39 | 39 | public function register() |
40 | 40 | { |
41 | - return array(T_INLINE_THEN); |
|
41 | + return array( T_INLINE_THEN ); |
|
42 | 42 | |
43 | 43 | }//end register() |
44 | 44 | |
@@ -52,74 +52,74 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return void |
54 | 54 | */ |
55 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
55 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
56 | 56 | { |
57 | 57 | $tokens = $phpcsFile->getTokens(); |
58 | 58 | |
59 | 59 | $openBracket = null; |
60 | 60 | $closeBracket = null; |
61 | - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
62 | - $parens = $tokens[$stackPtr]['nested_parenthesis']; |
|
63 | - $openBracket = array_pop($parens); |
|
64 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
61 | + if ( isset( $tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === true ) { |
|
62 | + $parens = $tokens[ $stackPtr ][ 'nested_parenthesis' ]; |
|
63 | + $openBracket = array_pop( $parens ); |
|
64 | + $closeBracket = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | // Find the beginning of the statement. If we don't find a |
68 | 68 | // semicolon (end of statement) or comma (end of array value) |
69 | 69 | // then assume the content before the closing parenthesis is the end. |
70 | - $else = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1)); |
|
71 | - $statementEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), ($else + 1), $closeBracket); |
|
72 | - if ($statementEnd === false) { |
|
73 | - $statementEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
|
70 | + $else = $phpcsFile->findNext( T_INLINE_ELSE, ( $stackPtr + 1 ) ); |
|
71 | + $statementEnd = $phpcsFile->findNext( array( T_SEMICOLON, T_COMMA ), ( $else + 1 ), $closeBracket ); |
|
72 | + if ( $statementEnd === false ) { |
|
73 | + $statementEnd = $phpcsFile->findPrevious( T_WHITESPACE, ( $closeBracket - 1 ), null, true ); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | // Make sure it's all on the same line. |
77 | - if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) { |
|
77 | + if ( $tokens[ $statementEnd ][ 'line' ] !== $tokens[ $stackPtr ][ 'line' ] ) { |
|
78 | 78 | $error = 'Inline shorthand IF statement must be declared on a single line'; |
79 | - $phpcsFile->addError($error, $stackPtr, 'NotSingleLine'); |
|
79 | + $phpcsFile->addError( $error, $stackPtr, 'NotSingleLine' ); |
|
80 | 80 | return; |
81 | 81 | } |
82 | 82 | |
83 | 83 | // Make sure there are spaces around the question mark. |
84 | - $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
85 | - $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
86 | - if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) { |
|
84 | + $contentBefore = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); |
|
85 | + $contentAfter = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
86 | + if ( $tokens[ $contentBefore ][ 'code' ] !== T_CLOSE_PARENTHESIS ) { |
|
87 | 87 | $error = 'Inline shorthand IF statement requires brackets around comparison'; |
88 | - $phpcsFile->addError($error, $stackPtr, 'NoBrackets'); |
|
88 | + $phpcsFile->addError( $error, $stackPtr, 'NoBrackets' ); |
|
89 | 89 | return; |
90 | 90 | } |
91 | 91 | |
92 | - $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
|
93 | - if ($spaceBefore !== 1) { |
|
92 | + $spaceBefore = ( $tokens[ $stackPtr ][ 'column' ] - ( $tokens[ $contentBefore ][ 'column' ] + $tokens[ $contentBefore ][ 'length' ] ) ); |
|
93 | + if ( $spaceBefore !== 1 ) { |
|
94 | 94 | $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found'; |
95 | - $data = array($spaceBefore); |
|
96 | - $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data); |
|
95 | + $data = array( $spaceBefore ); |
|
96 | + $phpcsFile->addError( $error, $stackPtr, 'SpacingBeforeThen', $data ); |
|
97 | 97 | } |
98 | 98 | |
99 | - $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1)); |
|
100 | - if ($spaceAfter !== 1) { |
|
99 | + $spaceAfter = ( ( $tokens[ $contentAfter ][ 'column' ] ) - ( $tokens[ $stackPtr ][ 'column' ] + 1 ) ); |
|
100 | + if ( $spaceAfter !== 1 ) { |
|
101 | 101 | $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found'; |
102 | - $data = array($spaceAfter); |
|
103 | - $phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data); |
|
102 | + $data = array( $spaceAfter ); |
|
103 | + $phpcsFile->addError( $error, $stackPtr, 'SpacingAfterThen', $data ); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | // Make sure the ELSE has the correct spacing. |
107 | - $inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false); |
|
108 | - $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true); |
|
109 | - $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true); |
|
107 | + $inlineElse = $phpcsFile->findNext( T_INLINE_ELSE, ( $stackPtr + 1 ), $statementEnd, false ); |
|
108 | + $contentBefore = $phpcsFile->findPrevious( T_WHITESPACE, ( $inlineElse - 1 ), null, true ); |
|
109 | + $contentAfter = $phpcsFile->findNext( T_WHITESPACE, ( $inlineElse + 1 ), null, true ); |
|
110 | 110 | |
111 | - $spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
|
112 | - if ($spaceBefore !== 1) { |
|
111 | + $spaceBefore = ( $tokens[ $inlineElse ][ 'column' ] - ( $tokens[ $contentBefore ][ 'column' ] + $tokens[ $contentBefore ][ 'length' ] ) ); |
|
112 | + if ( $spaceBefore !== 1 ) { |
|
113 | 113 | $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found'; |
114 | - $data = array($spaceBefore); |
|
115 | - $phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data); |
|
114 | + $data = array( $spaceBefore ); |
|
115 | + $phpcsFile->addError( $error, $inlineElse, 'SpacingBeforeElse', $data ); |
|
116 | 116 | } |
117 | 117 | |
118 | - $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1)); |
|
119 | - if ($spaceAfter !== 1) { |
|
118 | + $spaceAfter = ( ( $tokens[ $contentAfter ][ 'column' ] ) - ( $tokens[ $inlineElse ][ 'column' ] + 1 ) ); |
|
119 | + if ( $spaceAfter !== 1 ) { |
|
120 | 120 | $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found'; |
121 | - $data = array($spaceAfter); |
|
122 | - $phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data); |
|
121 | + $data = array( $spaceAfter ); |
|
122 | + $phpcsFile->addError( $error, $inlineElse, 'SpacingAfterElse', $data ); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | }//end process() |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * @version Release: @package_version@ |
28 | 28 | * @link http://pear.php.net/package/PHP_CodeSniffer |
29 | 29 | */ |
30 | -class Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff implements PHP_CodeSniffer_Sniff |
|
31 | -{ |
|
30 | +class Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff implements PHP_CodeSniffer_Sniff { |
|
32 | 31 | |
33 | 32 | |
34 | 33 | /** |
@@ -36,8 +35,7 @@ discard block |
||
36 | 35 | * |
37 | 36 | * @return array |
38 | 37 | */ |
39 | - public function register() |
|
40 | - { |
|
38 | + public function register() { |
|
41 | 39 | return array(T_INLINE_THEN); |
42 | 40 | |
43 | 41 | }//end register() |
@@ -52,8 +50,7 @@ discard block |
||
52 | 50 | * |
53 | 51 | * @return void |
54 | 52 | */ |
55 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
56 | - { |
|
53 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
57 | 54 | $tokens = $phpcsFile->getTokens(); |
58 | 55 | |
59 | 56 | $openBracket = null; |
@@ -39,7 +39,7 @@ |
||
39 | 39 | /** |
40 | 40 | * Returns the token types that this sniff is interested in. |
41 | 41 | * |
42 | - * @return int[] |
|
42 | + * @return string[] |
|
43 | 43 | */ |
44 | 44 | public function register() |
45 | 45 | { |
@@ -28,72 +28,72 @@ |
||
28 | 28 | class Squiz_Sniffs_CSS_MissingColonSniff implements PHP_CodeSniffer_Sniff |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * A list of tokenizers this sniff supports. |
|
33 | - * |
|
34 | - * @var array |
|
35 | - */ |
|
36 | - public $supportedTokenizers = array('CSS'); |
|
31 | + /** |
|
32 | + * A list of tokenizers this sniff supports. |
|
33 | + * |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + public $supportedTokenizers = array('CSS'); |
|
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * Returns the token types that this sniff is interested in. |
|
41 | - * |
|
42 | - * @return int[] |
|
43 | - */ |
|
44 | - public function register() |
|
45 | - { |
|
46 | - return array(T_OPEN_CURLY_BRACKET); |
|
39 | + /** |
|
40 | + * Returns the token types that this sniff is interested in. |
|
41 | + * |
|
42 | + * @return int[] |
|
43 | + */ |
|
44 | + public function register() |
|
45 | + { |
|
46 | + return array(T_OPEN_CURLY_BRACKET); |
|
47 | 47 | |
48 | - }//end register() |
|
48 | + }//end register() |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * Processes the tokens that this sniff is interested in. |
|
53 | - * |
|
54 | - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
55 | - * @param int $stackPtr The position in the stack where |
|
56 | - * the token was found. |
|
57 | - * |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | - { |
|
62 | - $tokens = $phpcsFile->getTokens(); |
|
63 | - $lastLine = $tokens[$stackPtr]['line']; |
|
64 | - $end = $tokens[$stackPtr]['bracket_closer']; |
|
65 | - $endLine = $tokens[$end]['line']; |
|
51 | + /** |
|
52 | + * Processes the tokens that this sniff is interested in. |
|
53 | + * |
|
54 | + * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
55 | + * @param int $stackPtr The position in the stack where |
|
56 | + * the token was found. |
|
57 | + * |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | + { |
|
62 | + $tokens = $phpcsFile->getTokens(); |
|
63 | + $lastLine = $tokens[$stackPtr]['line']; |
|
64 | + $end = $tokens[$stackPtr]['bracket_closer']; |
|
65 | + $endLine = $tokens[$end]['line']; |
|
66 | 66 | |
67 | - // Do not check nested style definitions as, for example, in @media style rules. |
|
68 | - $nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $end); |
|
69 | - if ($nested !== false) { |
|
70 | - return; |
|
71 | - } |
|
67 | + // Do not check nested style definitions as, for example, in @media style rules. |
|
68 | + $nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $end); |
|
69 | + if ($nested !== false) { |
|
70 | + return; |
|
71 | + } |
|
72 | 72 | |
73 | - $foundColon = false; |
|
74 | - $foundString = false; |
|
75 | - for ($i = ($stackPtr + 1); $i <= $end; $i++) { |
|
76 | - if ($tokens[$i]['line'] !== $lastLine) { |
|
77 | - // We changed lines. |
|
78 | - if ($foundColon === false && $foundString !== false) { |
|
79 | - // We didn't find a colon on the previous line. |
|
80 | - $error = 'No style definition found on line; check for missing colon'; |
|
81 | - $phpcsFile->addError($error, $foundString, 'Found'); |
|
82 | - } |
|
73 | + $foundColon = false; |
|
74 | + $foundString = false; |
|
75 | + for ($i = ($stackPtr + 1); $i <= $end; $i++) { |
|
76 | + if ($tokens[$i]['line'] !== $lastLine) { |
|
77 | + // We changed lines. |
|
78 | + if ($foundColon === false && $foundString !== false) { |
|
79 | + // We didn't find a colon on the previous line. |
|
80 | + $error = 'No style definition found on line; check for missing colon'; |
|
81 | + $phpcsFile->addError($error, $foundString, 'Found'); |
|
82 | + } |
|
83 | 83 | |
84 | - $foundColon = false; |
|
85 | - $foundString = false; |
|
86 | - $lastLine = $tokens[$i]['line']; |
|
87 | - } |
|
84 | + $foundColon = false; |
|
85 | + $foundString = false; |
|
86 | + $lastLine = $tokens[$i]['line']; |
|
87 | + } |
|
88 | 88 | |
89 | - if ($tokens[$i]['code'] === T_STRING) { |
|
90 | - $foundString = $i; |
|
91 | - } else if ($tokens[$i]['code'] === T_COLON) { |
|
92 | - $foundColon = $i; |
|
93 | - } |
|
94 | - }//end for |
|
89 | + if ($tokens[$i]['code'] === T_STRING) { |
|
90 | + $foundString = $i; |
|
91 | + } else if ($tokens[$i]['code'] === T_COLON) { |
|
92 | + $foundColon = $i; |
|
93 | + } |
|
94 | + }//end for |
|
95 | 95 | |
96 | - }//end process() |
|
96 | + }//end process() |
|
97 | 97 | |
98 | 98 | |
99 | 99 | }//end class |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @var array |
35 | 35 | */ |
36 | - public $supportedTokenizers = array('CSS'); |
|
36 | + public $supportedTokenizers = array( 'CSS' ); |
|
37 | 37 | |
38 | 38 | |
39 | 39 | /** |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public function register() |
45 | 45 | { |
46 | - return array(T_OPEN_CURLY_BRACKET); |
|
46 | + return array( T_OPEN_CURLY_BRACKET ); |
|
47 | 47 | |
48 | 48 | }//end register() |
49 | 49 | |
@@ -57,38 +57,38 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @return void |
59 | 59 | */ |
60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
60 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
61 | 61 | { |
62 | 62 | $tokens = $phpcsFile->getTokens(); |
63 | - $lastLine = $tokens[$stackPtr]['line']; |
|
64 | - $end = $tokens[$stackPtr]['bracket_closer']; |
|
65 | - $endLine = $tokens[$end]['line']; |
|
63 | + $lastLine = $tokens[ $stackPtr ][ 'line' ]; |
|
64 | + $end = $tokens[ $stackPtr ][ 'bracket_closer' ]; |
|
65 | + $endLine = $tokens[ $end ][ 'line' ]; |
|
66 | 66 | |
67 | 67 | // Do not check nested style definitions as, for example, in @media style rules. |
68 | - $nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $end); |
|
69 | - if ($nested !== false) { |
|
68 | + $nested = $phpcsFile->findNext( T_OPEN_CURLY_BRACKET, ( $stackPtr + 1 ), $end ); |
|
69 | + if ( $nested !== false ) { |
|
70 | 70 | return; |
71 | 71 | } |
72 | 72 | |
73 | 73 | $foundColon = false; |
74 | 74 | $foundString = false; |
75 | - for ($i = ($stackPtr + 1); $i <= $end; $i++) { |
|
76 | - if ($tokens[$i]['line'] !== $lastLine) { |
|
75 | + for ( $i = ( $stackPtr + 1 ); $i <= $end; $i++ ) { |
|
76 | + if ( $tokens[ $i ][ 'line' ] !== $lastLine ) { |
|
77 | 77 | // We changed lines. |
78 | - if ($foundColon === false && $foundString !== false) { |
|
78 | + if ( $foundColon === false && $foundString !== false ) { |
|
79 | 79 | // We didn't find a colon on the previous line. |
80 | 80 | $error = 'No style definition found on line; check for missing colon'; |
81 | - $phpcsFile->addError($error, $foundString, 'Found'); |
|
81 | + $phpcsFile->addError( $error, $foundString, 'Found' ); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | $foundColon = false; |
85 | 85 | $foundString = false; |
86 | - $lastLine = $tokens[$i]['line']; |
|
86 | + $lastLine = $tokens[ $i ][ 'line' ]; |
|
87 | 87 | } |
88 | 88 | |
89 | - if ($tokens[$i]['code'] === T_STRING) { |
|
89 | + if ( $tokens[ $i ][ 'code' ] === T_STRING ) { |
|
90 | 90 | $foundString = $i; |
91 | - } else if ($tokens[$i]['code'] === T_COLON) { |
|
91 | + } else if ( $tokens[ $i ][ 'code' ] === T_COLON ) { |
|
92 | 92 | $foundColon = $i; |
93 | 93 | } |
94 | 94 | }//end for |
@@ -25,8 +25,7 @@ discard block |
||
25 | 25 | * @version Release: @package_version@ |
26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
27 | 27 | */ |
28 | -class Squiz_Sniffs_CSS_MissingColonSniff implements PHP_CodeSniffer_Sniff |
|
29 | -{ |
|
28 | +class Squiz_Sniffs_CSS_MissingColonSniff implements PHP_CodeSniffer_Sniff { |
|
30 | 29 | |
31 | 30 | /** |
32 | 31 | * A list of tokenizers this sniff supports. |
@@ -41,8 +40,7 @@ discard block |
||
41 | 40 | * |
42 | 41 | * @return int[] |
43 | 42 | */ |
44 | - public function register() |
|
45 | - { |
|
43 | + public function register() { |
|
46 | 44 | return array(T_OPEN_CURLY_BRACKET); |
47 | 45 | |
48 | 46 | }//end register() |
@@ -57,8 +55,7 @@ discard block |
||
57 | 55 | * |
58 | 56 | * @return void |
59 | 57 | */ |
60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | - { |
|
58 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
62 | 59 | $tokens = $phpcsFile->getTokens(); |
63 | 60 | $lastLine = $tokens[$stackPtr]['line']; |
64 | 61 | $end = $tokens[$stackPtr]['bracket_closer']; |
@@ -55,7 +55,7 @@ |
||
55 | 55 | /** |
56 | 56 | * Returns the token types that this sniff is interested in. |
57 | 57 | * |
58 | - * @return int[] |
|
58 | + * @return string[] |
|
59 | 59 | */ |
60 | 60 | public function register() |
61 | 61 | { |
@@ -29,163 +29,163 @@ |
||
29 | 29 | class Squiz_Sniffs_CSS_ShorthandSizeSniff implements PHP_CodeSniffer_Sniff |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * A list of tokenizers this sniff supports. |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - public $supportedTokenizers = array('CSS'); |
|
38 | - |
|
39 | - /** |
|
40 | - * A list of styles that we shouldn't check. |
|
41 | - * |
|
42 | - * These have values that looks like sizes, but are not. |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - public $excludeStyles = array( |
|
47 | - 'background-position' => 'background-position', |
|
48 | - 'box-shadow' => 'box-shadow', |
|
49 | - 'transform-origin' => 'transform-origin', |
|
50 | - '-webkit-transform-origin' => '-webkit-transform-origin', |
|
51 | - '-ms-transform-origin' => '-ms-transform-origin', |
|
52 | - ); |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Returns the token types that this sniff is interested in. |
|
57 | - * |
|
58 | - * @return int[] |
|
59 | - */ |
|
60 | - public function register() |
|
61 | - { |
|
62 | - return array(T_STYLE); |
|
63 | - |
|
64 | - }//end register() |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Processes the tokens that this sniff is interested in. |
|
69 | - * |
|
70 | - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
71 | - * @param int $stackPtr The position in the stack where |
|
72 | - * the token was found. |
|
73 | - * |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
77 | - { |
|
78 | - $tokens = $phpcsFile->getTokens(); |
|
79 | - |
|
80 | - // Some styles look like shorthand but are not actually a set of 4 sizes. |
|
81 | - $style = strtolower($tokens[$stackPtr]['content']); |
|
82 | - if (isset($this->excludeStyles[$style]) === true) { |
|
83 | - return; |
|
84 | - } |
|
85 | - |
|
86 | - // Get the whole style content. |
|
87 | - $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
|
88 | - $origContent = $phpcsFile->getTokensAsString(($stackPtr + 1), ($end - $stackPtr - 1)); |
|
89 | - $origContent = trim($origContent, ': '); |
|
90 | - |
|
91 | - // Account for a !important annotation. |
|
92 | - $content = $origContent; |
|
93 | - if (substr($content, -10) === '!important') { |
|
94 | - $content = substr($content, 0, -10); |
|
95 | - $content = trim($content); |
|
96 | - } |
|
97 | - |
|
98 | - // Check if this style value is a set of numbers with optional prefixes. |
|
99 | - $content = preg_replace('/\s+/', ' ', $content); |
|
100 | - $values = array(); |
|
101 | - $num = preg_match_all( |
|
102 | - '/([0-9]+)([a-zA-Z]{2}\s+|%\s+|\s+)/', |
|
103 | - $content.' ', |
|
104 | - $values, |
|
105 | - PREG_SET_ORDER |
|
106 | - ); |
|
107 | - |
|
108 | - // Only interested in styles that have multiple sizes defined. |
|
109 | - if ($num < 2) { |
|
110 | - return; |
|
111 | - } |
|
112 | - |
|
113 | - // Rebuild the content we matched to ensure we got everything. |
|
114 | - $matched = ''; |
|
115 | - foreach ($values as $value) { |
|
116 | - $matched .= $value[0]; |
|
117 | - } |
|
118 | - |
|
119 | - if ($content !== trim($matched)) { |
|
120 | - return; |
|
121 | - } |
|
122 | - |
|
123 | - if ($num === 3) { |
|
124 | - $expected = trim($content.' '.$values[1][1].$values[1][2]); |
|
125 | - $error = 'Shorthand syntax not allowed here; use %s instead'; |
|
126 | - $data = array($expected); |
|
127 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed', $data); |
|
128 | - |
|
129 | - if ($fix === true) { |
|
130 | - $phpcsFile->fixer->beginChangeset(); |
|
131 | - if (substr($origContent, -10) === '!important') { |
|
132 | - $expected .= ' !important'; |
|
133 | - } |
|
134 | - |
|
135 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
136 | - $phpcsFile->fixer->replaceToken($next, $expected); |
|
137 | - for ($next++; $next < $end; $next++) { |
|
138 | - $phpcsFile->fixer->replaceToken($next, ''); |
|
139 | - } |
|
140 | - |
|
141 | - $phpcsFile->fixer->endChangeset(); |
|
142 | - } |
|
143 | - |
|
144 | - return; |
|
145 | - }//end if |
|
146 | - |
|
147 | - if ($num === 2) { |
|
148 | - if ($values[0][0] !== $values[1][0]) { |
|
149 | - // Both values are different, so it is already shorthand. |
|
150 | - return; |
|
151 | - } |
|
152 | - } else if ($values[0][0] !== $values[2][0] || $values[1][0] !== $values[3][0]) { |
|
153 | - // Can't shorthand this. |
|
154 | - return; |
|
155 | - } |
|
156 | - |
|
157 | - if ($values[0][0] === $values[1][0]) { |
|
158 | - // All values are the same. |
|
159 | - $expected = $values[0][0]; |
|
160 | - } else { |
|
161 | - $expected = $values[0][0].' '.$values[1][0]; |
|
162 | - } |
|
163 | - |
|
164 | - $expected = preg_replace('/\s+/', ' ', trim($expected)); |
|
165 | - |
|
166 | - $error = 'Size definitions must use shorthand if available; expected "%s" but found "%s"'; |
|
167 | - $data = array( |
|
168 | - $expected, |
|
169 | - $content, |
|
170 | - ); |
|
171 | - |
|
172 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotUsed', $data); |
|
173 | - if ($fix === true) { |
|
174 | - $phpcsFile->fixer->beginChangeset(); |
|
175 | - if (substr($origContent, -10) === '!important') { |
|
176 | - $expected .= ' !important'; |
|
177 | - } |
|
178 | - |
|
179 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
180 | - $phpcsFile->fixer->replaceToken($next, $expected); |
|
181 | - for ($next++; $next < $end; $next++) { |
|
182 | - $phpcsFile->fixer->replaceToken($next, ''); |
|
183 | - } |
|
184 | - |
|
185 | - $phpcsFile->fixer->endChangeset(); |
|
186 | - } |
|
187 | - |
|
188 | - }//end process() |
|
32 | + /** |
|
33 | + * A list of tokenizers this sniff supports. |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + public $supportedTokenizers = array('CSS'); |
|
38 | + |
|
39 | + /** |
|
40 | + * A list of styles that we shouldn't check. |
|
41 | + * |
|
42 | + * These have values that looks like sizes, but are not. |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + public $excludeStyles = array( |
|
47 | + 'background-position' => 'background-position', |
|
48 | + 'box-shadow' => 'box-shadow', |
|
49 | + 'transform-origin' => 'transform-origin', |
|
50 | + '-webkit-transform-origin' => '-webkit-transform-origin', |
|
51 | + '-ms-transform-origin' => '-ms-transform-origin', |
|
52 | + ); |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Returns the token types that this sniff is interested in. |
|
57 | + * |
|
58 | + * @return int[] |
|
59 | + */ |
|
60 | + public function register() |
|
61 | + { |
|
62 | + return array(T_STYLE); |
|
63 | + |
|
64 | + }//end register() |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Processes the tokens that this sniff is interested in. |
|
69 | + * |
|
70 | + * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
71 | + * @param int $stackPtr The position in the stack where |
|
72 | + * the token was found. |
|
73 | + * |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
77 | + { |
|
78 | + $tokens = $phpcsFile->getTokens(); |
|
79 | + |
|
80 | + // Some styles look like shorthand but are not actually a set of 4 sizes. |
|
81 | + $style = strtolower($tokens[$stackPtr]['content']); |
|
82 | + if (isset($this->excludeStyles[$style]) === true) { |
|
83 | + return; |
|
84 | + } |
|
85 | + |
|
86 | + // Get the whole style content. |
|
87 | + $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
|
88 | + $origContent = $phpcsFile->getTokensAsString(($stackPtr + 1), ($end - $stackPtr - 1)); |
|
89 | + $origContent = trim($origContent, ': '); |
|
90 | + |
|
91 | + // Account for a !important annotation. |
|
92 | + $content = $origContent; |
|
93 | + if (substr($content, -10) === '!important') { |
|
94 | + $content = substr($content, 0, -10); |
|
95 | + $content = trim($content); |
|
96 | + } |
|
97 | + |
|
98 | + // Check if this style value is a set of numbers with optional prefixes. |
|
99 | + $content = preg_replace('/\s+/', ' ', $content); |
|
100 | + $values = array(); |
|
101 | + $num = preg_match_all( |
|
102 | + '/([0-9]+)([a-zA-Z]{2}\s+|%\s+|\s+)/', |
|
103 | + $content.' ', |
|
104 | + $values, |
|
105 | + PREG_SET_ORDER |
|
106 | + ); |
|
107 | + |
|
108 | + // Only interested in styles that have multiple sizes defined. |
|
109 | + if ($num < 2) { |
|
110 | + return; |
|
111 | + } |
|
112 | + |
|
113 | + // Rebuild the content we matched to ensure we got everything. |
|
114 | + $matched = ''; |
|
115 | + foreach ($values as $value) { |
|
116 | + $matched .= $value[0]; |
|
117 | + } |
|
118 | + |
|
119 | + if ($content !== trim($matched)) { |
|
120 | + return; |
|
121 | + } |
|
122 | + |
|
123 | + if ($num === 3) { |
|
124 | + $expected = trim($content.' '.$values[1][1].$values[1][2]); |
|
125 | + $error = 'Shorthand syntax not allowed here; use %s instead'; |
|
126 | + $data = array($expected); |
|
127 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed', $data); |
|
128 | + |
|
129 | + if ($fix === true) { |
|
130 | + $phpcsFile->fixer->beginChangeset(); |
|
131 | + if (substr($origContent, -10) === '!important') { |
|
132 | + $expected .= ' !important'; |
|
133 | + } |
|
134 | + |
|
135 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
136 | + $phpcsFile->fixer->replaceToken($next, $expected); |
|
137 | + for ($next++; $next < $end; $next++) { |
|
138 | + $phpcsFile->fixer->replaceToken($next, ''); |
|
139 | + } |
|
140 | + |
|
141 | + $phpcsFile->fixer->endChangeset(); |
|
142 | + } |
|
143 | + |
|
144 | + return; |
|
145 | + }//end if |
|
146 | + |
|
147 | + if ($num === 2) { |
|
148 | + if ($values[0][0] !== $values[1][0]) { |
|
149 | + // Both values are different, so it is already shorthand. |
|
150 | + return; |
|
151 | + } |
|
152 | + } else if ($values[0][0] !== $values[2][0] || $values[1][0] !== $values[3][0]) { |
|
153 | + // Can't shorthand this. |
|
154 | + return; |
|
155 | + } |
|
156 | + |
|
157 | + if ($values[0][0] === $values[1][0]) { |
|
158 | + // All values are the same. |
|
159 | + $expected = $values[0][0]; |
|
160 | + } else { |
|
161 | + $expected = $values[0][0].' '.$values[1][0]; |
|
162 | + } |
|
163 | + |
|
164 | + $expected = preg_replace('/\s+/', ' ', trim($expected)); |
|
165 | + |
|
166 | + $error = 'Size definitions must use shorthand if available; expected "%s" but found "%s"'; |
|
167 | + $data = array( |
|
168 | + $expected, |
|
169 | + $content, |
|
170 | + ); |
|
171 | + |
|
172 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotUsed', $data); |
|
173 | + if ($fix === true) { |
|
174 | + $phpcsFile->fixer->beginChangeset(); |
|
175 | + if (substr($origContent, -10) === '!important') { |
|
176 | + $expected .= ' !important'; |
|
177 | + } |
|
178 | + |
|
179 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
180 | + $phpcsFile->fixer->replaceToken($next, $expected); |
|
181 | + for ($next++; $next < $end; $next++) { |
|
182 | + $phpcsFile->fixer->replaceToken($next, ''); |
|
183 | + } |
|
184 | + |
|
185 | + $phpcsFile->fixer->endChangeset(); |
|
186 | + } |
|
187 | + |
|
188 | + }//end process() |
|
189 | 189 | |
190 | 190 | |
191 | 191 | }//end class |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @var array |
36 | 36 | */ |
37 | - public $supportedTokenizers = array('CSS'); |
|
37 | + public $supportedTokenizers = array( 'CSS' ); |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * A list of styles that we shouldn't check. |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function register() |
61 | 61 | { |
62 | - return array(T_STYLE); |
|
62 | + return array( T_STYLE ); |
|
63 | 63 | |
64 | 64 | }//end register() |
65 | 65 | |
@@ -73,69 +73,69 @@ discard block |
||
73 | 73 | * |
74 | 74 | * @return void |
75 | 75 | */ |
76 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
76 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
77 | 77 | { |
78 | 78 | $tokens = $phpcsFile->getTokens(); |
79 | 79 | |
80 | 80 | // Some styles look like shorthand but are not actually a set of 4 sizes. |
81 | - $style = strtolower($tokens[$stackPtr]['content']); |
|
82 | - if (isset($this->excludeStyles[$style]) === true) { |
|
81 | + $style = strtolower( $tokens[ $stackPtr ][ 'content' ] ); |
|
82 | + if ( isset( $this->excludeStyles[ $style ] ) === true ) { |
|
83 | 83 | return; |
84 | 84 | } |
85 | 85 | |
86 | 86 | // Get the whole style content. |
87 | - $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
|
88 | - $origContent = $phpcsFile->getTokensAsString(($stackPtr + 1), ($end - $stackPtr - 1)); |
|
89 | - $origContent = trim($origContent, ': '); |
|
87 | + $end = $phpcsFile->findNext( T_SEMICOLON, ( $stackPtr + 1 ) ); |
|
88 | + $origContent = $phpcsFile->getTokensAsString( ( $stackPtr + 1 ), ( $end - $stackPtr - 1 ) ); |
|
89 | + $origContent = trim( $origContent, ': ' ); |
|
90 | 90 | |
91 | 91 | // Account for a !important annotation. |
92 | 92 | $content = $origContent; |
93 | - if (substr($content, -10) === '!important') { |
|
94 | - $content = substr($content, 0, -10); |
|
95 | - $content = trim($content); |
|
93 | + if ( substr( $content, -10 ) === '!important' ) { |
|
94 | + $content = substr( $content, 0, -10 ); |
|
95 | + $content = trim( $content ); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | // Check if this style value is a set of numbers with optional prefixes. |
99 | - $content = preg_replace('/\s+/', ' ', $content); |
|
99 | + $content = preg_replace( '/\s+/', ' ', $content ); |
|
100 | 100 | $values = array(); |
101 | 101 | $num = preg_match_all( |
102 | 102 | '/([0-9]+)([a-zA-Z]{2}\s+|%\s+|\s+)/', |
103 | - $content.' ', |
|
103 | + $content . ' ', |
|
104 | 104 | $values, |
105 | 105 | PREG_SET_ORDER |
106 | 106 | ); |
107 | 107 | |
108 | 108 | // Only interested in styles that have multiple sizes defined. |
109 | - if ($num < 2) { |
|
109 | + if ( $num < 2 ) { |
|
110 | 110 | return; |
111 | 111 | } |
112 | 112 | |
113 | 113 | // Rebuild the content we matched to ensure we got everything. |
114 | 114 | $matched = ''; |
115 | - foreach ($values as $value) { |
|
116 | - $matched .= $value[0]; |
|
115 | + foreach ( $values as $value ) { |
|
116 | + $matched .= $value[ 0 ]; |
|
117 | 117 | } |
118 | 118 | |
119 | - if ($content !== trim($matched)) { |
|
119 | + if ( $content !== trim( $matched ) ) { |
|
120 | 120 | return; |
121 | 121 | } |
122 | 122 | |
123 | - if ($num === 3) { |
|
124 | - $expected = trim($content.' '.$values[1][1].$values[1][2]); |
|
123 | + if ( $num === 3 ) { |
|
124 | + $expected = trim( $content . ' ' . $values[ 1 ][ 1 ] . $values[ 1 ][ 2 ] ); |
|
125 | 125 | $error = 'Shorthand syntax not allowed here; use %s instead'; |
126 | - $data = array($expected); |
|
127 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed', $data); |
|
126 | + $data = array( $expected ); |
|
127 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'NotAllowed', $data ); |
|
128 | 128 | |
129 | - if ($fix === true) { |
|
129 | + if ( $fix === true ) { |
|
130 | 130 | $phpcsFile->fixer->beginChangeset(); |
131 | - if (substr($origContent, -10) === '!important') { |
|
131 | + if ( substr( $origContent, -10 ) === '!important' ) { |
|
132 | 132 | $expected .= ' !important'; |
133 | 133 | } |
134 | 134 | |
135 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
136 | - $phpcsFile->fixer->replaceToken($next, $expected); |
|
137 | - for ($next++; $next < $end; $next++) { |
|
138 | - $phpcsFile->fixer->replaceToken($next, ''); |
|
135 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 2 ), null, true ); |
|
136 | + $phpcsFile->fixer->replaceToken( $next, $expected ); |
|
137 | + for ( $next++; $next < $end; $next++ ) { |
|
138 | + $phpcsFile->fixer->replaceToken( $next, '' ); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | $phpcsFile->fixer->endChangeset(); |
@@ -144,24 +144,24 @@ discard block |
||
144 | 144 | return; |
145 | 145 | }//end if |
146 | 146 | |
147 | - if ($num === 2) { |
|
148 | - if ($values[0][0] !== $values[1][0]) { |
|
147 | + if ( $num === 2 ) { |
|
148 | + if ( $values[ 0 ][ 0 ] !== $values[ 1 ][ 0 ] ) { |
|
149 | 149 | // Both values are different, so it is already shorthand. |
150 | 150 | return; |
151 | 151 | } |
152 | - } else if ($values[0][0] !== $values[2][0] || $values[1][0] !== $values[3][0]) { |
|
152 | + } else if ( $values[ 0 ][ 0 ] !== $values[ 2 ][ 0 ] || $values[ 1 ][ 0 ] !== $values[ 3 ][ 0 ] ) { |
|
153 | 153 | // Can't shorthand this. |
154 | 154 | return; |
155 | 155 | } |
156 | 156 | |
157 | - if ($values[0][0] === $values[1][0]) { |
|
157 | + if ( $values[ 0 ][ 0 ] === $values[ 1 ][ 0 ] ) { |
|
158 | 158 | // All values are the same. |
159 | - $expected = $values[0][0]; |
|
159 | + $expected = $values[ 0 ][ 0 ]; |
|
160 | 160 | } else { |
161 | - $expected = $values[0][0].' '.$values[1][0]; |
|
161 | + $expected = $values[ 0 ][ 0 ] . ' ' . $values[ 1 ][ 0 ]; |
|
162 | 162 | } |
163 | 163 | |
164 | - $expected = preg_replace('/\s+/', ' ', trim($expected)); |
|
164 | + $expected = preg_replace( '/\s+/', ' ', trim( $expected ) ); |
|
165 | 165 | |
166 | 166 | $error = 'Size definitions must use shorthand if available; expected "%s" but found "%s"'; |
167 | 167 | $data = array( |
@@ -169,17 +169,17 @@ discard block |
||
169 | 169 | $content, |
170 | 170 | ); |
171 | 171 | |
172 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotUsed', $data); |
|
173 | - if ($fix === true) { |
|
172 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'NotUsed', $data ); |
|
173 | + if ( $fix === true ) { |
|
174 | 174 | $phpcsFile->fixer->beginChangeset(); |
175 | - if (substr($origContent, -10) === '!important') { |
|
175 | + if ( substr( $origContent, -10 ) === '!important' ) { |
|
176 | 176 | $expected .= ' !important'; |
177 | 177 | } |
178 | 178 | |
179 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
180 | - $phpcsFile->fixer->replaceToken($next, $expected); |
|
181 | - for ($next++; $next < $end; $next++) { |
|
182 | - $phpcsFile->fixer->replaceToken($next, ''); |
|
179 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 2 ), null, true ); |
|
180 | + $phpcsFile->fixer->replaceToken( $next, $expected ); |
|
181 | + for ( $next++; $next < $end; $next++ ) { |
|
182 | + $phpcsFile->fixer->replaceToken( $next, '' ); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $phpcsFile->fixer->endChangeset(); |
@@ -26,8 +26,7 @@ discard block |
||
26 | 26 | * @version Release: @package_version@ |
27 | 27 | * @link http://pear.php.net/package/PHP_CodeSniffer |
28 | 28 | */ |
29 | -class Squiz_Sniffs_CSS_ShorthandSizeSniff implements PHP_CodeSniffer_Sniff |
|
30 | -{ |
|
29 | +class Squiz_Sniffs_CSS_ShorthandSizeSniff implements PHP_CodeSniffer_Sniff { |
|
31 | 30 | |
32 | 31 | /** |
33 | 32 | * A list of tokenizers this sniff supports. |
@@ -57,8 +56,7 @@ discard block |
||
57 | 56 | * |
58 | 57 | * @return int[] |
59 | 58 | */ |
60 | - public function register() |
|
61 | - { |
|
59 | + public function register() { |
|
62 | 60 | return array(T_STYLE); |
63 | 61 | |
64 | 62 | }//end register() |
@@ -73,8 +71,7 @@ discard block |
||
73 | 71 | * |
74 | 72 | * @return void |
75 | 73 | */ |
76 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
77 | - { |
|
74 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
78 | 75 | $tokens = $phpcsFile->getTokens(); |
79 | 76 | |
80 | 77 | // Some styles look like shorthand but are not actually a set of 4 sizes. |