|
30 | 30 | class Squiz_Sniffs_Commenting_BlockCommentSniff implements PHP_CodeSniffer_Sniff
|
31 | 31 | {
|
32 | 32 |
|
| 33 | + /** |
| 34 | + * The --tab-width CLI value that is being used. |
| 35 | + * |
| 36 | + * @var int |
| 37 | + */ |
| 38 | + private $_tabWidth = null; |
| 39 | + |
33 | 40 |
|
34 | 41 | /**
|
35 | 42 | * Returns an array of tokens this test wants to listen for.
|
@@ -57,6 +64,16 @@ public function register()
|
57 | 64 | */
|
58 | 65 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
59 | 66 | {
|
| 67 | + if ($this->_tabWidth === null) { |
| 68 | + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
| 69 | + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { |
| 70 | + // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
| 71 | + $this->_tabWidth = 4; |
| 72 | + } else { |
| 73 | + $this->_tabWidth = $cliValues['tabWidth']; |
| 74 | + } |
| 75 | + } |
| 76 | + |
60 | 77 | $tokens = $phpcsFile->getTokens();
|
61 | 78 |
|
62 | 79 | // If it's an inline comment, return.
|
@@ -169,17 +186,26 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
169 | 186 | $error = 'Block comment text must start on a new line';
|
170 | 187 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoNewLine');
|
171 | 188 | if ($fix === true) {
|
| 189 | + $indent = ''; |
| 190 | + if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
| 191 | + if (isset($tokens[($stackPtr - 1)]['orig_content']) === true) { |
| 192 | + $indent = $tokens[($stackPtr - 1)]['orig_content']; |
| 193 | + } else { |
| 194 | + $indent = $tokens[($stackPtr - 1)]['content']; |
| 195 | + } |
| 196 | + } |
| 197 | + |
172 | 198 | $comment = preg_replace(
|
173 | 199 | '/^(\s*\/\*\*?)/',
|
174 |
| - '$1'.$phpcsFile->eolChar.' ', |
| 200 | + '$1'.$phpcsFile->eolChar.$indent, |
175 | 201 | $tokens[$stackPtr]['content'],
|
176 | 202 | 1
|
177 | 203 | );
|
178 | 204 | $phpcsFile->fixer->replaceToken($stackPtr, $comment);
|
179 | 205 | }
|
180 | 206 |
|
181 | 207 | return;
|
182 |
| - } |
| 208 | + }//end if |
183 | 209 |
|
184 | 210 | $starColumn = ($tokens[$stackPtr]['column'] + 3);
|
185 | 211 |
|
@@ -209,10 +235,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
209 | 235 | $error = 'First line of comment not aligned correctly; expected %s but found %s';
|
210 | 236 | $fix = $phpcsFile->addFixableError($error, $commentLines[1], 'FirstLineIndent', $data);
|
211 | 237 | if ($fix === true) {
|
212 |
| - $newContent = str_repeat(' ', $starColumn).ltrim($content); |
213 |
| - $phpcsFile->fixer->replaceToken($commentLines[1], $newContent); |
| 238 | + if (isset($tokens[$commentLines[1]]['orig_content']) === true |
| 239 | + && $tokens[$commentLines[1]]['orig_content'][0] === "\t" |
| 240 | + ) { |
| 241 | + // Line is indented using tabs. |
| 242 | + $padding = str_repeat("\t", floor($starColumn / $this->_tabWidth)); |
| 243 | + } else { |
| 244 | + $padding = str_repeat(' ', $starColumn); |
| 245 | + } |
| 246 | + |
| 247 | + $phpcsFile->fixer->replaceToken($commentLines[1], $padding.ltrim($content)); |
214 | 248 | }
|
215 |
| - } |
| 249 | + }//end if |
216 | 250 |
|
217 | 251 | if (preg_match('/^\p{Ll}/u', $commentText) === 1) {
|
218 | 252 | $error = 'Block comments must start with a capital letter';
|
@@ -252,10 +286,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
252 | 286 | $error = 'Comment line indented incorrectly; expected at least %s but found %s';
|
253 | 287 | $fix = $phpcsFile->addFixableError($error, $line, 'LineIndent', $data);
|
254 | 288 | if ($fix === true) {
|
255 |
| - $newContent = str_repeat(' ', $starColumn).ltrim($tokens[$line]['content']); |
256 |
| - $phpcsFile->fixer->replaceToken($line, $newContent); |
| 289 | + if (isset($tokens[$line]['orig_content']) === true |
| 290 | + && $tokens[$line]['orig_content'][0] === "\t" |
| 291 | + ) { |
| 292 | + // Line is indented using tabs. |
| 293 | + $padding = str_repeat("\t", floor($starColumn / $this->_tabWidth)); |
| 294 | + } else { |
| 295 | + $padding = str_repeat(' ', $starColumn); |
| 296 | + } |
| 297 | + |
| 298 | + $phpcsFile->fixer->replaceToken($line, $padding.ltrim($tokens[$line]['content'])); |
257 | 299 | }
|
258 |
| - } |
| 300 | + }//end if |
259 | 301 | }//end foreach
|
260 | 302 |
|
261 | 303 | // Finally, test the last line is correct.
|
|
0 commit comments