@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | /** |
357 | 357 | * Start recording actions for a changeset. |
358 | 358 | * |
359 | - * @return void |
|
359 | + * @return false|null |
|
360 | 360 | */ |
361 | 361 | public function beginChangeset() |
362 | 362 | { |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | /** |
384 | 384 | * Stop recording actions for a changeset, and apply logged changes. |
385 | 385 | * |
386 | - * @return boolean |
|
386 | + * @return false|null |
|
387 | 387 | */ |
388 | 388 | public function endChangeset() |
389 | 389 | { |
@@ -29,706 +29,706 @@ |
||
29 | 29 | class PHP_CodeSniffer_Fixer |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * Is the fixer enabled and fixing a file? |
|
34 | - * |
|
35 | - * Sniffs should check this value to ensure they are not |
|
36 | - * doing extra processing to prepare for a fix when fixing is |
|
37 | - * not required. |
|
38 | - * |
|
39 | - * @var boolean |
|
40 | - */ |
|
41 | - public $enabled = false; |
|
42 | - |
|
43 | - /** |
|
44 | - * The number of times we have looped over a file. |
|
45 | - * |
|
46 | - * @var int |
|
47 | - */ |
|
48 | - public $loops = 0; |
|
49 | - |
|
50 | - /** |
|
51 | - * The file being fixed. |
|
52 | - * |
|
53 | - * @var PHP_CodeSniffer_File |
|
54 | - */ |
|
55 | - private $_currentFile = null; |
|
56 | - |
|
57 | - /** |
|
58 | - * The list of tokens that make up the file contents. |
|
59 | - * |
|
60 | - * This is a simplified list which just contains the token content and nothing |
|
61 | - * else. This is the array that is updated as fixes are made, not the file's |
|
62 | - * token array. Imploding this array will give you the file content back. |
|
63 | - * |
|
64 | - * @var array(int => string) |
|
65 | - */ |
|
66 | - private $_tokens = array(); |
|
67 | - |
|
68 | - /** |
|
69 | - * A list of tokens that have already been fixed. |
|
70 | - * |
|
71 | - * We don't allow the same token to be fixed more than once each time |
|
72 | - * through a file as this can easily cause conflicts between sniffs. |
|
73 | - * |
|
74 | - * @var array(int) |
|
75 | - */ |
|
76 | - private $_fixedTokens = array(); |
|
77 | - |
|
78 | - /** |
|
79 | - * The last value of each fixed token. |
|
80 | - * |
|
81 | - * If a token is being "fixed" back to its last value, the fix is |
|
82 | - * probably conflicting with another. |
|
83 | - * |
|
84 | - * @var array(int => string) |
|
85 | - */ |
|
86 | - private $_oldTokenValues = array(); |
|
87 | - |
|
88 | - /** |
|
89 | - * A list of tokens that have been fixed during a changeset. |
|
90 | - * |
|
91 | - * All changes in changeset must be able to be applied, or else |
|
92 | - * the entire changeset is rejected. |
|
93 | - * |
|
94 | - * @var array() |
|
95 | - */ |
|
96 | - private $_changeset = array(); |
|
97 | - |
|
98 | - /** |
|
99 | - * Is there an open changeset. |
|
100 | - * |
|
101 | - * @var boolean |
|
102 | - */ |
|
103 | - private $_inChangeset = false; |
|
104 | - |
|
105 | - /** |
|
106 | - * Is the current fixing loop in conflict? |
|
107 | - * |
|
108 | - * @var boolean |
|
109 | - */ |
|
110 | - private $_inConflict = false; |
|
111 | - |
|
112 | - /** |
|
113 | - * The number of fixes that have been performed. |
|
114 | - * |
|
115 | - * @var int |
|
116 | - */ |
|
117 | - private $_numFixes = 0; |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * Starts fixing a new file. |
|
122 | - * |
|
123 | - * @param PHP_CodeSniffer_File $phpcsFile The file being fixed. |
|
124 | - * |
|
125 | - * @return void |
|
126 | - */ |
|
127 | - public function startFile($phpcsFile) |
|
128 | - { |
|
129 | - $this->_currentFile = $phpcsFile; |
|
130 | - $this->_numFixes = 0; |
|
131 | - $this->_fixedTokens = array(); |
|
132 | - |
|
133 | - $tokens = $phpcsFile->getTokens(); |
|
134 | - $this->_tokens = array(); |
|
135 | - foreach ($tokens as $index => $token) { |
|
136 | - if (isset($token['orig_content']) === true) { |
|
137 | - $this->_tokens[$index] = $token['orig_content']; |
|
138 | - } else { |
|
139 | - $this->_tokens[$index] = $token['content']; |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - }//end startFile() |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * Attempt to fix the file by processing it until no fixes are made. |
|
148 | - * |
|
149 | - * @return boolean |
|
150 | - */ |
|
151 | - public function fixFile() |
|
152 | - { |
|
153 | - $fixable = $this->_currentFile->getFixableCount(); |
|
154 | - if ($fixable === 0) { |
|
155 | - // Nothing to fix. |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - $stdin = false; |
|
160 | - $cliValues = $this->_currentFile->phpcs->cli->getCommandLineValues(); |
|
161 | - if (empty($cliValues['files']) === true) { |
|
162 | - $stdin = true; |
|
163 | - } |
|
164 | - |
|
165 | - $this->enabled = true; |
|
166 | - |
|
167 | - $this->loops = 0; |
|
168 | - while ($this->loops < 50) { |
|
169 | - ob_start(); |
|
170 | - |
|
171 | - // Only needed once file content has changed. |
|
172 | - $contents = $this->getContents(); |
|
173 | - |
|
174 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
175 | - @ob_end_clean(); |
|
176 | - echo '---START FILE CONTENT---'.PHP_EOL; |
|
177 | - $lines = explode($this->_currentFile->eolChar, $contents); |
|
178 | - $max = strlen(count($lines)); |
|
179 | - foreach ($lines as $lineNum => $line) { |
|
180 | - $lineNum++; |
|
181 | - echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
182 | - } |
|
183 | - |
|
184 | - echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
185 | - ob_start(); |
|
186 | - } |
|
187 | - |
|
188 | - $this->_inConflict = false; |
|
189 | - $this->_currentFile->refreshTokenListeners(); |
|
190 | - $this->_currentFile->start($contents); |
|
191 | - ob_end_clean(); |
|
192 | - |
|
193 | - $this->loops++; |
|
194 | - |
|
195 | - if (PHP_CODESNIFFER_CBF === true && $stdin === false) { |
|
196 | - echo "\r".str_repeat(' ', 80)."\r"; |
|
197 | - echo "\t=> Fixing file: $this->_numFixes/$fixable violations remaining [made $this->loops pass"; |
|
198 | - if ($this->loops > 1) { |
|
199 | - echo 'es'; |
|
200 | - } |
|
201 | - |
|
202 | - echo ']... '; |
|
203 | - } |
|
204 | - |
|
205 | - if ($this->_numFixes === 0 && $this->_inConflict === false) { |
|
206 | - // Nothing left to do. |
|
207 | - break; |
|
208 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
209 | - echo "\t* fixed $this->_numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
210 | - } |
|
211 | - }//end while |
|
212 | - |
|
213 | - $this->enabled = false; |
|
214 | - |
|
215 | - if ($this->_numFixes > 0) { |
|
216 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
217 | - @ob_end_clean(); |
|
218 | - echo "\t*** Reached maximum number of loops with $this->_numFixes violations left unfixed ***".PHP_EOL; |
|
219 | - ob_start(); |
|
220 | - } |
|
221 | - |
|
222 | - return false; |
|
223 | - } |
|
224 | - |
|
225 | - return true; |
|
226 | - |
|
227 | - }//end fixFile() |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Generates a text diff of the original file and the new content. |
|
232 | - * |
|
233 | - * @param string $filePath Optional file path to diff the file against. |
|
234 | - * If not specified, the original version of the |
|
235 | - * file will be used. |
|
236 | - * @param boolean $colors Print colored output or not. |
|
237 | - * |
|
238 | - * @return string |
|
239 | - */ |
|
240 | - public function generateDiff($filePath=null, $colors=true) |
|
241 | - { |
|
242 | - if ($filePath === null) { |
|
243 | - $filePath = $this->_currentFile->getFilename(); |
|
244 | - } |
|
245 | - |
|
246 | - $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
247 | - $filename = str_replace($cwd, '', $filePath); |
|
248 | - $contents = $this->getContents(); |
|
249 | - |
|
250 | - if (function_exists('sys_get_temp_dir') === true) { |
|
251 | - // This is needed for HHVM support, but only available from 5.2.1. |
|
252 | - $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
253 | - $fixedFile = fopen($tempName, 'w'); |
|
254 | - } else { |
|
255 | - $fixedFile = tmpfile(); |
|
256 | - $data = stream_get_meta_data($fixedFile); |
|
257 | - $tempName = $data['uri']; |
|
258 | - } |
|
259 | - |
|
260 | - fwrite($fixedFile, $contents); |
|
261 | - |
|
262 | - // We must use something like shell_exec() because whitespace at the end |
|
263 | - // of lines is critical to diff files. |
|
264 | - $cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\""; |
|
265 | - $diff = shell_exec($cmd); |
|
266 | - |
|
267 | - fclose($fixedFile); |
|
268 | - if (is_file($tempName) === true) { |
|
269 | - unlink($tempName); |
|
270 | - } |
|
271 | - |
|
272 | - if ($colors === false) { |
|
273 | - return $diff; |
|
274 | - } |
|
275 | - |
|
276 | - $diffLines = explode(PHP_EOL, $diff); |
|
277 | - if (count($diffLines) === 1) { |
|
278 | - // Seems to be required for cygwin. |
|
279 | - $diffLines = explode("\n", $diff); |
|
280 | - } |
|
281 | - |
|
282 | - $diff = array(); |
|
283 | - foreach ($diffLines as $line) { |
|
284 | - if (isset($line[0]) === true) { |
|
285 | - switch ($line[0]) { |
|
286 | - case '-': |
|
287 | - $diff[] = "\033[31m$line\033[0m"; |
|
288 | - break; |
|
289 | - case '+': |
|
290 | - $diff[] = "\033[32m$line\033[0m"; |
|
291 | - break; |
|
292 | - default: |
|
293 | - $diff[] = $line; |
|
294 | - } |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - $diff = implode(PHP_EOL, $diff); |
|
299 | - |
|
300 | - return $diff; |
|
301 | - |
|
302 | - }//end generateDiff() |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Get a count of fixes that have been performed on the file. |
|
307 | - * |
|
308 | - * This value is reset every time a new file is started, or an existing |
|
309 | - * file is restarted. |
|
310 | - * |
|
311 | - * @return int |
|
312 | - */ |
|
313 | - public function getFixCount() |
|
314 | - { |
|
315 | - return $this->_numFixes; |
|
316 | - |
|
317 | - }//end getFixCount() |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * Get the current content of the file, as a string. |
|
322 | - * |
|
323 | - * @return string |
|
324 | - */ |
|
325 | - public function getContents() |
|
326 | - { |
|
327 | - $contents = implode($this->_tokens); |
|
328 | - return $contents; |
|
329 | - |
|
330 | - }//end getContents() |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * Get the current fixed content of a token. |
|
335 | - * |
|
336 | - * This function takes changesets into account so should be used |
|
337 | - * instead of directly accessing the token array. |
|
338 | - * |
|
339 | - * @param int $stackPtr The position of the token in the token stack. |
|
340 | - * |
|
341 | - * @return string |
|
342 | - */ |
|
343 | - public function getTokenContent($stackPtr) |
|
344 | - { |
|
345 | - if ($this->_inChangeset === true |
|
346 | - && isset($this->_changeset[$stackPtr]) === true |
|
347 | - ) { |
|
348 | - return $this->_changeset[$stackPtr]; |
|
349 | - } else { |
|
350 | - return $this->_tokens[$stackPtr]; |
|
351 | - } |
|
352 | - |
|
353 | - }//end getTokenContent() |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Start recording actions for a changeset. |
|
358 | - * |
|
359 | - * @return void |
|
360 | - */ |
|
361 | - public function beginChangeset() |
|
362 | - { |
|
363 | - if ($this->_inConflict === true) { |
|
364 | - return false; |
|
365 | - } |
|
366 | - |
|
367 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
368 | - $bt = debug_backtrace(); |
|
369 | - $sniff = $bt[1]['class']; |
|
370 | - $line = $bt[0]['line']; |
|
371 | - |
|
372 | - @ob_end_clean(); |
|
373 | - echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
|
374 | - ob_start(); |
|
375 | - } |
|
376 | - |
|
377 | - $this->_changeset = array(); |
|
378 | - $this->_inChangeset = true; |
|
379 | - |
|
380 | - }//end beginChangeset() |
|
381 | - |
|
382 | - |
|
383 | - /** |
|
384 | - * Stop recording actions for a changeset, and apply logged changes. |
|
385 | - * |
|
386 | - * @return boolean |
|
387 | - */ |
|
388 | - public function endChangeset() |
|
389 | - { |
|
390 | - if ($this->_inConflict === true) { |
|
391 | - return false; |
|
392 | - } |
|
393 | - |
|
394 | - $this->_inChangeset = false; |
|
395 | - |
|
396 | - $success = true; |
|
397 | - $applied = array(); |
|
398 | - foreach ($this->_changeset as $stackPtr => $content) { |
|
399 | - $success = $this->replaceToken($stackPtr, $content); |
|
400 | - if ($success === false) { |
|
401 | - break; |
|
402 | - } else { |
|
403 | - $applied[] = $stackPtr; |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - if ($success === false) { |
|
408 | - // Rolling back all changes. |
|
409 | - foreach ($applied as $stackPtr) { |
|
410 | - $this->revertToken($stackPtr); |
|
411 | - } |
|
412 | - |
|
413 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
414 | - @ob_end_clean(); |
|
415 | - echo "\t=> Changeset failed to apply".PHP_EOL; |
|
416 | - ob_start(); |
|
417 | - } |
|
418 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
419 | - $fixes = count($this->_changeset); |
|
420 | - @ob_end_clean(); |
|
421 | - echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
422 | - ob_start(); |
|
423 | - } |
|
424 | - |
|
425 | - $this->_changeset = array(); |
|
426 | - |
|
427 | - }//end endChangeset() |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * Stop recording actions for a changeset, and discard logged changes. |
|
432 | - * |
|
433 | - * @return void |
|
434 | - */ |
|
435 | - public function rollbackChangeset() |
|
436 | - { |
|
437 | - $this->_inChangeset = false; |
|
438 | - $this->_inConflict = false; |
|
439 | - |
|
440 | - if (empty($this->_changeset) === false) { |
|
441 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
442 | - $bt = debug_backtrace(); |
|
443 | - if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
444 | - $sniff = $bt[2]['class']; |
|
445 | - $line = $bt[1]['line']; |
|
446 | - } else { |
|
447 | - $sniff = $bt[1]['class']; |
|
448 | - $line = $bt[0]['line']; |
|
449 | - } |
|
450 | - |
|
451 | - $numChanges = count($this->_changeset); |
|
452 | - |
|
453 | - @ob_end_clean(); |
|
454 | - echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
455 | - echo "\t=> Changeset rolled back".PHP_EOL; |
|
456 | - ob_start(); |
|
457 | - } |
|
458 | - |
|
459 | - $this->_changeset = array(); |
|
460 | - }//end if |
|
461 | - |
|
462 | - }//end rollbackChangeset() |
|
463 | - |
|
464 | - |
|
465 | - /** |
|
466 | - * Replace the entire contents of a token. |
|
467 | - * |
|
468 | - * @param int $stackPtr The position of the token in the token stack. |
|
469 | - * @param string $content The new content of the token. |
|
470 | - * |
|
471 | - * @return bool If the change was accepted. |
|
472 | - */ |
|
473 | - public function replaceToken($stackPtr, $content) |
|
474 | - { |
|
475 | - if ($this->_inConflict === true) { |
|
476 | - return false; |
|
477 | - } |
|
478 | - |
|
479 | - if ($this->_inChangeset === false |
|
480 | - && isset($this->_fixedTokens[$stackPtr]) === true |
|
481 | - ) { |
|
482 | - $indent = "\t"; |
|
483 | - if (empty($this->_changeset) === false) { |
|
484 | - $indent .= "\t"; |
|
485 | - } |
|
486 | - |
|
487 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
488 | - @ob_end_clean(); |
|
489 | - echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
490 | - ob_start(); |
|
491 | - } |
|
492 | - |
|
493 | - return false; |
|
494 | - } |
|
495 | - |
|
496 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
497 | - $bt = debug_backtrace(); |
|
498 | - if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
499 | - $sniff = $bt[2]['class']; |
|
500 | - $line = $bt[1]['line']; |
|
501 | - } else { |
|
502 | - $sniff = $bt[1]['class']; |
|
503 | - $line = $bt[0]['line']; |
|
504 | - } |
|
505 | - |
|
506 | - $tokens = $this->_currentFile->getTokens(); |
|
507 | - $type = $tokens[$stackPtr]['type']; |
|
508 | - $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
|
509 | - $newContent = PHP_CodeSniffer::prepareForOutput($content); |
|
510 | - if (trim($this->_tokens[$stackPtr]) === '' && isset($this->_tokens[($stackPtr + 1)]) === true) { |
|
511 | - // Add some context for whitespace only changes. |
|
512 | - $append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
|
513 | - $oldContent .= $append; |
|
514 | - $newContent .= $append; |
|
515 | - } |
|
516 | - }//end if |
|
517 | - |
|
518 | - if ($this->_inChangeset === true) { |
|
519 | - $this->_changeset[$stackPtr] = $content; |
|
520 | - |
|
521 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
522 | - @ob_end_clean(); |
|
523 | - echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
524 | - ob_start(); |
|
525 | - } |
|
526 | - |
|
527 | - return true; |
|
528 | - } |
|
529 | - |
|
530 | - if (isset($this->_oldTokenValues[$stackPtr]) === false) { |
|
531 | - $this->_oldTokenValues[$stackPtr] = array( |
|
532 | - 'curr' => $content, |
|
533 | - 'prev' => $this->_tokens[$stackPtr], |
|
534 | - 'loop' => $this->loops, |
|
535 | - ); |
|
536 | - } else { |
|
537 | - if ($this->_oldTokenValues[$stackPtr]['prev'] === $content |
|
538 | - && $this->_oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
539 | - ) { |
|
540 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
541 | - $indent = "\t"; |
|
542 | - if (empty($this->_changeset) === false) { |
|
543 | - $indent .= "\t"; |
|
544 | - } |
|
545 | - |
|
546 | - $loop = $this->_oldTokenValues[$stackPtr]['loop']; |
|
547 | - |
|
548 | - @ob_end_clean(); |
|
549 | - echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
550 | - echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
551 | - } |
|
552 | - |
|
553 | - if ($this->_oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
554 | - $this->_inConflict = true; |
|
555 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
556 | - echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
557 | - } |
|
558 | - } |
|
559 | - |
|
560 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
561 | - ob_start(); |
|
562 | - } |
|
563 | - |
|
564 | - return false; |
|
565 | - }//end if |
|
566 | - |
|
567 | - $this->_oldTokenValues[$stackPtr]['prev'] = $this->_oldTokenValues[$stackPtr]['curr']; |
|
568 | - $this->_oldTokenValues[$stackPtr]['curr'] = $content; |
|
569 | - $this->_oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
570 | - }//end if |
|
571 | - |
|
572 | - $this->_fixedTokens[$stackPtr] = $this->_tokens[$stackPtr]; |
|
573 | - $this->_tokens[$stackPtr] = $content; |
|
574 | - $this->_numFixes++; |
|
575 | - |
|
576 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
577 | - $indent = "\t"; |
|
578 | - if (empty($this->_changeset) === false) { |
|
579 | - $indent .= "\tA: "; |
|
580 | - } |
|
581 | - |
|
582 | - @ob_end_clean(); |
|
583 | - echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
584 | - ob_start(); |
|
585 | - } |
|
586 | - |
|
587 | - return true; |
|
588 | - |
|
589 | - }//end replaceToken() |
|
590 | - |
|
591 | - |
|
592 | - /** |
|
593 | - * Reverts the previous fix made to a token. |
|
594 | - * |
|
595 | - * @param int $stackPtr The position of the token in the token stack. |
|
596 | - * |
|
597 | - * @return bool If a change was reverted. |
|
598 | - */ |
|
599 | - public function revertToken($stackPtr) |
|
600 | - { |
|
601 | - if (isset($this->_fixedTokens[$stackPtr]) === false) { |
|
602 | - return false; |
|
603 | - } |
|
604 | - |
|
605 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
606 | - $bt = debug_backtrace(); |
|
607 | - if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
608 | - $sniff = $bt[2]['class']; |
|
609 | - $line = $bt[1]['line']; |
|
610 | - } else { |
|
611 | - $sniff = $bt[1]['class']; |
|
612 | - $line = $bt[0]['line']; |
|
613 | - } |
|
614 | - |
|
615 | - $tokens = $this->_currentFile->getTokens(); |
|
616 | - $type = $tokens[$stackPtr]['type']; |
|
617 | - $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
|
618 | - $newContent = PHP_CodeSniffer::prepareForOutput($this->_fixedTokens[$stackPtr]); |
|
619 | - if (trim($this->_tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
620 | - // Add some context for whitespace only changes. |
|
621 | - $append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
|
622 | - $oldContent .= $append; |
|
623 | - $newContent .= $append; |
|
624 | - } |
|
625 | - }//end if |
|
626 | - |
|
627 | - $this->_tokens[$stackPtr] = $this->_fixedTokens[$stackPtr]; |
|
628 | - unset($this->_fixedTokens[$stackPtr]); |
|
629 | - $this->_numFixes--; |
|
630 | - |
|
631 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
632 | - $indent = "\t"; |
|
633 | - if (empty($this->_changeset) === false) { |
|
634 | - $indent .= "\tR: "; |
|
635 | - } |
|
636 | - |
|
637 | - @ob_end_clean(); |
|
638 | - echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
639 | - ob_start(); |
|
640 | - } |
|
641 | - |
|
642 | - return true; |
|
643 | - |
|
644 | - }//end revertToken() |
|
645 | - |
|
646 | - |
|
647 | - /** |
|
648 | - * Replace the content of a token with a part of its current content. |
|
649 | - * |
|
650 | - * @param int $stackPtr The position of the token in the token stack. |
|
651 | - * @param int $start The first character to keep. |
|
652 | - * @param int $length The number of chacters to keep. If NULL, the content of |
|
653 | - * the token from $start to the end of the content is kept. |
|
654 | - * |
|
655 | - * @return bool If the change was accepted. |
|
656 | - */ |
|
657 | - public function substrToken($stackPtr, $start, $length=null) |
|
658 | - { |
|
659 | - $current = $this->getTokenContent($stackPtr); |
|
660 | - |
|
661 | - if ($length === null) { |
|
662 | - $newContent = substr($current, $start); |
|
663 | - } else { |
|
664 | - $newContent = substr($current, $start, $length); |
|
665 | - } |
|
666 | - |
|
667 | - return $this->replaceToken($stackPtr, $newContent); |
|
668 | - |
|
669 | - }//end substrToken() |
|
670 | - |
|
671 | - |
|
672 | - /** |
|
673 | - * Adds a newline to end of a token's content. |
|
674 | - * |
|
675 | - * @param int $stackPtr The position of the token in the token stack. |
|
676 | - * |
|
677 | - * @return bool If the change was accepted. |
|
678 | - */ |
|
679 | - public function addNewline($stackPtr) |
|
680 | - { |
|
681 | - $current = $this->getTokenContent($stackPtr); |
|
682 | - return $this->replaceToken($stackPtr, $current.$this->_currentFile->eolChar); |
|
683 | - |
|
684 | - }//end addNewline() |
|
685 | - |
|
686 | - |
|
687 | - /** |
|
688 | - * Adds a newline to the start of a token's content. |
|
689 | - * |
|
690 | - * @param int $stackPtr The position of the token in the token stack. |
|
691 | - * |
|
692 | - * @return bool If the change was accepted. |
|
693 | - */ |
|
694 | - public function addNewlineBefore($stackPtr) |
|
695 | - { |
|
696 | - $current = $this->getTokenContent($stackPtr); |
|
697 | - return $this->replaceToken($stackPtr, $this->_currentFile->eolChar.$current); |
|
698 | - |
|
699 | - }//end addNewlineBefore() |
|
700 | - |
|
701 | - |
|
702 | - /** |
|
703 | - * Adds content to the end of a token's current content. |
|
704 | - * |
|
705 | - * @param int $stackPtr The position of the token in the token stack. |
|
706 | - * @param string $content The content to add. |
|
707 | - * |
|
708 | - * @return bool If the change was accepted. |
|
709 | - */ |
|
710 | - public function addContent($stackPtr, $content) |
|
711 | - { |
|
712 | - $current = $this->getTokenContent($stackPtr); |
|
713 | - return $this->replaceToken($stackPtr, $current.$content); |
|
714 | - |
|
715 | - }//end addContent() |
|
716 | - |
|
717 | - |
|
718 | - /** |
|
719 | - * Adds content to the start of a token's current content. |
|
720 | - * |
|
721 | - * @param int $stackPtr The position of the token in the token stack. |
|
722 | - * @param string $content The content to add. |
|
723 | - * |
|
724 | - * @return bool If the change was accepted. |
|
725 | - */ |
|
726 | - public function addContentBefore($stackPtr, $content) |
|
727 | - { |
|
728 | - $current = $this->getTokenContent($stackPtr); |
|
729 | - return $this->replaceToken($stackPtr, $content.$current); |
|
730 | - |
|
731 | - }//end addContentBefore() |
|
32 | + /** |
|
33 | + * Is the fixer enabled and fixing a file? |
|
34 | + * |
|
35 | + * Sniffs should check this value to ensure they are not |
|
36 | + * doing extra processing to prepare for a fix when fixing is |
|
37 | + * not required. |
|
38 | + * |
|
39 | + * @var boolean |
|
40 | + */ |
|
41 | + public $enabled = false; |
|
42 | + |
|
43 | + /** |
|
44 | + * The number of times we have looped over a file. |
|
45 | + * |
|
46 | + * @var int |
|
47 | + */ |
|
48 | + public $loops = 0; |
|
49 | + |
|
50 | + /** |
|
51 | + * The file being fixed. |
|
52 | + * |
|
53 | + * @var PHP_CodeSniffer_File |
|
54 | + */ |
|
55 | + private $_currentFile = null; |
|
56 | + |
|
57 | + /** |
|
58 | + * The list of tokens that make up the file contents. |
|
59 | + * |
|
60 | + * This is a simplified list which just contains the token content and nothing |
|
61 | + * else. This is the array that is updated as fixes are made, not the file's |
|
62 | + * token array. Imploding this array will give you the file content back. |
|
63 | + * |
|
64 | + * @var array(int => string) |
|
65 | + */ |
|
66 | + private $_tokens = array(); |
|
67 | + |
|
68 | + /** |
|
69 | + * A list of tokens that have already been fixed. |
|
70 | + * |
|
71 | + * We don't allow the same token to be fixed more than once each time |
|
72 | + * through a file as this can easily cause conflicts between sniffs. |
|
73 | + * |
|
74 | + * @var array(int) |
|
75 | + */ |
|
76 | + private $_fixedTokens = array(); |
|
77 | + |
|
78 | + /** |
|
79 | + * The last value of each fixed token. |
|
80 | + * |
|
81 | + * If a token is being "fixed" back to its last value, the fix is |
|
82 | + * probably conflicting with another. |
|
83 | + * |
|
84 | + * @var array(int => string) |
|
85 | + */ |
|
86 | + private $_oldTokenValues = array(); |
|
87 | + |
|
88 | + /** |
|
89 | + * A list of tokens that have been fixed during a changeset. |
|
90 | + * |
|
91 | + * All changes in changeset must be able to be applied, or else |
|
92 | + * the entire changeset is rejected. |
|
93 | + * |
|
94 | + * @var array() |
|
95 | + */ |
|
96 | + private $_changeset = array(); |
|
97 | + |
|
98 | + /** |
|
99 | + * Is there an open changeset. |
|
100 | + * |
|
101 | + * @var boolean |
|
102 | + */ |
|
103 | + private $_inChangeset = false; |
|
104 | + |
|
105 | + /** |
|
106 | + * Is the current fixing loop in conflict? |
|
107 | + * |
|
108 | + * @var boolean |
|
109 | + */ |
|
110 | + private $_inConflict = false; |
|
111 | + |
|
112 | + /** |
|
113 | + * The number of fixes that have been performed. |
|
114 | + * |
|
115 | + * @var int |
|
116 | + */ |
|
117 | + private $_numFixes = 0; |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * Starts fixing a new file. |
|
122 | + * |
|
123 | + * @param PHP_CodeSniffer_File $phpcsFile The file being fixed. |
|
124 | + * |
|
125 | + * @return void |
|
126 | + */ |
|
127 | + public function startFile($phpcsFile) |
|
128 | + { |
|
129 | + $this->_currentFile = $phpcsFile; |
|
130 | + $this->_numFixes = 0; |
|
131 | + $this->_fixedTokens = array(); |
|
132 | + |
|
133 | + $tokens = $phpcsFile->getTokens(); |
|
134 | + $this->_tokens = array(); |
|
135 | + foreach ($tokens as $index => $token) { |
|
136 | + if (isset($token['orig_content']) === true) { |
|
137 | + $this->_tokens[$index] = $token['orig_content']; |
|
138 | + } else { |
|
139 | + $this->_tokens[$index] = $token['content']; |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + }//end startFile() |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * Attempt to fix the file by processing it until no fixes are made. |
|
148 | + * |
|
149 | + * @return boolean |
|
150 | + */ |
|
151 | + public function fixFile() |
|
152 | + { |
|
153 | + $fixable = $this->_currentFile->getFixableCount(); |
|
154 | + if ($fixable === 0) { |
|
155 | + // Nothing to fix. |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + $stdin = false; |
|
160 | + $cliValues = $this->_currentFile->phpcs->cli->getCommandLineValues(); |
|
161 | + if (empty($cliValues['files']) === true) { |
|
162 | + $stdin = true; |
|
163 | + } |
|
164 | + |
|
165 | + $this->enabled = true; |
|
166 | + |
|
167 | + $this->loops = 0; |
|
168 | + while ($this->loops < 50) { |
|
169 | + ob_start(); |
|
170 | + |
|
171 | + // Only needed once file content has changed. |
|
172 | + $contents = $this->getContents(); |
|
173 | + |
|
174 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
175 | + @ob_end_clean(); |
|
176 | + echo '---START FILE CONTENT---'.PHP_EOL; |
|
177 | + $lines = explode($this->_currentFile->eolChar, $contents); |
|
178 | + $max = strlen(count($lines)); |
|
179 | + foreach ($lines as $lineNum => $line) { |
|
180 | + $lineNum++; |
|
181 | + echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
182 | + } |
|
183 | + |
|
184 | + echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
185 | + ob_start(); |
|
186 | + } |
|
187 | + |
|
188 | + $this->_inConflict = false; |
|
189 | + $this->_currentFile->refreshTokenListeners(); |
|
190 | + $this->_currentFile->start($contents); |
|
191 | + ob_end_clean(); |
|
192 | + |
|
193 | + $this->loops++; |
|
194 | + |
|
195 | + if (PHP_CODESNIFFER_CBF === true && $stdin === false) { |
|
196 | + echo "\r".str_repeat(' ', 80)."\r"; |
|
197 | + echo "\t=> Fixing file: $this->_numFixes/$fixable violations remaining [made $this->loops pass"; |
|
198 | + if ($this->loops > 1) { |
|
199 | + echo 'es'; |
|
200 | + } |
|
201 | + |
|
202 | + echo ']... '; |
|
203 | + } |
|
204 | + |
|
205 | + if ($this->_numFixes === 0 && $this->_inConflict === false) { |
|
206 | + // Nothing left to do. |
|
207 | + break; |
|
208 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
209 | + echo "\t* fixed $this->_numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
210 | + } |
|
211 | + }//end while |
|
212 | + |
|
213 | + $this->enabled = false; |
|
214 | + |
|
215 | + if ($this->_numFixes > 0) { |
|
216 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
217 | + @ob_end_clean(); |
|
218 | + echo "\t*** Reached maximum number of loops with $this->_numFixes violations left unfixed ***".PHP_EOL; |
|
219 | + ob_start(); |
|
220 | + } |
|
221 | + |
|
222 | + return false; |
|
223 | + } |
|
224 | + |
|
225 | + return true; |
|
226 | + |
|
227 | + }//end fixFile() |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Generates a text diff of the original file and the new content. |
|
232 | + * |
|
233 | + * @param string $filePath Optional file path to diff the file against. |
|
234 | + * If not specified, the original version of the |
|
235 | + * file will be used. |
|
236 | + * @param boolean $colors Print colored output or not. |
|
237 | + * |
|
238 | + * @return string |
|
239 | + */ |
|
240 | + public function generateDiff($filePath=null, $colors=true) |
|
241 | + { |
|
242 | + if ($filePath === null) { |
|
243 | + $filePath = $this->_currentFile->getFilename(); |
|
244 | + } |
|
245 | + |
|
246 | + $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
247 | + $filename = str_replace($cwd, '', $filePath); |
|
248 | + $contents = $this->getContents(); |
|
249 | + |
|
250 | + if (function_exists('sys_get_temp_dir') === true) { |
|
251 | + // This is needed for HHVM support, but only available from 5.2.1. |
|
252 | + $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
253 | + $fixedFile = fopen($tempName, 'w'); |
|
254 | + } else { |
|
255 | + $fixedFile = tmpfile(); |
|
256 | + $data = stream_get_meta_data($fixedFile); |
|
257 | + $tempName = $data['uri']; |
|
258 | + } |
|
259 | + |
|
260 | + fwrite($fixedFile, $contents); |
|
261 | + |
|
262 | + // We must use something like shell_exec() because whitespace at the end |
|
263 | + // of lines is critical to diff files. |
|
264 | + $cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\""; |
|
265 | + $diff = shell_exec($cmd); |
|
266 | + |
|
267 | + fclose($fixedFile); |
|
268 | + if (is_file($tempName) === true) { |
|
269 | + unlink($tempName); |
|
270 | + } |
|
271 | + |
|
272 | + if ($colors === false) { |
|
273 | + return $diff; |
|
274 | + } |
|
275 | + |
|
276 | + $diffLines = explode(PHP_EOL, $diff); |
|
277 | + if (count($diffLines) === 1) { |
|
278 | + // Seems to be required for cygwin. |
|
279 | + $diffLines = explode("\n", $diff); |
|
280 | + } |
|
281 | + |
|
282 | + $diff = array(); |
|
283 | + foreach ($diffLines as $line) { |
|
284 | + if (isset($line[0]) === true) { |
|
285 | + switch ($line[0]) { |
|
286 | + case '-': |
|
287 | + $diff[] = "\033[31m$line\033[0m"; |
|
288 | + break; |
|
289 | + case '+': |
|
290 | + $diff[] = "\033[32m$line\033[0m"; |
|
291 | + break; |
|
292 | + default: |
|
293 | + $diff[] = $line; |
|
294 | + } |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + $diff = implode(PHP_EOL, $diff); |
|
299 | + |
|
300 | + return $diff; |
|
301 | + |
|
302 | + }//end generateDiff() |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Get a count of fixes that have been performed on the file. |
|
307 | + * |
|
308 | + * This value is reset every time a new file is started, or an existing |
|
309 | + * file is restarted. |
|
310 | + * |
|
311 | + * @return int |
|
312 | + */ |
|
313 | + public function getFixCount() |
|
314 | + { |
|
315 | + return $this->_numFixes; |
|
316 | + |
|
317 | + }//end getFixCount() |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * Get the current content of the file, as a string. |
|
322 | + * |
|
323 | + * @return string |
|
324 | + */ |
|
325 | + public function getContents() |
|
326 | + { |
|
327 | + $contents = implode($this->_tokens); |
|
328 | + return $contents; |
|
329 | + |
|
330 | + }//end getContents() |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * Get the current fixed content of a token. |
|
335 | + * |
|
336 | + * This function takes changesets into account so should be used |
|
337 | + * instead of directly accessing the token array. |
|
338 | + * |
|
339 | + * @param int $stackPtr The position of the token in the token stack. |
|
340 | + * |
|
341 | + * @return string |
|
342 | + */ |
|
343 | + public function getTokenContent($stackPtr) |
|
344 | + { |
|
345 | + if ($this->_inChangeset === true |
|
346 | + && isset($this->_changeset[$stackPtr]) === true |
|
347 | + ) { |
|
348 | + return $this->_changeset[$stackPtr]; |
|
349 | + } else { |
|
350 | + return $this->_tokens[$stackPtr]; |
|
351 | + } |
|
352 | + |
|
353 | + }//end getTokenContent() |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Start recording actions for a changeset. |
|
358 | + * |
|
359 | + * @return void |
|
360 | + */ |
|
361 | + public function beginChangeset() |
|
362 | + { |
|
363 | + if ($this->_inConflict === true) { |
|
364 | + return false; |
|
365 | + } |
|
366 | + |
|
367 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
368 | + $bt = debug_backtrace(); |
|
369 | + $sniff = $bt[1]['class']; |
|
370 | + $line = $bt[0]['line']; |
|
371 | + |
|
372 | + @ob_end_clean(); |
|
373 | + echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
|
374 | + ob_start(); |
|
375 | + } |
|
376 | + |
|
377 | + $this->_changeset = array(); |
|
378 | + $this->_inChangeset = true; |
|
379 | + |
|
380 | + }//end beginChangeset() |
|
381 | + |
|
382 | + |
|
383 | + /** |
|
384 | + * Stop recording actions for a changeset, and apply logged changes. |
|
385 | + * |
|
386 | + * @return boolean |
|
387 | + */ |
|
388 | + public function endChangeset() |
|
389 | + { |
|
390 | + if ($this->_inConflict === true) { |
|
391 | + return false; |
|
392 | + } |
|
393 | + |
|
394 | + $this->_inChangeset = false; |
|
395 | + |
|
396 | + $success = true; |
|
397 | + $applied = array(); |
|
398 | + foreach ($this->_changeset as $stackPtr => $content) { |
|
399 | + $success = $this->replaceToken($stackPtr, $content); |
|
400 | + if ($success === false) { |
|
401 | + break; |
|
402 | + } else { |
|
403 | + $applied[] = $stackPtr; |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + if ($success === false) { |
|
408 | + // Rolling back all changes. |
|
409 | + foreach ($applied as $stackPtr) { |
|
410 | + $this->revertToken($stackPtr); |
|
411 | + } |
|
412 | + |
|
413 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
414 | + @ob_end_clean(); |
|
415 | + echo "\t=> Changeset failed to apply".PHP_EOL; |
|
416 | + ob_start(); |
|
417 | + } |
|
418 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
419 | + $fixes = count($this->_changeset); |
|
420 | + @ob_end_clean(); |
|
421 | + echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
422 | + ob_start(); |
|
423 | + } |
|
424 | + |
|
425 | + $this->_changeset = array(); |
|
426 | + |
|
427 | + }//end endChangeset() |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * Stop recording actions for a changeset, and discard logged changes. |
|
432 | + * |
|
433 | + * @return void |
|
434 | + */ |
|
435 | + public function rollbackChangeset() |
|
436 | + { |
|
437 | + $this->_inChangeset = false; |
|
438 | + $this->_inConflict = false; |
|
439 | + |
|
440 | + if (empty($this->_changeset) === false) { |
|
441 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
442 | + $bt = debug_backtrace(); |
|
443 | + if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
444 | + $sniff = $bt[2]['class']; |
|
445 | + $line = $bt[1]['line']; |
|
446 | + } else { |
|
447 | + $sniff = $bt[1]['class']; |
|
448 | + $line = $bt[0]['line']; |
|
449 | + } |
|
450 | + |
|
451 | + $numChanges = count($this->_changeset); |
|
452 | + |
|
453 | + @ob_end_clean(); |
|
454 | + echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
455 | + echo "\t=> Changeset rolled back".PHP_EOL; |
|
456 | + ob_start(); |
|
457 | + } |
|
458 | + |
|
459 | + $this->_changeset = array(); |
|
460 | + }//end if |
|
461 | + |
|
462 | + }//end rollbackChangeset() |
|
463 | + |
|
464 | + |
|
465 | + /** |
|
466 | + * Replace the entire contents of a token. |
|
467 | + * |
|
468 | + * @param int $stackPtr The position of the token in the token stack. |
|
469 | + * @param string $content The new content of the token. |
|
470 | + * |
|
471 | + * @return bool If the change was accepted. |
|
472 | + */ |
|
473 | + public function replaceToken($stackPtr, $content) |
|
474 | + { |
|
475 | + if ($this->_inConflict === true) { |
|
476 | + return false; |
|
477 | + } |
|
478 | + |
|
479 | + if ($this->_inChangeset === false |
|
480 | + && isset($this->_fixedTokens[$stackPtr]) === true |
|
481 | + ) { |
|
482 | + $indent = "\t"; |
|
483 | + if (empty($this->_changeset) === false) { |
|
484 | + $indent .= "\t"; |
|
485 | + } |
|
486 | + |
|
487 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
488 | + @ob_end_clean(); |
|
489 | + echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
490 | + ob_start(); |
|
491 | + } |
|
492 | + |
|
493 | + return false; |
|
494 | + } |
|
495 | + |
|
496 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
497 | + $bt = debug_backtrace(); |
|
498 | + if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
499 | + $sniff = $bt[2]['class']; |
|
500 | + $line = $bt[1]['line']; |
|
501 | + } else { |
|
502 | + $sniff = $bt[1]['class']; |
|
503 | + $line = $bt[0]['line']; |
|
504 | + } |
|
505 | + |
|
506 | + $tokens = $this->_currentFile->getTokens(); |
|
507 | + $type = $tokens[$stackPtr]['type']; |
|
508 | + $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
|
509 | + $newContent = PHP_CodeSniffer::prepareForOutput($content); |
|
510 | + if (trim($this->_tokens[$stackPtr]) === '' && isset($this->_tokens[($stackPtr + 1)]) === true) { |
|
511 | + // Add some context for whitespace only changes. |
|
512 | + $append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
|
513 | + $oldContent .= $append; |
|
514 | + $newContent .= $append; |
|
515 | + } |
|
516 | + }//end if |
|
517 | + |
|
518 | + if ($this->_inChangeset === true) { |
|
519 | + $this->_changeset[$stackPtr] = $content; |
|
520 | + |
|
521 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
522 | + @ob_end_clean(); |
|
523 | + echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
524 | + ob_start(); |
|
525 | + } |
|
526 | + |
|
527 | + return true; |
|
528 | + } |
|
529 | + |
|
530 | + if (isset($this->_oldTokenValues[$stackPtr]) === false) { |
|
531 | + $this->_oldTokenValues[$stackPtr] = array( |
|
532 | + 'curr' => $content, |
|
533 | + 'prev' => $this->_tokens[$stackPtr], |
|
534 | + 'loop' => $this->loops, |
|
535 | + ); |
|
536 | + } else { |
|
537 | + if ($this->_oldTokenValues[$stackPtr]['prev'] === $content |
|
538 | + && $this->_oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
539 | + ) { |
|
540 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
541 | + $indent = "\t"; |
|
542 | + if (empty($this->_changeset) === false) { |
|
543 | + $indent .= "\t"; |
|
544 | + } |
|
545 | + |
|
546 | + $loop = $this->_oldTokenValues[$stackPtr]['loop']; |
|
547 | + |
|
548 | + @ob_end_clean(); |
|
549 | + echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
550 | + echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
551 | + } |
|
552 | + |
|
553 | + if ($this->_oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
554 | + $this->_inConflict = true; |
|
555 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
556 | + echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
557 | + } |
|
558 | + } |
|
559 | + |
|
560 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
561 | + ob_start(); |
|
562 | + } |
|
563 | + |
|
564 | + return false; |
|
565 | + }//end if |
|
566 | + |
|
567 | + $this->_oldTokenValues[$stackPtr]['prev'] = $this->_oldTokenValues[$stackPtr]['curr']; |
|
568 | + $this->_oldTokenValues[$stackPtr]['curr'] = $content; |
|
569 | + $this->_oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
570 | + }//end if |
|
571 | + |
|
572 | + $this->_fixedTokens[$stackPtr] = $this->_tokens[$stackPtr]; |
|
573 | + $this->_tokens[$stackPtr] = $content; |
|
574 | + $this->_numFixes++; |
|
575 | + |
|
576 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
577 | + $indent = "\t"; |
|
578 | + if (empty($this->_changeset) === false) { |
|
579 | + $indent .= "\tA: "; |
|
580 | + } |
|
581 | + |
|
582 | + @ob_end_clean(); |
|
583 | + echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
584 | + ob_start(); |
|
585 | + } |
|
586 | + |
|
587 | + return true; |
|
588 | + |
|
589 | + }//end replaceToken() |
|
590 | + |
|
591 | + |
|
592 | + /** |
|
593 | + * Reverts the previous fix made to a token. |
|
594 | + * |
|
595 | + * @param int $stackPtr The position of the token in the token stack. |
|
596 | + * |
|
597 | + * @return bool If a change was reverted. |
|
598 | + */ |
|
599 | + public function revertToken($stackPtr) |
|
600 | + { |
|
601 | + if (isset($this->_fixedTokens[$stackPtr]) === false) { |
|
602 | + return false; |
|
603 | + } |
|
604 | + |
|
605 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
606 | + $bt = debug_backtrace(); |
|
607 | + if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
608 | + $sniff = $bt[2]['class']; |
|
609 | + $line = $bt[1]['line']; |
|
610 | + } else { |
|
611 | + $sniff = $bt[1]['class']; |
|
612 | + $line = $bt[0]['line']; |
|
613 | + } |
|
614 | + |
|
615 | + $tokens = $this->_currentFile->getTokens(); |
|
616 | + $type = $tokens[$stackPtr]['type']; |
|
617 | + $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
|
618 | + $newContent = PHP_CodeSniffer::prepareForOutput($this->_fixedTokens[$stackPtr]); |
|
619 | + if (trim($this->_tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
620 | + // Add some context for whitespace only changes. |
|
621 | + $append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
|
622 | + $oldContent .= $append; |
|
623 | + $newContent .= $append; |
|
624 | + } |
|
625 | + }//end if |
|
626 | + |
|
627 | + $this->_tokens[$stackPtr] = $this->_fixedTokens[$stackPtr]; |
|
628 | + unset($this->_fixedTokens[$stackPtr]); |
|
629 | + $this->_numFixes--; |
|
630 | + |
|
631 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
632 | + $indent = "\t"; |
|
633 | + if (empty($this->_changeset) === false) { |
|
634 | + $indent .= "\tR: "; |
|
635 | + } |
|
636 | + |
|
637 | + @ob_end_clean(); |
|
638 | + echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
639 | + ob_start(); |
|
640 | + } |
|
641 | + |
|
642 | + return true; |
|
643 | + |
|
644 | + }//end revertToken() |
|
645 | + |
|
646 | + |
|
647 | + /** |
|
648 | + * Replace the content of a token with a part of its current content. |
|
649 | + * |
|
650 | + * @param int $stackPtr The position of the token in the token stack. |
|
651 | + * @param int $start The first character to keep. |
|
652 | + * @param int $length The number of chacters to keep. If NULL, the content of |
|
653 | + * the token from $start to the end of the content is kept. |
|
654 | + * |
|
655 | + * @return bool If the change was accepted. |
|
656 | + */ |
|
657 | + public function substrToken($stackPtr, $start, $length=null) |
|
658 | + { |
|
659 | + $current = $this->getTokenContent($stackPtr); |
|
660 | + |
|
661 | + if ($length === null) { |
|
662 | + $newContent = substr($current, $start); |
|
663 | + } else { |
|
664 | + $newContent = substr($current, $start, $length); |
|
665 | + } |
|
666 | + |
|
667 | + return $this->replaceToken($stackPtr, $newContent); |
|
668 | + |
|
669 | + }//end substrToken() |
|
670 | + |
|
671 | + |
|
672 | + /** |
|
673 | + * Adds a newline to end of a token's content. |
|
674 | + * |
|
675 | + * @param int $stackPtr The position of the token in the token stack. |
|
676 | + * |
|
677 | + * @return bool If the change was accepted. |
|
678 | + */ |
|
679 | + public function addNewline($stackPtr) |
|
680 | + { |
|
681 | + $current = $this->getTokenContent($stackPtr); |
|
682 | + return $this->replaceToken($stackPtr, $current.$this->_currentFile->eolChar); |
|
683 | + |
|
684 | + }//end addNewline() |
|
685 | + |
|
686 | + |
|
687 | + /** |
|
688 | + * Adds a newline to the start of a token's content. |
|
689 | + * |
|
690 | + * @param int $stackPtr The position of the token in the token stack. |
|
691 | + * |
|
692 | + * @return bool If the change was accepted. |
|
693 | + */ |
|
694 | + public function addNewlineBefore($stackPtr) |
|
695 | + { |
|
696 | + $current = $this->getTokenContent($stackPtr); |
|
697 | + return $this->replaceToken($stackPtr, $this->_currentFile->eolChar.$current); |
|
698 | + |
|
699 | + }//end addNewlineBefore() |
|
700 | + |
|
701 | + |
|
702 | + /** |
|
703 | + * Adds content to the end of a token's current content. |
|
704 | + * |
|
705 | + * @param int $stackPtr The position of the token in the token stack. |
|
706 | + * @param string $content The content to add. |
|
707 | + * |
|
708 | + * @return bool If the change was accepted. |
|
709 | + */ |
|
710 | + public function addContent($stackPtr, $content) |
|
711 | + { |
|
712 | + $current = $this->getTokenContent($stackPtr); |
|
713 | + return $this->replaceToken($stackPtr, $current.$content); |
|
714 | + |
|
715 | + }//end addContent() |
|
716 | + |
|
717 | + |
|
718 | + /** |
|
719 | + * Adds content to the start of a token's current content. |
|
720 | + * |
|
721 | + * @param int $stackPtr The position of the token in the token stack. |
|
722 | + * @param string $content The content to add. |
|
723 | + * |
|
724 | + * @return bool If the change was accepted. |
|
725 | + */ |
|
726 | + public function addContentBefore($stackPtr, $content) |
|
727 | + { |
|
728 | + $current = $this->getTokenContent($stackPtr); |
|
729 | + return $this->replaceToken($stackPtr, $content.$current); |
|
730 | + |
|
731 | + }//end addContentBefore() |
|
732 | 732 | |
733 | 733 | |
734 | 734 | }//end class |
@@ -283,14 +283,14 @@ |
||
283 | 283 | foreach ($diffLines as $line) { |
284 | 284 | if (isset($line[0]) === true) { |
285 | 285 | switch ($line[0]) { |
286 | - case '-': |
|
287 | - $diff[] = "\033[31m$line\033[0m"; |
|
288 | - break; |
|
289 | - case '+': |
|
290 | - $diff[] = "\033[32m$line\033[0m"; |
|
291 | - break; |
|
292 | - default: |
|
293 | - $diff[] = $line; |
|
286 | + case '-': |
|
287 | + $diff[] = "\033[31m$line\033[0m"; |
|
288 | + break; |
|
289 | + case '+': |
|
290 | + $diff[] = "\033[32m$line\033[0m"; |
|
291 | + break; |
|
292 | + default: |
|
293 | + $diff[] = $line; |
|
294 | 294 | } |
295 | 295 | } |
296 | 296 | } |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | * |
125 | 125 | * @return void |
126 | 126 | */ |
127 | - public function startFile($phpcsFile) |
|
127 | + public function startFile( $phpcsFile ) |
|
128 | 128 | { |
129 | 129 | $this->_currentFile = $phpcsFile; |
130 | 130 | $this->_numFixes = 0; |
@@ -132,11 +132,11 @@ discard block |
||
132 | 132 | |
133 | 133 | $tokens = $phpcsFile->getTokens(); |
134 | 134 | $this->_tokens = array(); |
135 | - foreach ($tokens as $index => $token) { |
|
136 | - if (isset($token['orig_content']) === true) { |
|
137 | - $this->_tokens[$index] = $token['orig_content']; |
|
135 | + foreach ( $tokens as $index => $token ) { |
|
136 | + if ( isset( $token[ 'orig_content' ] ) === true ) { |
|
137 | + $this->_tokens[ $index ] = $token[ 'orig_content' ]; |
|
138 | 138 | } else { |
139 | - $this->_tokens[$index] = $token['content']; |
|
139 | + $this->_tokens[ $index ] = $token[ 'content' ]; |
|
140 | 140 | } |
141 | 141 | } |
142 | 142 | |
@@ -151,71 +151,71 @@ discard block |
||
151 | 151 | public function fixFile() |
152 | 152 | { |
153 | 153 | $fixable = $this->_currentFile->getFixableCount(); |
154 | - if ($fixable === 0) { |
|
154 | + if ( $fixable === 0 ) { |
|
155 | 155 | // Nothing to fix. |
156 | 156 | return false; |
157 | 157 | } |
158 | 158 | |
159 | 159 | $stdin = false; |
160 | 160 | $cliValues = $this->_currentFile->phpcs->cli->getCommandLineValues(); |
161 | - if (empty($cliValues['files']) === true) { |
|
161 | + if ( empty( $cliValues[ 'files' ] ) === true ) { |
|
162 | 162 | $stdin = true; |
163 | 163 | } |
164 | 164 | |
165 | 165 | $this->enabled = true; |
166 | 166 | |
167 | 167 | $this->loops = 0; |
168 | - while ($this->loops < 50) { |
|
168 | + while ( $this->loops < 50 ) { |
|
169 | 169 | ob_start(); |
170 | 170 | |
171 | 171 | // Only needed once file content has changed. |
172 | 172 | $contents = $this->getContents(); |
173 | 173 | |
174 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
174 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
175 | 175 | @ob_end_clean(); |
176 | - echo '---START FILE CONTENT---'.PHP_EOL; |
|
177 | - $lines = explode($this->_currentFile->eolChar, $contents); |
|
178 | - $max = strlen(count($lines)); |
|
179 | - foreach ($lines as $lineNum => $line) { |
|
176 | + echo '---START FILE CONTENT---' . PHP_EOL; |
|
177 | + $lines = explode( $this->_currentFile->eolChar, $contents ); |
|
178 | + $max = strlen( count( $lines ) ); |
|
179 | + foreach ( $lines as $lineNum => $line ) { |
|
180 | 180 | $lineNum++; |
181 | - echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
181 | + echo str_pad( $lineNum, $max, ' ', STR_PAD_LEFT ) . '|' . $line . PHP_EOL; |
|
182 | 182 | } |
183 | 183 | |
184 | - echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
184 | + echo '--- END FILE CONTENT ---' . PHP_EOL; |
|
185 | 185 | ob_start(); |
186 | 186 | } |
187 | 187 | |
188 | 188 | $this->_inConflict = false; |
189 | 189 | $this->_currentFile->refreshTokenListeners(); |
190 | - $this->_currentFile->start($contents); |
|
190 | + $this->_currentFile->start( $contents ); |
|
191 | 191 | ob_end_clean(); |
192 | 192 | |
193 | 193 | $this->loops++; |
194 | 194 | |
195 | - if (PHP_CODESNIFFER_CBF === true && $stdin === false) { |
|
196 | - echo "\r".str_repeat(' ', 80)."\r"; |
|
195 | + if ( PHP_CODESNIFFER_CBF === true && $stdin === false ) { |
|
196 | + echo "\r" . str_repeat( ' ', 80 ) . "\r"; |
|
197 | 197 | echo "\t=> Fixing file: $this->_numFixes/$fixable violations remaining [made $this->loops pass"; |
198 | - if ($this->loops > 1) { |
|
198 | + if ( $this->loops > 1 ) { |
|
199 | 199 | echo 'es'; |
200 | 200 | } |
201 | 201 | |
202 | 202 | echo ']... '; |
203 | 203 | } |
204 | 204 | |
205 | - if ($this->_numFixes === 0 && $this->_inConflict === false) { |
|
205 | + if ( $this->_numFixes === 0 && $this->_inConflict === false ) { |
|
206 | 206 | // Nothing left to do. |
207 | 207 | break; |
208 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
209 | - echo "\t* fixed $this->_numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
208 | + } else if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
209 | + echo "\t* fixed $this->_numFixes violations, starting loop " . ( $this->loops + 1 ) . ' *' . PHP_EOL; |
|
210 | 210 | } |
211 | 211 | }//end while |
212 | 212 | |
213 | 213 | $this->enabled = false; |
214 | 214 | |
215 | - if ($this->_numFixes > 0) { |
|
216 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
215 | + if ( $this->_numFixes > 0 ) { |
|
216 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
217 | 217 | @ob_end_clean(); |
218 | - echo "\t*** Reached maximum number of loops with $this->_numFixes violations left unfixed ***".PHP_EOL; |
|
218 | + echo "\t*** Reached maximum number of loops with $this->_numFixes violations left unfixed ***" . PHP_EOL; |
|
219 | 219 | ob_start(); |
220 | 220 | } |
221 | 221 | |
@@ -237,65 +237,65 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return string |
239 | 239 | */ |
240 | - public function generateDiff($filePath=null, $colors=true) |
|
240 | + public function generateDiff( $filePath = null, $colors = true ) |
|
241 | 241 | { |
242 | - if ($filePath === null) { |
|
242 | + if ( $filePath === null ) { |
|
243 | 243 | $filePath = $this->_currentFile->getFilename(); |
244 | 244 | } |
245 | 245 | |
246 | - $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
247 | - $filename = str_replace($cwd, '', $filePath); |
|
246 | + $cwd = getcwd() . DIRECTORY_SEPARATOR; |
|
247 | + $filename = str_replace( $cwd, '', $filePath ); |
|
248 | 248 | $contents = $this->getContents(); |
249 | 249 | |
250 | - if (function_exists('sys_get_temp_dir') === true) { |
|
250 | + if ( function_exists( 'sys_get_temp_dir' ) === true ) { |
|
251 | 251 | // This is needed for HHVM support, but only available from 5.2.1. |
252 | - $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
253 | - $fixedFile = fopen($tempName, 'w'); |
|
252 | + $tempName = tempnam( sys_get_temp_dir(), 'phpcs-fixer' ); |
|
253 | + $fixedFile = fopen( $tempName, 'w' ); |
|
254 | 254 | } else { |
255 | 255 | $fixedFile = tmpfile(); |
256 | - $data = stream_get_meta_data($fixedFile); |
|
257 | - $tempName = $data['uri']; |
|
256 | + $data = stream_get_meta_data( $fixedFile ); |
|
257 | + $tempName = $data[ 'uri' ]; |
|
258 | 258 | } |
259 | 259 | |
260 | - fwrite($fixedFile, $contents); |
|
260 | + fwrite( $fixedFile, $contents ); |
|
261 | 261 | |
262 | 262 | // We must use something like shell_exec() because whitespace at the end |
263 | 263 | // of lines is critical to diff files. |
264 | 264 | $cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\""; |
265 | - $diff = shell_exec($cmd); |
|
265 | + $diff = shell_exec( $cmd ); |
|
266 | 266 | |
267 | - fclose($fixedFile); |
|
268 | - if (is_file($tempName) === true) { |
|
269 | - unlink($tempName); |
|
267 | + fclose( $fixedFile ); |
|
268 | + if ( is_file( $tempName ) === true ) { |
|
269 | + unlink( $tempName ); |
|
270 | 270 | } |
271 | 271 | |
272 | - if ($colors === false) { |
|
272 | + if ( $colors === false ) { |
|
273 | 273 | return $diff; |
274 | 274 | } |
275 | 275 | |
276 | - $diffLines = explode(PHP_EOL, $diff); |
|
277 | - if (count($diffLines) === 1) { |
|
276 | + $diffLines = explode( PHP_EOL, $diff ); |
|
277 | + if ( count( $diffLines ) === 1 ) { |
|
278 | 278 | // Seems to be required for cygwin. |
279 | - $diffLines = explode("\n", $diff); |
|
279 | + $diffLines = explode( "\n", $diff ); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | $diff = array(); |
283 | - foreach ($diffLines as $line) { |
|
284 | - if (isset($line[0]) === true) { |
|
285 | - switch ($line[0]) { |
|
283 | + foreach ( $diffLines as $line ) { |
|
284 | + if ( isset( $line[ 0 ] ) === true ) { |
|
285 | + switch ( $line[ 0 ] ) { |
|
286 | 286 | case '-': |
287 | - $diff[] = "\033[31m$line\033[0m"; |
|
287 | + $diff[ ] = "\033[31m$line\033[0m"; |
|
288 | 288 | break; |
289 | 289 | case '+': |
290 | - $diff[] = "\033[32m$line\033[0m"; |
|
290 | + $diff[ ] = "\033[32m$line\033[0m"; |
|
291 | 291 | break; |
292 | 292 | default: |
293 | - $diff[] = $line; |
|
293 | + $diff[ ] = $line; |
|
294 | 294 | } |
295 | 295 | } |
296 | 296 | } |
297 | 297 | |
298 | - $diff = implode(PHP_EOL, $diff); |
|
298 | + $diff = implode( PHP_EOL, $diff ); |
|
299 | 299 | |
300 | 300 | return $diff; |
301 | 301 | |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | */ |
325 | 325 | public function getContents() |
326 | 326 | { |
327 | - $contents = implode($this->_tokens); |
|
327 | + $contents = implode( $this->_tokens ); |
|
328 | 328 | return $contents; |
329 | 329 | |
330 | 330 | }//end getContents() |
@@ -340,14 +340,14 @@ discard block |
||
340 | 340 | * |
341 | 341 | * @return string |
342 | 342 | */ |
343 | - public function getTokenContent($stackPtr) |
|
343 | + public function getTokenContent( $stackPtr ) |
|
344 | 344 | { |
345 | - if ($this->_inChangeset === true |
|
346 | - && isset($this->_changeset[$stackPtr]) === true |
|
345 | + if ( $this->_inChangeset === true |
|
346 | + && isset( $this->_changeset[ $stackPtr ] ) === true |
|
347 | 347 | ) { |
348 | - return $this->_changeset[$stackPtr]; |
|
348 | + return $this->_changeset[ $stackPtr ]; |
|
349 | 349 | } else { |
350 | - return $this->_tokens[$stackPtr]; |
|
350 | + return $this->_tokens[ $stackPtr ]; |
|
351 | 351 | } |
352 | 352 | |
353 | 353 | }//end getTokenContent() |
@@ -360,17 +360,17 @@ discard block |
||
360 | 360 | */ |
361 | 361 | public function beginChangeset() |
362 | 362 | { |
363 | - if ($this->_inConflict === true) { |
|
363 | + if ( $this->_inConflict === true ) { |
|
364 | 364 | return false; |
365 | 365 | } |
366 | 366 | |
367 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
367 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
368 | 368 | $bt = debug_backtrace(); |
369 | - $sniff = $bt[1]['class']; |
|
370 | - $line = $bt[0]['line']; |
|
369 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
370 | + $line = $bt[ 0 ][ 'line' ]; |
|
371 | 371 | |
372 | 372 | @ob_end_clean(); |
373 | - echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
|
373 | + echo "\t=> Changeset started by $sniff (line $line)" . PHP_EOL; |
|
374 | 374 | ob_start(); |
375 | 375 | } |
376 | 376 | |
@@ -387,7 +387,7 @@ discard block |
||
387 | 387 | */ |
388 | 388 | public function endChangeset() |
389 | 389 | { |
390 | - if ($this->_inConflict === true) { |
|
390 | + if ( $this->_inConflict === true ) { |
|
391 | 391 | return false; |
392 | 392 | } |
393 | 393 | |
@@ -395,30 +395,30 @@ discard block |
||
395 | 395 | |
396 | 396 | $success = true; |
397 | 397 | $applied = array(); |
398 | - foreach ($this->_changeset as $stackPtr => $content) { |
|
399 | - $success = $this->replaceToken($stackPtr, $content); |
|
400 | - if ($success === false) { |
|
398 | + foreach ( $this->_changeset as $stackPtr => $content ) { |
|
399 | + $success = $this->replaceToken( $stackPtr, $content ); |
|
400 | + if ( $success === false ) { |
|
401 | 401 | break; |
402 | 402 | } else { |
403 | - $applied[] = $stackPtr; |
|
403 | + $applied[ ] = $stackPtr; |
|
404 | 404 | } |
405 | 405 | } |
406 | 406 | |
407 | - if ($success === false) { |
|
407 | + if ( $success === false ) { |
|
408 | 408 | // Rolling back all changes. |
409 | - foreach ($applied as $stackPtr) { |
|
410 | - $this->revertToken($stackPtr); |
|
409 | + foreach ( $applied as $stackPtr ) { |
|
410 | + $this->revertToken( $stackPtr ); |
|
411 | 411 | } |
412 | 412 | |
413 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
413 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
414 | 414 | @ob_end_clean(); |
415 | - echo "\t=> Changeset failed to apply".PHP_EOL; |
|
415 | + echo "\t=> Changeset failed to apply" . PHP_EOL; |
|
416 | 416 | ob_start(); |
417 | 417 | } |
418 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
419 | - $fixes = count($this->_changeset); |
|
418 | + } else if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
419 | + $fixes = count( $this->_changeset ); |
|
420 | 420 | @ob_end_clean(); |
421 | - echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
421 | + echo "\t=> Changeset ended: $fixes changes applied" . PHP_EOL; |
|
422 | 422 | ob_start(); |
423 | 423 | } |
424 | 424 | |
@@ -437,22 +437,22 @@ discard block |
||
437 | 437 | $this->_inChangeset = false; |
438 | 438 | $this->_inConflict = false; |
439 | 439 | |
440 | - if (empty($this->_changeset) === false) { |
|
441 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
440 | + if ( empty( $this->_changeset ) === false ) { |
|
441 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
442 | 442 | $bt = debug_backtrace(); |
443 | - if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
444 | - $sniff = $bt[2]['class']; |
|
445 | - $line = $bt[1]['line']; |
|
443 | + if ( $bt[ 1 ][ 'class' ] === 'PHP_CodeSniffer_Fixer' ) { |
|
444 | + $sniff = $bt[ 2 ][ 'class' ]; |
|
445 | + $line = $bt[ 1 ][ 'line' ]; |
|
446 | 446 | } else { |
447 | - $sniff = $bt[1]['class']; |
|
448 | - $line = $bt[0]['line']; |
|
447 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
448 | + $line = $bt[ 0 ][ 'line' ]; |
|
449 | 449 | } |
450 | 450 | |
451 | - $numChanges = count($this->_changeset); |
|
451 | + $numChanges = count( $this->_changeset ); |
|
452 | 452 | |
453 | 453 | @ob_end_clean(); |
454 | - echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
455 | - echo "\t=> Changeset rolled back".PHP_EOL; |
|
454 | + echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)" . PHP_EOL; |
|
455 | + echo "\t=> Changeset rolled back" . PHP_EOL; |
|
456 | 456 | ob_start(); |
457 | 457 | } |
458 | 458 | |
@@ -470,117 +470,117 @@ discard block |
||
470 | 470 | * |
471 | 471 | * @return bool If the change was accepted. |
472 | 472 | */ |
473 | - public function replaceToken($stackPtr, $content) |
|
473 | + public function replaceToken( $stackPtr, $content ) |
|
474 | 474 | { |
475 | - if ($this->_inConflict === true) { |
|
475 | + if ( $this->_inConflict === true ) { |
|
476 | 476 | return false; |
477 | 477 | } |
478 | 478 | |
479 | - if ($this->_inChangeset === false |
|
480 | - && isset($this->_fixedTokens[$stackPtr]) === true |
|
479 | + if ( $this->_inChangeset === false |
|
480 | + && isset( $this->_fixedTokens[ $stackPtr ] ) === true |
|
481 | 481 | ) { |
482 | 482 | $indent = "\t"; |
483 | - if (empty($this->_changeset) === false) { |
|
483 | + if ( empty( $this->_changeset ) === false ) { |
|
484 | 484 | $indent .= "\t"; |
485 | 485 | } |
486 | 486 | |
487 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
487 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
488 | 488 | @ob_end_clean(); |
489 | - echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
489 | + echo "$indent* token $stackPtr has already been modified, skipping *" . PHP_EOL; |
|
490 | 490 | ob_start(); |
491 | 491 | } |
492 | 492 | |
493 | 493 | return false; |
494 | 494 | } |
495 | 495 | |
496 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
496 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
497 | 497 | $bt = debug_backtrace(); |
498 | - if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
499 | - $sniff = $bt[2]['class']; |
|
500 | - $line = $bt[1]['line']; |
|
498 | + if ( $bt[ 1 ][ 'class' ] === 'PHP_CodeSniffer_Fixer' ) { |
|
499 | + $sniff = $bt[ 2 ][ 'class' ]; |
|
500 | + $line = $bt[ 1 ][ 'line' ]; |
|
501 | 501 | } else { |
502 | - $sniff = $bt[1]['class']; |
|
503 | - $line = $bt[0]['line']; |
|
502 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
503 | + $line = $bt[ 0 ][ 'line' ]; |
|
504 | 504 | } |
505 | 505 | |
506 | 506 | $tokens = $this->_currentFile->getTokens(); |
507 | - $type = $tokens[$stackPtr]['type']; |
|
508 | - $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
|
509 | - $newContent = PHP_CodeSniffer::prepareForOutput($content); |
|
510 | - if (trim($this->_tokens[$stackPtr]) === '' && isset($this->_tokens[($stackPtr + 1)]) === true) { |
|
507 | + $type = $tokens[ $stackPtr ][ 'type' ]; |
|
508 | + $oldContent = PHP_CodeSniffer::prepareForOutput( $this->_tokens[ $stackPtr ] ); |
|
509 | + $newContent = PHP_CodeSniffer::prepareForOutput( $content ); |
|
510 | + if ( trim( $this->_tokens[ $stackPtr ] ) === '' && isset( $this->_tokens[ ( $stackPtr + 1 ) ] ) === true ) { |
|
511 | 511 | // Add some context for whitespace only changes. |
512 | - $append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
|
512 | + $append = PHP_CodeSniffer::prepareForOutput( $this->_tokens[ ( $stackPtr + 1 ) ] ); |
|
513 | 513 | $oldContent .= $append; |
514 | 514 | $newContent .= $append; |
515 | 515 | } |
516 | 516 | }//end if |
517 | 517 | |
518 | - if ($this->_inChangeset === true) { |
|
519 | - $this->_changeset[$stackPtr] = $content; |
|
518 | + if ( $this->_inChangeset === true ) { |
|
519 | + $this->_changeset[ $stackPtr ] = $content; |
|
520 | 520 | |
521 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
521 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
522 | 522 | @ob_end_clean(); |
523 | - echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
523 | + echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"" . PHP_EOL; |
|
524 | 524 | ob_start(); |
525 | 525 | } |
526 | 526 | |
527 | 527 | return true; |
528 | 528 | } |
529 | 529 | |
530 | - if (isset($this->_oldTokenValues[$stackPtr]) === false) { |
|
531 | - $this->_oldTokenValues[$stackPtr] = array( |
|
530 | + if ( isset( $this->_oldTokenValues[ $stackPtr ] ) === false ) { |
|
531 | + $this->_oldTokenValues[ $stackPtr ] = array( |
|
532 | 532 | 'curr' => $content, |
533 | - 'prev' => $this->_tokens[$stackPtr], |
|
533 | + 'prev' => $this->_tokens[ $stackPtr ], |
|
534 | 534 | 'loop' => $this->loops, |
535 | 535 | ); |
536 | 536 | } else { |
537 | - if ($this->_oldTokenValues[$stackPtr]['prev'] === $content |
|
538 | - && $this->_oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
537 | + if ( $this->_oldTokenValues[ $stackPtr ][ 'prev' ] === $content |
|
538 | + && $this->_oldTokenValues[ $stackPtr ][ 'loop' ] === ( $this->loops - 1 ) |
|
539 | 539 | ) { |
540 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
540 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
541 | 541 | $indent = "\t"; |
542 | - if (empty($this->_changeset) === false) { |
|
542 | + if ( empty( $this->_changeset ) === false ) { |
|
543 | 543 | $indent .= "\t"; |
544 | 544 | } |
545 | 545 | |
546 | - $loop = $this->_oldTokenValues[$stackPtr]['loop']; |
|
546 | + $loop = $this->_oldTokenValues[ $stackPtr ][ 'loop' ]; |
|
547 | 547 | |
548 | 548 | @ob_end_clean(); |
549 | - echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
550 | - echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
549 | + echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****" . PHP_EOL; |
|
550 | + echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****" . PHP_EOL; |
|
551 | 551 | } |
552 | 552 | |
553 | - if ($this->_oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
553 | + if ( $this->_oldTokenValues[ $stackPtr ][ 'loop' ] >= ( $this->loops - 1 ) ) { |
|
554 | 554 | $this->_inConflict = true; |
555 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
556 | - echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
555 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
556 | + echo "$indent**** ignoring all changes until next loop ****" . PHP_EOL; |
|
557 | 557 | } |
558 | 558 | } |
559 | 559 | |
560 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
560 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
561 | 561 | ob_start(); |
562 | 562 | } |
563 | 563 | |
564 | 564 | return false; |
565 | 565 | }//end if |
566 | 566 | |
567 | - $this->_oldTokenValues[$stackPtr]['prev'] = $this->_oldTokenValues[$stackPtr]['curr']; |
|
568 | - $this->_oldTokenValues[$stackPtr]['curr'] = $content; |
|
569 | - $this->_oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
567 | + $this->_oldTokenValues[ $stackPtr ][ 'prev' ] = $this->_oldTokenValues[ $stackPtr ][ 'curr' ]; |
|
568 | + $this->_oldTokenValues[ $stackPtr ][ 'curr' ] = $content; |
|
569 | + $this->_oldTokenValues[ $stackPtr ][ 'loop' ] = $this->loops; |
|
570 | 570 | }//end if |
571 | 571 | |
572 | - $this->_fixedTokens[$stackPtr] = $this->_tokens[$stackPtr]; |
|
573 | - $this->_tokens[$stackPtr] = $content; |
|
572 | + $this->_fixedTokens[ $stackPtr ] = $this->_tokens[ $stackPtr ]; |
|
573 | + $this->_tokens[ $stackPtr ] = $content; |
|
574 | 574 | $this->_numFixes++; |
575 | 575 | |
576 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
576 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
577 | 577 | $indent = "\t"; |
578 | - if (empty($this->_changeset) === false) { |
|
578 | + if ( empty( $this->_changeset ) === false ) { |
|
579 | 579 | $indent .= "\tA: "; |
580 | 580 | } |
581 | 581 | |
582 | 582 | @ob_end_clean(); |
583 | - echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
583 | + echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"" . PHP_EOL; |
|
584 | 584 | ob_start(); |
585 | 585 | } |
586 | 586 | |
@@ -596,46 +596,46 @@ discard block |
||
596 | 596 | * |
597 | 597 | * @return bool If a change was reverted. |
598 | 598 | */ |
599 | - public function revertToken($stackPtr) |
|
599 | + public function revertToken( $stackPtr ) |
|
600 | 600 | { |
601 | - if (isset($this->_fixedTokens[$stackPtr]) === false) { |
|
601 | + if ( isset( $this->_fixedTokens[ $stackPtr ] ) === false ) { |
|
602 | 602 | return false; |
603 | 603 | } |
604 | 604 | |
605 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
605 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
606 | 606 | $bt = debug_backtrace(); |
607 | - if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
|
608 | - $sniff = $bt[2]['class']; |
|
609 | - $line = $bt[1]['line']; |
|
607 | + if ( $bt[ 1 ][ 'class' ] === 'PHP_CodeSniffer_Fixer' ) { |
|
608 | + $sniff = $bt[ 2 ][ 'class' ]; |
|
609 | + $line = $bt[ 1 ][ 'line' ]; |
|
610 | 610 | } else { |
611 | - $sniff = $bt[1]['class']; |
|
612 | - $line = $bt[0]['line']; |
|
611 | + $sniff = $bt[ 1 ][ 'class' ]; |
|
612 | + $line = $bt[ 0 ][ 'line' ]; |
|
613 | 613 | } |
614 | 614 | |
615 | 615 | $tokens = $this->_currentFile->getTokens(); |
616 | - $type = $tokens[$stackPtr]['type']; |
|
617 | - $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
|
618 | - $newContent = PHP_CodeSniffer::prepareForOutput($this->_fixedTokens[$stackPtr]); |
|
619 | - if (trim($this->_tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
616 | + $type = $tokens[ $stackPtr ][ 'type' ]; |
|
617 | + $oldContent = PHP_CodeSniffer::prepareForOutput( $this->_tokens[ $stackPtr ] ); |
|
618 | + $newContent = PHP_CodeSniffer::prepareForOutput( $this->_fixedTokens[ $stackPtr ] ); |
|
619 | + if ( trim( $this->_tokens[ $stackPtr ] ) === '' && isset( $tokens[ ( $stackPtr + 1 ) ] ) === true ) { |
|
620 | 620 | // Add some context for whitespace only changes. |
621 | - $append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
|
621 | + $append = PHP_CodeSniffer::prepareForOutput( $this->_tokens[ ( $stackPtr + 1 ) ] ); |
|
622 | 622 | $oldContent .= $append; |
623 | 623 | $newContent .= $append; |
624 | 624 | } |
625 | 625 | }//end if |
626 | 626 | |
627 | - $this->_tokens[$stackPtr] = $this->_fixedTokens[$stackPtr]; |
|
628 | - unset($this->_fixedTokens[$stackPtr]); |
|
627 | + $this->_tokens[ $stackPtr ] = $this->_fixedTokens[ $stackPtr ]; |
|
628 | + unset( $this->_fixedTokens[ $stackPtr ] ); |
|
629 | 629 | $this->_numFixes--; |
630 | 630 | |
631 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
631 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
632 | 632 | $indent = "\t"; |
633 | - if (empty($this->_changeset) === false) { |
|
633 | + if ( empty( $this->_changeset ) === false ) { |
|
634 | 634 | $indent .= "\tR: "; |
635 | 635 | } |
636 | 636 | |
637 | 637 | @ob_end_clean(); |
638 | - echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
638 | + echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"" . PHP_EOL; |
|
639 | 639 | ob_start(); |
640 | 640 | } |
641 | 641 | |
@@ -654,17 +654,17 @@ discard block |
||
654 | 654 | * |
655 | 655 | * @return bool If the change was accepted. |
656 | 656 | */ |
657 | - public function substrToken($stackPtr, $start, $length=null) |
|
657 | + public function substrToken( $stackPtr, $start, $length = null ) |
|
658 | 658 | { |
659 | - $current = $this->getTokenContent($stackPtr); |
|
659 | + $current = $this->getTokenContent( $stackPtr ); |
|
660 | 660 | |
661 | - if ($length === null) { |
|
662 | - $newContent = substr($current, $start); |
|
661 | + if ( $length === null ) { |
|
662 | + $newContent = substr( $current, $start ); |
|
663 | 663 | } else { |
664 | - $newContent = substr($current, $start, $length); |
|
664 | + $newContent = substr( $current, $start, $length ); |
|
665 | 665 | } |
666 | 666 | |
667 | - return $this->replaceToken($stackPtr, $newContent); |
|
667 | + return $this->replaceToken( $stackPtr, $newContent ); |
|
668 | 668 | |
669 | 669 | }//end substrToken() |
670 | 670 | |
@@ -676,10 +676,10 @@ discard block |
||
676 | 676 | * |
677 | 677 | * @return bool If the change was accepted. |
678 | 678 | */ |
679 | - public function addNewline($stackPtr) |
|
679 | + public function addNewline( $stackPtr ) |
|
680 | 680 | { |
681 | - $current = $this->getTokenContent($stackPtr); |
|
682 | - return $this->replaceToken($stackPtr, $current.$this->_currentFile->eolChar); |
|
681 | + $current = $this->getTokenContent( $stackPtr ); |
|
682 | + return $this->replaceToken( $stackPtr, $current . $this->_currentFile->eolChar ); |
|
683 | 683 | |
684 | 684 | }//end addNewline() |
685 | 685 | |
@@ -691,10 +691,10 @@ discard block |
||
691 | 691 | * |
692 | 692 | * @return bool If the change was accepted. |
693 | 693 | */ |
694 | - public function addNewlineBefore($stackPtr) |
|
694 | + public function addNewlineBefore( $stackPtr ) |
|
695 | 695 | { |
696 | - $current = $this->getTokenContent($stackPtr); |
|
697 | - return $this->replaceToken($stackPtr, $this->_currentFile->eolChar.$current); |
|
696 | + $current = $this->getTokenContent( $stackPtr ); |
|
697 | + return $this->replaceToken( $stackPtr, $this->_currentFile->eolChar . $current ); |
|
698 | 698 | |
699 | 699 | }//end addNewlineBefore() |
700 | 700 | |
@@ -707,10 +707,10 @@ discard block |
||
707 | 707 | * |
708 | 708 | * @return bool If the change was accepted. |
709 | 709 | */ |
710 | - public function addContent($stackPtr, $content) |
|
710 | + public function addContent( $stackPtr, $content ) |
|
711 | 711 | { |
712 | - $current = $this->getTokenContent($stackPtr); |
|
713 | - return $this->replaceToken($stackPtr, $current.$content); |
|
712 | + $current = $this->getTokenContent( $stackPtr ); |
|
713 | + return $this->replaceToken( $stackPtr, $current . $content ); |
|
714 | 714 | |
715 | 715 | }//end addContent() |
716 | 716 | |
@@ -723,10 +723,10 @@ discard block |
||
723 | 723 | * |
724 | 724 | * @return bool If the change was accepted. |
725 | 725 | */ |
726 | - public function addContentBefore($stackPtr, $content) |
|
726 | + public function addContentBefore( $stackPtr, $content ) |
|
727 | 727 | { |
728 | - $current = $this->getTokenContent($stackPtr); |
|
729 | - return $this->replaceToken($stackPtr, $content.$current); |
|
728 | + $current = $this->getTokenContent( $stackPtr ); |
|
729 | + return $this->replaceToken( $stackPtr, $content . $current ); |
|
730 | 730 | |
731 | 731 | }//end addContentBefore() |
732 | 732 |
@@ -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 PHP_CodeSniffer_Fixer |
|
30 | -{ |
|
29 | +class PHP_CodeSniffer_Fixer { |
|
31 | 30 | |
32 | 31 | /** |
33 | 32 | * Is the fixer enabled and fixing a file? |
@@ -124,8 +123,7 @@ discard block |
||
124 | 123 | * |
125 | 124 | * @return void |
126 | 125 | */ |
127 | - public function startFile($phpcsFile) |
|
128 | - { |
|
126 | + public function startFile($phpcsFile) { |
|
129 | 127 | $this->_currentFile = $phpcsFile; |
130 | 128 | $this->_numFixes = 0; |
131 | 129 | $this->_fixedTokens = array(); |
@@ -148,8 +146,7 @@ discard block |
||
148 | 146 | * |
149 | 147 | * @return boolean |
150 | 148 | */ |
151 | - public function fixFile() |
|
152 | - { |
|
149 | + public function fixFile() { |
|
153 | 150 | $fixable = $this->_currentFile->getFixableCount(); |
154 | 151 | if ($fixable === 0) { |
155 | 152 | // Nothing to fix. |
@@ -237,8 +234,7 @@ discard block |
||
237 | 234 | * |
238 | 235 | * @return string |
239 | 236 | */ |
240 | - public function generateDiff($filePath=null, $colors=true) |
|
241 | - { |
|
237 | + public function generateDiff($filePath=null, $colors=true) { |
|
242 | 238 | if ($filePath === null) { |
243 | 239 | $filePath = $this->_currentFile->getFilename(); |
244 | 240 | } |
@@ -310,8 +306,7 @@ discard block |
||
310 | 306 | * |
311 | 307 | * @return int |
312 | 308 | */ |
313 | - public function getFixCount() |
|
314 | - { |
|
309 | + public function getFixCount() { |
|
315 | 310 | return $this->_numFixes; |
316 | 311 | |
317 | 312 | }//end getFixCount() |
@@ -322,8 +317,7 @@ discard block |
||
322 | 317 | * |
323 | 318 | * @return string |
324 | 319 | */ |
325 | - public function getContents() |
|
326 | - { |
|
320 | + public function getContents() { |
|
327 | 321 | $contents = implode($this->_tokens); |
328 | 322 | return $contents; |
329 | 323 | |
@@ -340,8 +334,7 @@ discard block |
||
340 | 334 | * |
341 | 335 | * @return string |
342 | 336 | */ |
343 | - public function getTokenContent($stackPtr) |
|
344 | - { |
|
337 | + public function getTokenContent($stackPtr) { |
|
345 | 338 | if ($this->_inChangeset === true |
346 | 339 | && isset($this->_changeset[$stackPtr]) === true |
347 | 340 | ) { |
@@ -358,8 +351,7 @@ discard block |
||
358 | 351 | * |
359 | 352 | * @return void |
360 | 353 | */ |
361 | - public function beginChangeset() |
|
362 | - { |
|
354 | + public function beginChangeset() { |
|
363 | 355 | if ($this->_inConflict === true) { |
364 | 356 | return false; |
365 | 357 | } |
@@ -385,8 +377,7 @@ discard block |
||
385 | 377 | * |
386 | 378 | * @return boolean |
387 | 379 | */ |
388 | - public function endChangeset() |
|
389 | - { |
|
380 | + public function endChangeset() { |
|
390 | 381 | if ($this->_inConflict === true) { |
391 | 382 | return false; |
392 | 383 | } |
@@ -432,8 +423,7 @@ discard block |
||
432 | 423 | * |
433 | 424 | * @return void |
434 | 425 | */ |
435 | - public function rollbackChangeset() |
|
436 | - { |
|
426 | + public function rollbackChangeset() { |
|
437 | 427 | $this->_inChangeset = false; |
438 | 428 | $this->_inConflict = false; |
439 | 429 | |
@@ -470,8 +460,7 @@ discard block |
||
470 | 460 | * |
471 | 461 | * @return bool If the change was accepted. |
472 | 462 | */ |
473 | - public function replaceToken($stackPtr, $content) |
|
474 | - { |
|
463 | + public function replaceToken($stackPtr, $content) { |
|
475 | 464 | if ($this->_inConflict === true) { |
476 | 465 | return false; |
477 | 466 | } |
@@ -596,8 +585,7 @@ discard block |
||
596 | 585 | * |
597 | 586 | * @return bool If a change was reverted. |
598 | 587 | */ |
599 | - public function revertToken($stackPtr) |
|
600 | - { |
|
588 | + public function revertToken($stackPtr) { |
|
601 | 589 | if (isset($this->_fixedTokens[$stackPtr]) === false) { |
602 | 590 | return false; |
603 | 591 | } |
@@ -654,8 +642,7 @@ discard block |
||
654 | 642 | * |
655 | 643 | * @return bool If the change was accepted. |
656 | 644 | */ |
657 | - public function substrToken($stackPtr, $start, $length=null) |
|
658 | - { |
|
645 | + public function substrToken($stackPtr, $start, $length=null) { |
|
659 | 646 | $current = $this->getTokenContent($stackPtr); |
660 | 647 | |
661 | 648 | if ($length === null) { |
@@ -676,8 +663,7 @@ discard block |
||
676 | 663 | * |
677 | 664 | * @return bool If the change was accepted. |
678 | 665 | */ |
679 | - public function addNewline($stackPtr) |
|
680 | - { |
|
666 | + public function addNewline($stackPtr) { |
|
681 | 667 | $current = $this->getTokenContent($stackPtr); |
682 | 668 | return $this->replaceToken($stackPtr, $current.$this->_currentFile->eolChar); |
683 | 669 | |
@@ -691,8 +677,7 @@ discard block |
||
691 | 677 | * |
692 | 678 | * @return bool If the change was accepted. |
693 | 679 | */ |
694 | - public function addNewlineBefore($stackPtr) |
|
695 | - { |
|
680 | + public function addNewlineBefore($stackPtr) { |
|
696 | 681 | $current = $this->getTokenContent($stackPtr); |
697 | 682 | return $this->replaceToken($stackPtr, $this->_currentFile->eolChar.$current); |
698 | 683 | |
@@ -707,8 +692,7 @@ discard block |
||
707 | 692 | * |
708 | 693 | * @return bool If the change was accepted. |
709 | 694 | */ |
710 | - public function addContent($stackPtr, $content) |
|
711 | - { |
|
695 | + public function addContent($stackPtr, $content) { |
|
712 | 696 | $current = $this->getTokenContent($stackPtr); |
713 | 697 | return $this->replaceToken($stackPtr, $current.$content); |
714 | 698 | |
@@ -723,8 +707,7 @@ discard block |
||
723 | 707 | * |
724 | 708 | * @return bool If the change was accepted. |
725 | 709 | */ |
726 | - public function addContentBefore($stackPtr, $content) |
|
727 | - { |
|
710 | + public function addContentBefore($stackPtr, $content) { |
|
728 | 711 | $current = $this->getTokenContent($stackPtr); |
729 | 712 | return $this->replaceToken($stackPtr, $content.$current); |
730 | 713 |
@@ -49,7 +49,7 @@ |
||
49 | 49 | * @param boolean $showSources Show sources? |
50 | 50 | * @param int $width Maximum allowed line width. |
51 | 51 | * |
52 | - * @return boolean |
|
52 | + * @return null|boolean |
|
53 | 53 | */ |
54 | 54 | public function generateFileReport( |
55 | 55 | $report, |
@@ -37,115 +37,115 @@ |
||
37 | 37 | { |
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * Generate a partial report for a single processed file. |
|
42 | - * |
|
43 | - * Function should return TRUE if it printed or stored data about the file |
|
44 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
45 | - * its data should be counted in the grand totals. |
|
46 | - * |
|
47 | - * @param array $report Prepared report data. |
|
48 | - * @param PHP_CodeSniffer_File $phpcsFile The file being reported on. |
|
49 | - * @param boolean $showSources Show sources? |
|
50 | - * @param int $width Maximum allowed line width. |
|
51 | - * |
|
52 | - * @return boolean |
|
53 | - */ |
|
54 | - public function generateFileReport( |
|
55 | - $report, |
|
56 | - PHP_CodeSniffer_File $phpcsFile, |
|
57 | - $showSources=false, |
|
58 | - $width=80 |
|
59 | - ) { |
|
60 | - $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
|
61 | - $errors = $phpcsFile->getFixableCount(); |
|
62 | - if ($errors !== 0) { |
|
63 | - if (empty($cliValues['files']) === false) { |
|
64 | - ob_end_clean(); |
|
65 | - $errors = $phpcsFile->getFixableCount(); |
|
66 | - $startTime = microtime(true); |
|
67 | - echo "\t=> Fixing file: $errors/$errors violations remaining"; |
|
68 | - } |
|
69 | - |
|
70 | - $fixed = $phpcsFile->fixer->fixFile(); |
|
71 | - } |
|
72 | - |
|
73 | - if (empty($cliValues['files']) === true) { |
|
74 | - // Replacing STDIN, so output current file to STDOUT |
|
75 | - // even if nothing was fixed. Exit here because we |
|
76 | - // can't process any more than 1 file in this setup. |
|
77 | - echo $phpcsFile->fixer->getContents(); |
|
78 | - ob_end_flush(); |
|
79 | - exit(1); |
|
80 | - } |
|
81 | - |
|
82 | - if ($errors === 0) { |
|
83 | - return false; |
|
84 | - } |
|
85 | - |
|
86 | - if ($fixed === false) { |
|
87 | - echo 'ERROR'; |
|
88 | - } else { |
|
89 | - echo 'DONE'; |
|
90 | - } |
|
91 | - |
|
92 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
93 | - if ($timeTaken < 1000) { |
|
94 | - $timeTaken = round($timeTaken); |
|
95 | - echo " in {$timeTaken}ms".PHP_EOL; |
|
96 | - } else { |
|
97 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
98 | - echo " in $timeTaken secs".PHP_EOL; |
|
99 | - } |
|
100 | - |
|
101 | - if ($fixed === true) { |
|
102 | - $newFilename = $report['filename'].$cliValues['phpcbf-suffix']; |
|
103 | - $newContent = $phpcsFile->fixer->getContents(); |
|
104 | - file_put_contents($newFilename, $newContent); |
|
105 | - |
|
106 | - if ($newFilename === $report['filename']) { |
|
107 | - echo "\t=> File was overwritten".PHP_EOL; |
|
108 | - } else { |
|
109 | - echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - ob_start(); |
|
114 | - |
|
115 | - return $fixed; |
|
116 | - |
|
117 | - }//end generateFileReport() |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * Prints all errors and warnings for each file processed. |
|
122 | - * |
|
123 | - * @param string $cachedData Any partial report data that was returned from |
|
124 | - * generateFileReport during the run. |
|
125 | - * @param int $totalFiles Total number of files processed during the run. |
|
126 | - * @param int $totalErrors Total number of errors found during the run. |
|
127 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
128 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
129 | - * @param boolean $showSources Show sources? |
|
130 | - * @param int $width Maximum allowed line width. |
|
131 | - * @param boolean $toScreen Is the report being printed to screen? |
|
132 | - * |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - public function generate( |
|
136 | - $cachedData, |
|
137 | - $totalFiles, |
|
138 | - $totalErrors, |
|
139 | - $totalWarnings, |
|
140 | - $totalFixable, |
|
141 | - $showSources=false, |
|
142 | - $width=80, |
|
143 | - $toScreen=true |
|
144 | - ) { |
|
145 | - echo $cachedData; |
|
146 | - echo "Fixed $totalFiles files".PHP_EOL; |
|
147 | - |
|
148 | - }//end generate() |
|
40 | + /** |
|
41 | + * Generate a partial report for a single processed file. |
|
42 | + * |
|
43 | + * Function should return TRUE if it printed or stored data about the file |
|
44 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
45 | + * its data should be counted in the grand totals. |
|
46 | + * |
|
47 | + * @param array $report Prepared report data. |
|
48 | + * @param PHP_CodeSniffer_File $phpcsFile The file being reported on. |
|
49 | + * @param boolean $showSources Show sources? |
|
50 | + * @param int $width Maximum allowed line width. |
|
51 | + * |
|
52 | + * @return boolean |
|
53 | + */ |
|
54 | + public function generateFileReport( |
|
55 | + $report, |
|
56 | + PHP_CodeSniffer_File $phpcsFile, |
|
57 | + $showSources=false, |
|
58 | + $width=80 |
|
59 | + ) { |
|
60 | + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
|
61 | + $errors = $phpcsFile->getFixableCount(); |
|
62 | + if ($errors !== 0) { |
|
63 | + if (empty($cliValues['files']) === false) { |
|
64 | + ob_end_clean(); |
|
65 | + $errors = $phpcsFile->getFixableCount(); |
|
66 | + $startTime = microtime(true); |
|
67 | + echo "\t=> Fixing file: $errors/$errors violations remaining"; |
|
68 | + } |
|
69 | + |
|
70 | + $fixed = $phpcsFile->fixer->fixFile(); |
|
71 | + } |
|
72 | + |
|
73 | + if (empty($cliValues['files']) === true) { |
|
74 | + // Replacing STDIN, so output current file to STDOUT |
|
75 | + // even if nothing was fixed. Exit here because we |
|
76 | + // can't process any more than 1 file in this setup. |
|
77 | + echo $phpcsFile->fixer->getContents(); |
|
78 | + ob_end_flush(); |
|
79 | + exit(1); |
|
80 | + } |
|
81 | + |
|
82 | + if ($errors === 0) { |
|
83 | + return false; |
|
84 | + } |
|
85 | + |
|
86 | + if ($fixed === false) { |
|
87 | + echo 'ERROR'; |
|
88 | + } else { |
|
89 | + echo 'DONE'; |
|
90 | + } |
|
91 | + |
|
92 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
93 | + if ($timeTaken < 1000) { |
|
94 | + $timeTaken = round($timeTaken); |
|
95 | + echo " in {$timeTaken}ms".PHP_EOL; |
|
96 | + } else { |
|
97 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
98 | + echo " in $timeTaken secs".PHP_EOL; |
|
99 | + } |
|
100 | + |
|
101 | + if ($fixed === true) { |
|
102 | + $newFilename = $report['filename'].$cliValues['phpcbf-suffix']; |
|
103 | + $newContent = $phpcsFile->fixer->getContents(); |
|
104 | + file_put_contents($newFilename, $newContent); |
|
105 | + |
|
106 | + if ($newFilename === $report['filename']) { |
|
107 | + echo "\t=> File was overwritten".PHP_EOL; |
|
108 | + } else { |
|
109 | + echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + ob_start(); |
|
114 | + |
|
115 | + return $fixed; |
|
116 | + |
|
117 | + }//end generateFileReport() |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * Prints all errors and warnings for each file processed. |
|
122 | + * |
|
123 | + * @param string $cachedData Any partial report data that was returned from |
|
124 | + * generateFileReport during the run. |
|
125 | + * @param int $totalFiles Total number of files processed during the run. |
|
126 | + * @param int $totalErrors Total number of errors found during the run. |
|
127 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
128 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
129 | + * @param boolean $showSources Show sources? |
|
130 | + * @param int $width Maximum allowed line width. |
|
131 | + * @param boolean $toScreen Is the report being printed to screen? |
|
132 | + * |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + public function generate( |
|
136 | + $cachedData, |
|
137 | + $totalFiles, |
|
138 | + $totalErrors, |
|
139 | + $totalWarnings, |
|
140 | + $totalFixable, |
|
141 | + $showSources=false, |
|
142 | + $width=80, |
|
143 | + $toScreen=true |
|
144 | + ) { |
|
145 | + echo $cachedData; |
|
146 | + echo "Fixed $totalFiles files".PHP_EOL; |
|
147 | + |
|
148 | + }//end generate() |
|
149 | 149 | |
150 | 150 | |
151 | 151 | }//end class |
@@ -54,59 +54,59 @@ discard block |
||
54 | 54 | public function generateFileReport( |
55 | 55 | $report, |
56 | 56 | PHP_CodeSniffer_File $phpcsFile, |
57 | - $showSources=false, |
|
58 | - $width=80 |
|
57 | + $showSources = false, |
|
58 | + $width = 80 |
|
59 | 59 | ) { |
60 | 60 | $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); |
61 | 61 | $errors = $phpcsFile->getFixableCount(); |
62 | - if ($errors !== 0) { |
|
63 | - if (empty($cliValues['files']) === false) { |
|
62 | + if ( $errors !== 0 ) { |
|
63 | + if ( empty( $cliValues[ 'files' ] ) === false ) { |
|
64 | 64 | ob_end_clean(); |
65 | 65 | $errors = $phpcsFile->getFixableCount(); |
66 | - $startTime = microtime(true); |
|
66 | + $startTime = microtime( true ); |
|
67 | 67 | echo "\t=> Fixing file: $errors/$errors violations remaining"; |
68 | 68 | } |
69 | 69 | |
70 | 70 | $fixed = $phpcsFile->fixer->fixFile(); |
71 | 71 | } |
72 | 72 | |
73 | - if (empty($cliValues['files']) === true) { |
|
73 | + if ( empty( $cliValues[ 'files' ] ) === true ) { |
|
74 | 74 | // Replacing STDIN, so output current file to STDOUT |
75 | 75 | // even if nothing was fixed. Exit here because we |
76 | 76 | // can't process any more than 1 file in this setup. |
77 | 77 | echo $phpcsFile->fixer->getContents(); |
78 | 78 | ob_end_flush(); |
79 | - exit(1); |
|
79 | + exit( 1 ); |
|
80 | 80 | } |
81 | 81 | |
82 | - if ($errors === 0) { |
|
82 | + if ( $errors === 0 ) { |
|
83 | 83 | return false; |
84 | 84 | } |
85 | 85 | |
86 | - if ($fixed === false) { |
|
86 | + if ( $fixed === false ) { |
|
87 | 87 | echo 'ERROR'; |
88 | 88 | } else { |
89 | 89 | echo 'DONE'; |
90 | 90 | } |
91 | 91 | |
92 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
93 | - if ($timeTaken < 1000) { |
|
94 | - $timeTaken = round($timeTaken); |
|
95 | - echo " in {$timeTaken}ms".PHP_EOL; |
|
92 | + $timeTaken = ( ( microtime( true ) - $startTime ) * 1000 ); |
|
93 | + if ( $timeTaken < 1000 ) { |
|
94 | + $timeTaken = round( $timeTaken ); |
|
95 | + echo " in {$timeTaken}ms" . PHP_EOL; |
|
96 | 96 | } else { |
97 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
98 | - echo " in $timeTaken secs".PHP_EOL; |
|
97 | + $timeTaken = round( ( $timeTaken / 1000 ), 2 ); |
|
98 | + echo " in $timeTaken secs" . PHP_EOL; |
|
99 | 99 | } |
100 | 100 | |
101 | - if ($fixed === true) { |
|
102 | - $newFilename = $report['filename'].$cliValues['phpcbf-suffix']; |
|
101 | + if ( $fixed === true ) { |
|
102 | + $newFilename = $report[ 'filename' ] . $cliValues[ 'phpcbf-suffix' ]; |
|
103 | 103 | $newContent = $phpcsFile->fixer->getContents(); |
104 | - file_put_contents($newFilename, $newContent); |
|
104 | + file_put_contents( $newFilename, $newContent ); |
|
105 | 105 | |
106 | - if ($newFilename === $report['filename']) { |
|
107 | - echo "\t=> File was overwritten".PHP_EOL; |
|
106 | + if ( $newFilename === $report[ 'filename' ] ) { |
|
107 | + echo "\t=> File was overwritten" . PHP_EOL; |
|
108 | 108 | } else { |
109 | - echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; |
|
109 | + echo "\t=> Fixed file written to " . basename( $newFilename ) . PHP_EOL; |
|
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
@@ -138,12 +138,12 @@ discard block |
||
138 | 138 | $totalErrors, |
139 | 139 | $totalWarnings, |
140 | 140 | $totalFixable, |
141 | - $showSources=false, |
|
142 | - $width=80, |
|
143 | - $toScreen=true |
|
141 | + $showSources = false, |
|
142 | + $width = 80, |
|
143 | + $toScreen = true |
|
144 | 144 | ) { |
145 | 145 | echo $cachedData; |
146 | - echo "Fixed $totalFiles files".PHP_EOL; |
|
146 | + echo "Fixed $totalFiles files" . PHP_EOL; |
|
147 | 147 | |
148 | 148 | }//end generate() |
149 | 149 |
@@ -33,8 +33,7 @@ |
||
33 | 33 | * @version Release: @package_version@ |
34 | 34 | * @link http://pear.php.net/package/PHP_CodeSniffer |
35 | 35 | */ |
36 | -class PHP_CodeSniffer_Reports_Cbf implements PHP_CodeSniffer_Report |
|
37 | -{ |
|
36 | +class PHP_CodeSniffer_Reports_Cbf implements PHP_CodeSniffer_Report { |
|
38 | 37 | |
39 | 38 | |
40 | 39 | /** |
@@ -51,7 +51,7 @@ |
||
51 | 51 | /** |
52 | 52 | * Registers the tokens that this sniff wants to listen for. |
53 | 53 | * |
54 | - * @return int[] |
|
54 | + * @return integer[] |
|
55 | 55 | */ |
56 | 56 | public function register() |
57 | 57 | { |
@@ -48,99 +48,99 @@ |
||
48 | 48 | { |
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * Registers the tokens that this sniff wants to listen for. |
|
53 | - * |
|
54 | - * @return int[] |
|
55 | - */ |
|
56 | - public function register() |
|
57 | - { |
|
58 | - return array(T_FOR); |
|
59 | - |
|
60 | - }//end register() |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Processes this test, when one of its tokens is encountered. |
|
65 | - * |
|
66 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
67 | - * @param int $stackPtr The position of the current token |
|
68 | - * in the stack passed in $tokens. |
|
69 | - * |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
73 | - { |
|
74 | - $tokens = $phpcsFile->getTokens(); |
|
75 | - $token = $tokens[$stackPtr]; |
|
76 | - |
|
77 | - // Skip for-loop without body. |
|
78 | - if (isset($token['scope_opener']) === false) { |
|
79 | - return; |
|
80 | - } |
|
81 | - |
|
82 | - // Find incrementors for outer loop. |
|
83 | - $outer = $this->findIncrementers($tokens, $token); |
|
84 | - |
|
85 | - // Skip if empty. |
|
86 | - if (count($outer) === 0) { |
|
87 | - return; |
|
88 | - } |
|
89 | - |
|
90 | - // Find nested for loops. |
|
91 | - $start = ++$token['scope_opener']; |
|
92 | - $end = --$token['scope_closer']; |
|
93 | - |
|
94 | - for (; $start <= $end; ++$start) { |
|
95 | - if ($tokens[$start]['code'] !== T_FOR) { |
|
96 | - continue; |
|
97 | - } |
|
98 | - |
|
99 | - $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
100 | - $diff = array_intersect($outer, $inner); |
|
101 | - |
|
102 | - if (count($diff) !== 0) { |
|
103 | - $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
104 | - $data = array(join(', ', $diff)); |
|
105 | - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - }//end process() |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Get all used variables in the incrementer part of a for statement. |
|
114 | - * |
|
115 | - * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
116 | - * @param array(string=>mixed) $token Current for loop token |
|
117 | - * |
|
118 | - * @return string[] List of all found incrementer variables. |
|
119 | - */ |
|
120 | - protected function findIncrementers(array $tokens, array $token) |
|
121 | - { |
|
122 | - // Skip invalid statement. |
|
123 | - if (isset($token['parenthesis_opener']) === false) { |
|
124 | - return array(); |
|
125 | - } |
|
126 | - |
|
127 | - $start = ++$token['parenthesis_opener']; |
|
128 | - $end = --$token['parenthesis_closer']; |
|
129 | - |
|
130 | - $incrementers = array(); |
|
131 | - $semicolons = 0; |
|
132 | - for ($next = $start; $next <= $end; ++$next) { |
|
133 | - $code = $tokens[$next]['code']; |
|
134 | - if ($code === T_SEMICOLON) { |
|
135 | - ++$semicolons; |
|
136 | - } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
137 | - $incrementers[] = $tokens[$next]['content']; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - return $incrementers; |
|
142 | - |
|
143 | - }//end findIncrementers() |
|
51 | + /** |
|
52 | + * Registers the tokens that this sniff wants to listen for. |
|
53 | + * |
|
54 | + * @return int[] |
|
55 | + */ |
|
56 | + public function register() |
|
57 | + { |
|
58 | + return array(T_FOR); |
|
59 | + |
|
60 | + }//end register() |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Processes this test, when one of its tokens is encountered. |
|
65 | + * |
|
66 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
67 | + * @param int $stackPtr The position of the current token |
|
68 | + * in the stack passed in $tokens. |
|
69 | + * |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
73 | + { |
|
74 | + $tokens = $phpcsFile->getTokens(); |
|
75 | + $token = $tokens[$stackPtr]; |
|
76 | + |
|
77 | + // Skip for-loop without body. |
|
78 | + if (isset($token['scope_opener']) === false) { |
|
79 | + return; |
|
80 | + } |
|
81 | + |
|
82 | + // Find incrementors for outer loop. |
|
83 | + $outer = $this->findIncrementers($tokens, $token); |
|
84 | + |
|
85 | + // Skip if empty. |
|
86 | + if (count($outer) === 0) { |
|
87 | + return; |
|
88 | + } |
|
89 | + |
|
90 | + // Find nested for loops. |
|
91 | + $start = ++$token['scope_opener']; |
|
92 | + $end = --$token['scope_closer']; |
|
93 | + |
|
94 | + for (; $start <= $end; ++$start) { |
|
95 | + if ($tokens[$start]['code'] !== T_FOR) { |
|
96 | + continue; |
|
97 | + } |
|
98 | + |
|
99 | + $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
100 | + $diff = array_intersect($outer, $inner); |
|
101 | + |
|
102 | + if (count($diff) !== 0) { |
|
103 | + $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
104 | + $data = array(join(', ', $diff)); |
|
105 | + $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + }//end process() |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Get all used variables in the incrementer part of a for statement. |
|
114 | + * |
|
115 | + * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
116 | + * @param array(string=>mixed) $token Current for loop token |
|
117 | + * |
|
118 | + * @return string[] List of all found incrementer variables. |
|
119 | + */ |
|
120 | + protected function findIncrementers(array $tokens, array $token) |
|
121 | + { |
|
122 | + // Skip invalid statement. |
|
123 | + if (isset($token['parenthesis_opener']) === false) { |
|
124 | + return array(); |
|
125 | + } |
|
126 | + |
|
127 | + $start = ++$token['parenthesis_opener']; |
|
128 | + $end = --$token['parenthesis_closer']; |
|
129 | + |
|
130 | + $incrementers = array(); |
|
131 | + $semicolons = 0; |
|
132 | + for ($next = $start; $next <= $end; ++$next) { |
|
133 | + $code = $tokens[$next]['code']; |
|
134 | + if ($code === T_SEMICOLON) { |
|
135 | + ++$semicolons; |
|
136 | + } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
137 | + $incrementers[] = $tokens[$next]['content']; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + return $incrementers; |
|
142 | + |
|
143 | + }//end findIncrementers() |
|
144 | 144 | |
145 | 145 | |
146 | 146 | }//end class |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | public function register() |
57 | 57 | { |
58 | - return array(T_FOR); |
|
58 | + return array( T_FOR ); |
|
59 | 59 | |
60 | 60 | }//end register() |
61 | 61 | |
@@ -69,40 +69,40 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return void |
71 | 71 | */ |
72 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
72 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
73 | 73 | { |
74 | 74 | $tokens = $phpcsFile->getTokens(); |
75 | - $token = $tokens[$stackPtr]; |
|
75 | + $token = $tokens[ $stackPtr ]; |
|
76 | 76 | |
77 | 77 | // Skip for-loop without body. |
78 | - if (isset($token['scope_opener']) === false) { |
|
78 | + if ( isset( $token[ 'scope_opener' ] ) === false ) { |
|
79 | 79 | return; |
80 | 80 | } |
81 | 81 | |
82 | 82 | // Find incrementors for outer loop. |
83 | - $outer = $this->findIncrementers($tokens, $token); |
|
83 | + $outer = $this->findIncrementers( $tokens, $token ); |
|
84 | 84 | |
85 | 85 | // Skip if empty. |
86 | - if (count($outer) === 0) { |
|
86 | + if ( count( $outer ) === 0 ) { |
|
87 | 87 | return; |
88 | 88 | } |
89 | 89 | |
90 | 90 | // Find nested for loops. |
91 | - $start = ++$token['scope_opener']; |
|
92 | - $end = --$token['scope_closer']; |
|
91 | + $start = ++$token[ 'scope_opener' ]; |
|
92 | + $end = --$token[ 'scope_closer' ]; |
|
93 | 93 | |
94 | - for (; $start <= $end; ++$start) { |
|
95 | - if ($tokens[$start]['code'] !== T_FOR) { |
|
94 | + for ( ; $start <= $end; ++$start ) { |
|
95 | + if ( $tokens[ $start ][ 'code' ] !== T_FOR ) { |
|
96 | 96 | continue; |
97 | 97 | } |
98 | 98 | |
99 | - $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
100 | - $diff = array_intersect($outer, $inner); |
|
99 | + $inner = $this->findIncrementers( $tokens, $tokens[ $start ] ); |
|
100 | + $diff = array_intersect( $outer, $inner ); |
|
101 | 101 | |
102 | - if (count($diff) !== 0) { |
|
102 | + if ( count( $diff ) !== 0 ) { |
|
103 | 103 | $error = 'Loop incrementor (%s) jumbling with inner loop'; |
104 | - $data = array(join(', ', $diff)); |
|
105 | - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
104 | + $data = array( join( ', ', $diff ) ); |
|
105 | + $phpcsFile->addWarning( $error, $stackPtr, 'Found', $data ); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | |
@@ -117,24 +117,24 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return string[] List of all found incrementer variables. |
119 | 119 | */ |
120 | - protected function findIncrementers(array $tokens, array $token) |
|
120 | + protected function findIncrementers( array $tokens, array $token ) |
|
121 | 121 | { |
122 | 122 | // Skip invalid statement. |
123 | - if (isset($token['parenthesis_opener']) === false) { |
|
123 | + if ( isset( $token[ 'parenthesis_opener' ] ) === false ) { |
|
124 | 124 | return array(); |
125 | 125 | } |
126 | 126 | |
127 | - $start = ++$token['parenthesis_opener']; |
|
128 | - $end = --$token['parenthesis_closer']; |
|
127 | + $start = ++$token[ 'parenthesis_opener' ]; |
|
128 | + $end = --$token[ 'parenthesis_closer' ]; |
|
129 | 129 | |
130 | 130 | $incrementers = array(); |
131 | 131 | $semicolons = 0; |
132 | - for ($next = $start; $next <= $end; ++$next) { |
|
133 | - $code = $tokens[$next]['code']; |
|
134 | - if ($code === T_SEMICOLON) { |
|
132 | + for ( $next = $start; $next <= $end; ++$next ) { |
|
133 | + $code = $tokens[ $next ][ 'code' ]; |
|
134 | + if ( $code === T_SEMICOLON ) { |
|
135 | 135 | ++$semicolons; |
136 | - } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
137 | - $incrementers[] = $tokens[$next]['content']; |
|
136 | + } else if ( $semicolons === 2 && $code === T_VARIABLE ) { |
|
137 | + $incrementers[ ] = $tokens[ $next ][ 'content' ]; |
|
138 | 138 | } |
139 | 139 | } |
140 | 140 |
@@ -44,8 +44,7 @@ discard block |
||
44 | 44 | * @version Release: @package_version@ |
45 | 45 | * @link http://pear.php.net/package/PHP_CodeSniffer |
46 | 46 | */ |
47 | -class Generic_Sniffs_CodeAnalysis_JumbledIncrementerSniff implements PHP_CodeSniffer_Sniff |
|
48 | -{ |
|
47 | +class Generic_Sniffs_CodeAnalysis_JumbledIncrementerSniff implements PHP_CodeSniffer_Sniff { |
|
49 | 48 | |
50 | 49 | |
51 | 50 | /** |
@@ -53,8 +52,7 @@ discard block |
||
53 | 52 | * |
54 | 53 | * @return int[] |
55 | 54 | */ |
56 | - public function register() |
|
57 | - { |
|
55 | + public function register() { |
|
58 | 56 | return array(T_FOR); |
59 | 57 | |
60 | 58 | }//end register() |
@@ -69,8 +67,7 @@ discard block |
||
69 | 67 | * |
70 | 68 | * @return void |
71 | 69 | */ |
72 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
73 | - { |
|
70 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
74 | 71 | $tokens = $phpcsFile->getTokens(); |
75 | 72 | $token = $tokens[$stackPtr]; |
76 | 73 | |
@@ -117,8 +114,7 @@ discard block |
||
117 | 114 | * |
118 | 115 | * @return string[] List of all found incrementer variables. |
119 | 116 | */ |
120 | - protected function findIncrementers(array $tokens, array $token) |
|
121 | - { |
|
117 | + protected function findIncrementers(array $tokens, array $token) { |
|
122 | 118 | // Skip invalid statement. |
123 | 119 | if (isset($token['parenthesis_opener']) === false) { |
124 | 120 | return array(); |
@@ -43,7 +43,7 @@ |
||
43 | 43 | /** |
44 | 44 | * Registers the tokens that this sniff wants to listen for. |
45 | 45 | * |
46 | - * @return int[] |
|
46 | + * @return integer[] |
|
47 | 47 | */ |
48 | 48 | public function register() |
49 | 49 | { |
@@ -40,139 +40,139 @@ |
||
40 | 40 | { |
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * Registers the tokens that this sniff wants to listen for. |
|
45 | - * |
|
46 | - * @return int[] |
|
47 | - */ |
|
48 | - public function register() |
|
49 | - { |
|
50 | - return array(T_FUNCTION); |
|
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 void |
|
63 | - */ |
|
64 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
65 | - { |
|
66 | - $tokens = $phpcsFile->getTokens(); |
|
67 | - $token = $tokens[$stackPtr]; |
|
68 | - |
|
69 | - // Skip function without body. |
|
70 | - if (isset($token['scope_opener']) === false) { |
|
71 | - return; |
|
72 | - } |
|
73 | - |
|
74 | - // Get function name. |
|
75 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
76 | - |
|
77 | - // Get all parameters from method signature. |
|
78 | - $signature = array(); |
|
79 | - foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) { |
|
80 | - $signature[] = $param['name']; |
|
81 | - } |
|
82 | - |
|
83 | - $next = ++$token['scope_opener']; |
|
84 | - $end = --$token['scope_closer']; |
|
85 | - |
|
86 | - for (; $next <= $end; ++$next) { |
|
87 | - $code = $tokens[$next]['code']; |
|
88 | - |
|
89 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === true) { |
|
90 | - continue; |
|
91 | - } else if ($code === T_RETURN) { |
|
92 | - continue; |
|
93 | - } |
|
94 | - |
|
95 | - break; |
|
96 | - } |
|
97 | - |
|
98 | - // Any token except 'parent' indicates correct code. |
|
99 | - if ($tokens[$next]['code'] !== T_PARENT) { |
|
100 | - return; |
|
101 | - } |
|
102 | - |
|
103 | - // Find next non empty token index, should be double colon. |
|
104 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
105 | - |
|
106 | - // Skip for invalid code. |
|
107 | - if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) { |
|
108 | - return; |
|
109 | - } |
|
110 | - |
|
111 | - // Find next non empty token index, should be the function name. |
|
112 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
113 | - |
|
114 | - // Skip for invalid code or other method. |
|
115 | - if ($next === false || $tokens[$next]['content'] !== $methodName) { |
|
116 | - return; |
|
117 | - } |
|
118 | - |
|
119 | - // Find next non empty token index, should be the open parenthesis. |
|
120 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
121 | - |
|
122 | - // Skip for invalid code. |
|
123 | - if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { |
|
124 | - return; |
|
125 | - } |
|
126 | - |
|
127 | - $validParameterTypes = array( |
|
128 | - T_VARIABLE, |
|
129 | - T_LNUMBER, |
|
130 | - T_CONSTANT_ENCAPSED_STRING, |
|
131 | - ); |
|
132 | - |
|
133 | - $parameters = array(''); |
|
134 | - $parenthesisCount = 1; |
|
135 | - $count = count($tokens); |
|
136 | - for (++$next; $next < $count; ++$next) { |
|
137 | - $code = $tokens[$next]['code']; |
|
138 | - |
|
139 | - if ($code === T_OPEN_PARENTHESIS) { |
|
140 | - ++$parenthesisCount; |
|
141 | - } else if ($code === T_CLOSE_PARENTHESIS) { |
|
142 | - --$parenthesisCount; |
|
143 | - } else if ($parenthesisCount === 1 && $code === T_COMMA) { |
|
144 | - $parameters[] = ''; |
|
145 | - } else if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === false) { |
|
146 | - $parameters[(count($parameters) - 1)] .= $tokens[$next]['content']; |
|
147 | - } |
|
148 | - |
|
149 | - if ($parenthesisCount === 0) { |
|
150 | - break; |
|
151 | - } |
|
152 | - }//end for |
|
153 | - |
|
154 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
155 | - if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) { |
|
156 | - return; |
|
157 | - } |
|
158 | - |
|
159 | - // Check rest of the scope. |
|
160 | - for (++$next; $next <= $end; ++$next) { |
|
161 | - $code = $tokens[$next]['code']; |
|
162 | - // Skip for any other content. |
|
163 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === false) { |
|
164 | - return; |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - $parameters = array_map('trim', $parameters); |
|
169 | - $parameters = array_filter($parameters); |
|
170 | - |
|
171 | - if (count($parameters) === count($signature) && $parameters === $signature) { |
|
172 | - $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found'); |
|
173 | - } |
|
174 | - |
|
175 | - }//end process() |
|
43 | + /** |
|
44 | + * Registers the tokens that this sniff wants to listen for. |
|
45 | + * |
|
46 | + * @return int[] |
|
47 | + */ |
|
48 | + public function register() |
|
49 | + { |
|
50 | + return array(T_FUNCTION); |
|
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 void |
|
63 | + */ |
|
64 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
65 | + { |
|
66 | + $tokens = $phpcsFile->getTokens(); |
|
67 | + $token = $tokens[$stackPtr]; |
|
68 | + |
|
69 | + // Skip function without body. |
|
70 | + if (isset($token['scope_opener']) === false) { |
|
71 | + return; |
|
72 | + } |
|
73 | + |
|
74 | + // Get function name. |
|
75 | + $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
76 | + |
|
77 | + // Get all parameters from method signature. |
|
78 | + $signature = array(); |
|
79 | + foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) { |
|
80 | + $signature[] = $param['name']; |
|
81 | + } |
|
82 | + |
|
83 | + $next = ++$token['scope_opener']; |
|
84 | + $end = --$token['scope_closer']; |
|
85 | + |
|
86 | + for (; $next <= $end; ++$next) { |
|
87 | + $code = $tokens[$next]['code']; |
|
88 | + |
|
89 | + if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === true) { |
|
90 | + continue; |
|
91 | + } else if ($code === T_RETURN) { |
|
92 | + continue; |
|
93 | + } |
|
94 | + |
|
95 | + break; |
|
96 | + } |
|
97 | + |
|
98 | + // Any token except 'parent' indicates correct code. |
|
99 | + if ($tokens[$next]['code'] !== T_PARENT) { |
|
100 | + return; |
|
101 | + } |
|
102 | + |
|
103 | + // Find next non empty token index, should be double colon. |
|
104 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
105 | + |
|
106 | + // Skip for invalid code. |
|
107 | + if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) { |
|
108 | + return; |
|
109 | + } |
|
110 | + |
|
111 | + // Find next non empty token index, should be the function name. |
|
112 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
113 | + |
|
114 | + // Skip for invalid code or other method. |
|
115 | + if ($next === false || $tokens[$next]['content'] !== $methodName) { |
|
116 | + return; |
|
117 | + } |
|
118 | + |
|
119 | + // Find next non empty token index, should be the open parenthesis. |
|
120 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
121 | + |
|
122 | + // Skip for invalid code. |
|
123 | + if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { |
|
124 | + return; |
|
125 | + } |
|
126 | + |
|
127 | + $validParameterTypes = array( |
|
128 | + T_VARIABLE, |
|
129 | + T_LNUMBER, |
|
130 | + T_CONSTANT_ENCAPSED_STRING, |
|
131 | + ); |
|
132 | + |
|
133 | + $parameters = array(''); |
|
134 | + $parenthesisCount = 1; |
|
135 | + $count = count($tokens); |
|
136 | + for (++$next; $next < $count; ++$next) { |
|
137 | + $code = $tokens[$next]['code']; |
|
138 | + |
|
139 | + if ($code === T_OPEN_PARENTHESIS) { |
|
140 | + ++$parenthesisCount; |
|
141 | + } else if ($code === T_CLOSE_PARENTHESIS) { |
|
142 | + --$parenthesisCount; |
|
143 | + } else if ($parenthesisCount === 1 && $code === T_COMMA) { |
|
144 | + $parameters[] = ''; |
|
145 | + } else if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === false) { |
|
146 | + $parameters[(count($parameters) - 1)] .= $tokens[$next]['content']; |
|
147 | + } |
|
148 | + |
|
149 | + if ($parenthesisCount === 0) { |
|
150 | + break; |
|
151 | + } |
|
152 | + }//end for |
|
153 | + |
|
154 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
155 | + if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) { |
|
156 | + return; |
|
157 | + } |
|
158 | + |
|
159 | + // Check rest of the scope. |
|
160 | + for (++$next; $next <= $end; ++$next) { |
|
161 | + $code = $tokens[$next]['code']; |
|
162 | + // Skip for any other content. |
|
163 | + if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === false) { |
|
164 | + return; |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + $parameters = array_map('trim', $parameters); |
|
169 | + $parameters = array_filter($parameters); |
|
170 | + |
|
171 | + if (count($parameters) === count($signature) && $parameters === $signature) { |
|
172 | + $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found'); |
|
173 | + } |
|
174 | + |
|
175 | + }//end process() |
|
176 | 176 | |
177 | 177 | |
178 | 178 | }//end class |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public function register() |
49 | 49 | { |
50 | - return array(T_FUNCTION); |
|
50 | + return array( T_FUNCTION ); |
|
51 | 51 | |
52 | 52 | }//end register() |
53 | 53 | |
@@ -61,34 +61,34 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @return void |
63 | 63 | */ |
64 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
64 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
65 | 65 | { |
66 | 66 | $tokens = $phpcsFile->getTokens(); |
67 | - $token = $tokens[$stackPtr]; |
|
67 | + $token = $tokens[ $stackPtr ]; |
|
68 | 68 | |
69 | 69 | // Skip function without body. |
70 | - if (isset($token['scope_opener']) === false) { |
|
70 | + if ( isset( $token[ 'scope_opener' ] ) === false ) { |
|
71 | 71 | return; |
72 | 72 | } |
73 | 73 | |
74 | 74 | // Get function name. |
75 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
75 | + $methodName = $phpcsFile->getDeclarationName( $stackPtr ); |
|
76 | 76 | |
77 | 77 | // Get all parameters from method signature. |
78 | 78 | $signature = array(); |
79 | - foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) { |
|
80 | - $signature[] = $param['name']; |
|
79 | + foreach ( $phpcsFile->getMethodParameters( $stackPtr ) as $param ) { |
|
80 | + $signature[ ] = $param[ 'name' ]; |
|
81 | 81 | } |
82 | 82 | |
83 | - $next = ++$token['scope_opener']; |
|
84 | - $end = --$token['scope_closer']; |
|
83 | + $next = ++$token[ 'scope_opener' ]; |
|
84 | + $end = --$token[ 'scope_closer' ]; |
|
85 | 85 | |
86 | - for (; $next <= $end; ++$next) { |
|
87 | - $code = $tokens[$next]['code']; |
|
86 | + for ( ; $next <= $end; ++$next ) { |
|
87 | + $code = $tokens[ $next ][ 'code' ]; |
|
88 | 88 | |
89 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === true) { |
|
89 | + if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $code ] ) === true ) { |
|
90 | 90 | continue; |
91 | - } else if ($code === T_RETURN) { |
|
91 | + } else if ( $code === T_RETURN ) { |
|
92 | 92 | continue; |
93 | 93 | } |
94 | 94 | |
@@ -96,31 +96,31 @@ discard block |
||
96 | 96 | } |
97 | 97 | |
98 | 98 | // Any token except 'parent' indicates correct code. |
99 | - if ($tokens[$next]['code'] !== T_PARENT) { |
|
99 | + if ( $tokens[ $next ][ 'code' ] !== T_PARENT ) { |
|
100 | 100 | return; |
101 | 101 | } |
102 | 102 | |
103 | 103 | // Find next non empty token index, should be double colon. |
104 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
104 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), null, true ); |
|
105 | 105 | |
106 | 106 | // Skip for invalid code. |
107 | - if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) { |
|
107 | + if ( $next === false || $tokens[ $next ][ 'code' ] !== T_DOUBLE_COLON ) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | |
111 | 111 | // Find next non empty token index, should be the function name. |
112 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
112 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), null, true ); |
|
113 | 113 | |
114 | 114 | // Skip for invalid code or other method. |
115 | - if ($next === false || $tokens[$next]['content'] !== $methodName) { |
|
115 | + if ( $next === false || $tokens[ $next ][ 'content' ] !== $methodName ) { |
|
116 | 116 | return; |
117 | 117 | } |
118 | 118 | |
119 | 119 | // Find next non empty token index, should be the open parenthesis. |
120 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
120 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), null, true ); |
|
121 | 121 | |
122 | 122 | // Skip for invalid code. |
123 | - if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { |
|
123 | + if ( $next === false || $tokens[ $next ][ 'code' ] !== T_OPEN_PARENTHESIS ) { |
|
124 | 124 | return; |
125 | 125 | } |
126 | 126 | |
@@ -130,46 +130,46 @@ discard block |
||
130 | 130 | T_CONSTANT_ENCAPSED_STRING, |
131 | 131 | ); |
132 | 132 | |
133 | - $parameters = array(''); |
|
133 | + $parameters = array( '' ); |
|
134 | 134 | $parenthesisCount = 1; |
135 | - $count = count($tokens); |
|
135 | + $count = count( $tokens ); |
|
136 | 136 | for (++$next; $next < $count; ++$next) { |
137 | - $code = $tokens[$next]['code']; |
|
137 | + $code = $tokens[ $next ][ 'code' ]; |
|
138 | 138 | |
139 | - if ($code === T_OPEN_PARENTHESIS) { |
|
139 | + if ( $code === T_OPEN_PARENTHESIS ) { |
|
140 | 140 | ++$parenthesisCount; |
141 | - } else if ($code === T_CLOSE_PARENTHESIS) { |
|
141 | + } else if ( $code === T_CLOSE_PARENTHESIS ) { |
|
142 | 142 | --$parenthesisCount; |
143 | - } else if ($parenthesisCount === 1 && $code === T_COMMA) { |
|
144 | - $parameters[] = ''; |
|
145 | - } else if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === false) { |
|
146 | - $parameters[(count($parameters) - 1)] .= $tokens[$next]['content']; |
|
143 | + } else if ( $parenthesisCount === 1 && $code === T_COMMA ) { |
|
144 | + $parameters[ ] = ''; |
|
145 | + } else if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $code ] ) === false ) { |
|
146 | + $parameters[ ( count( $parameters ) - 1 ) ] .= $tokens[ $next ][ 'content' ]; |
|
147 | 147 | } |
148 | 148 | |
149 | - if ($parenthesisCount === 0) { |
|
149 | + if ( $parenthesisCount === 0 ) { |
|
150 | 150 | break; |
151 | 151 | } |
152 | 152 | }//end for |
153 | 153 | |
154 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
155 | - if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) { |
|
154 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), null, true ); |
|
155 | + if ( $next === false || $tokens[ $next ][ 'code' ] !== T_SEMICOLON ) { |
|
156 | 156 | return; |
157 | 157 | } |
158 | 158 | |
159 | 159 | // Check rest of the scope. |
160 | 160 | for (++$next; $next <= $end; ++$next) { |
161 | - $code = $tokens[$next]['code']; |
|
161 | + $code = $tokens[ $next ][ 'code' ]; |
|
162 | 162 | // Skip for any other content. |
163 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$code]) === false) { |
|
163 | + if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $code ] ) === false ) { |
|
164 | 164 | return; |
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
168 | - $parameters = array_map('trim', $parameters); |
|
169 | - $parameters = array_filter($parameters); |
|
168 | + $parameters = array_map( 'trim', $parameters ); |
|
169 | + $parameters = array_filter( $parameters ); |
|
170 | 170 | |
171 | - if (count($parameters) === count($signature) && $parameters === $signature) { |
|
172 | - $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found'); |
|
171 | + if ( count( $parameters ) === count( $signature ) && $parameters === $signature ) { |
|
172 | + $phpcsFile->addWarning( 'Possible useless method overriding detected', $stackPtr, 'Found' ); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | }//end process() |
@@ -36,8 +36,7 @@ discard block |
||
36 | 36 | * @version Release: @package_version@ |
37 | 37 | * @link http://pear.php.net/package/PHP_CodeSniffer |
38 | 38 | */ |
39 | -class Generic_Sniffs_CodeAnalysis_UselessOverridingMethodSniff implements PHP_CodeSniffer_Sniff |
|
40 | -{ |
|
39 | +class Generic_Sniffs_CodeAnalysis_UselessOverridingMethodSniff implements PHP_CodeSniffer_Sniff { |
|
41 | 40 | |
42 | 41 | |
43 | 42 | /** |
@@ -45,8 +44,7 @@ discard block |
||
45 | 44 | * |
46 | 45 | * @return int[] |
47 | 46 | */ |
48 | - public function register() |
|
49 | - { |
|
47 | + public function register() { |
|
50 | 48 | return array(T_FUNCTION); |
51 | 49 | |
52 | 50 | }//end register() |
@@ -61,8 +59,7 @@ discard block |
||
61 | 59 | * |
62 | 60 | * @return void |
63 | 61 | */ |
64 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
65 | - { |
|
62 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
66 | 63 | $tokens = $phpcsFile->getTokens(); |
67 | 64 | $token = $tokens[$stackPtr]; |
68 | 65 |
@@ -40,7 +40,7 @@ |
||
40 | 40 | /** |
41 | 41 | * Returns an array of tokens this test wants to listen for. |
42 | 42 | * |
43 | - * @return array |
|
43 | + * @return string[] |
|
44 | 44 | */ |
45 | 45 | public function register() |
46 | 46 | { |
@@ -26,325 +26,325 @@ |
||
26 | 26 | class Generic_Sniffs_Commenting_DocCommentSniff implements PHP_CodeSniffer_Sniff |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * A list of tokenizers this sniff supports. |
|
31 | - * |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - public $supportedTokenizers = array( |
|
35 | - 'PHP', |
|
36 | - 'JS', |
|
37 | - ); |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns an array of tokens this test wants to listen for. |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function register() |
|
46 | - { |
|
47 | - return array(T_DOC_COMMENT_OPEN_TAG); |
|
48 | - |
|
49 | - }//end register() |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * Processes this test, when one of its tokens is encountered. |
|
54 | - * |
|
55 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
56 | - * @param int $stackPtr The position of the current token |
|
57 | - * in the stack passed in $tokens. |
|
58 | - * |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
62 | - { |
|
63 | - $tokens = $phpcsFile->getTokens(); |
|
64 | - $commentStart = $stackPtr; |
|
65 | - $commentEnd = $tokens[$stackPtr]['comment_closer']; |
|
66 | - |
|
67 | - $empty = array( |
|
68 | - T_DOC_COMMENT_WHITESPACE, |
|
69 | - T_DOC_COMMENT_STAR, |
|
70 | - ); |
|
71 | - |
|
72 | - $short = $phpcsFile->findNext($empty, ($stackPtr + 1), $commentEnd, true); |
|
73 | - if ($short === false) { |
|
74 | - // No content at all. |
|
75 | - $error = 'Doc comment is empty'; |
|
76 | - $phpcsFile->addError($error, $stackPtr, 'Empty'); |
|
77 | - return; |
|
78 | - } |
|
79 | - |
|
80 | - // The first line of the comment should just be the /** code. |
|
81 | - if ($tokens[$short]['line'] === $tokens[$stackPtr]['line']) { |
|
82 | - $error = 'The open comment tag must be the only content on the line'; |
|
83 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen'); |
|
84 | - if ($fix === true) { |
|
85 | - $phpcsFile->fixer->beginChangeset(); |
|
86 | - $phpcsFile->fixer->addNewline($stackPtr); |
|
87 | - $phpcsFile->fixer->addContentBefore($short, '* '); |
|
88 | - $phpcsFile->fixer->endChangeset(); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - // The last line of the comment should just be the */ code. |
|
93 | - $prev = $phpcsFile->findPrevious($empty, ($commentEnd - 1), $stackPtr, true); |
|
94 | - if ($tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
|
95 | - $error = 'The close comment tag must be the only content on the line'; |
|
96 | - $fix = $phpcsFile->addFixableError($error, $commentEnd, 'ContentBeforeClose'); |
|
97 | - if ($fix === true) { |
|
98 | - $phpcsFile->fixer->addNewlineBefore($commentEnd); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - // Check for additional blank lines at the end of the comment. |
|
103 | - if ($tokens[$prev]['line'] < ($tokens[$commentEnd]['line'] - 1)) { |
|
104 | - $error = 'Additional blank lines found at end of doc comment'; |
|
105 | - $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter'); |
|
106 | - if ($fix === true) { |
|
107 | - $phpcsFile->fixer->beginChangeset(); |
|
108 | - for ($i = ($prev + 1); $i < $commentEnd; $i++) { |
|
109 | - if ($tokens[($i + 1)]['line'] === $tokens[$commentEnd]['line']) { |
|
110 | - break; |
|
111 | - } |
|
112 | - |
|
113 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
114 | - } |
|
115 | - |
|
116 | - $phpcsFile->fixer->endChangeset(); |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - // Check for a comment description. |
|
121 | - if ($tokens[$short]['code'] !== T_DOC_COMMENT_STRING) { |
|
122 | - $error = 'Missing short description in doc comment'; |
|
123 | - $phpcsFile->addError($error, $stackPtr, 'MissingShort'); |
|
124 | - return; |
|
125 | - } |
|
126 | - |
|
127 | - // No extra newline before short description. |
|
128 | - if ($tokens[$short]['line'] !== ($tokens[$stackPtr]['line'] + 1)) { |
|
129 | - $error = 'Doc comment short description must be on the first line'; |
|
130 | - $fix = $phpcsFile->addFixableError($error, $short, 'SpacingBeforeShort'); |
|
131 | - if ($fix === true) { |
|
132 | - $phpcsFile->fixer->beginChangeset(); |
|
133 | - for ($i = $stackPtr; $i < $short; $i++) { |
|
134 | - if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) { |
|
135 | - continue; |
|
136 | - } else if ($tokens[$i]['line'] === $tokens[$short]['line']) { |
|
137 | - break; |
|
138 | - } |
|
139 | - |
|
140 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
141 | - } |
|
142 | - |
|
143 | - $phpcsFile->fixer->endChangeset(); |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - // Account for the fact that a short description might cover |
|
148 | - // multiple lines. |
|
149 | - $shortContent = $tokens[$short]['content']; |
|
150 | - $shortEnd = $short; |
|
151 | - for ($i = ($short + 1); $i < $commentEnd; $i++) { |
|
152 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
|
153 | - if ($tokens[$i]['line'] === ($tokens[$shortEnd]['line'] + 1)) { |
|
154 | - $shortContent .= $tokens[$i]['content']; |
|
155 | - $shortEnd = $i; |
|
156 | - } else { |
|
157 | - break; |
|
158 | - } |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - if (preg_match('/^\p{Ll}/u', $shortContent) === 1) { |
|
163 | - $error = 'Doc comment short description must start with a capital letter'; |
|
164 | - $phpcsFile->addError($error, $short, 'ShortNotCapital'); |
|
165 | - } |
|
166 | - |
|
167 | - $long = $phpcsFile->findNext($empty, ($shortEnd + 1), ($commentEnd - 1), true); |
|
168 | - if ($long !== false && $tokens[$long]['code'] === T_DOC_COMMENT_STRING) { |
|
169 | - if ($tokens[$long]['line'] !== ($tokens[$shortEnd]['line'] + 2)) { |
|
170 | - $error = 'There must be exactly one blank line between descriptions in a doc comment'; |
|
171 | - $fix = $phpcsFile->addFixableError($error, $long, 'SpacingBetween'); |
|
172 | - if ($fix === true) { |
|
173 | - $phpcsFile->fixer->beginChangeset(); |
|
174 | - for ($i = ($shortEnd + 1); $i < $long; $i++) { |
|
175 | - if ($tokens[$i]['line'] === $tokens[$shortEnd]['line']) { |
|
176 | - continue; |
|
177 | - } else if ($tokens[$i]['line'] === ($tokens[$long]['line'] - 1)) { |
|
178 | - break; |
|
179 | - } |
|
180 | - |
|
181 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
182 | - } |
|
183 | - |
|
184 | - $phpcsFile->fixer->endChangeset(); |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - if (preg_match('/^\p{Ll}/u', $tokens[$long]['content']) === 1) { |
|
189 | - $error = 'Doc comment long description must start with a capital letter'; |
|
190 | - $phpcsFile->addError($error, $long, 'LongNotCapital'); |
|
191 | - } |
|
192 | - }//end if |
|
193 | - |
|
194 | - if (empty($tokens[$commentStart]['comment_tags']) === true) { |
|
195 | - // No tags in the comment. |
|
196 | - return; |
|
197 | - } |
|
198 | - |
|
199 | - $firstTag = $tokens[$commentStart]['comment_tags'][0]; |
|
200 | - $prev = $phpcsFile->findPrevious($empty, ($firstTag - 1), $stackPtr, true); |
|
201 | - if ($tokens[$firstTag]['line'] !== ($tokens[$prev]['line'] + 2)) { |
|
202 | - $error = 'There must be exactly one blank line before the tags in a doc comment'; |
|
203 | - $fix = $phpcsFile->addFixableError($error, $firstTag, 'SpacingBeforeTags'); |
|
204 | - if ($fix === true) { |
|
205 | - $phpcsFile->fixer->beginChangeset(); |
|
206 | - for ($i = ($prev + 1); $i < $firstTag; $i++) { |
|
207 | - if ($tokens[$i]['line'] === $tokens[$firstTag]['line']) { |
|
208 | - break; |
|
209 | - } |
|
210 | - |
|
211 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
212 | - } |
|
213 | - |
|
214 | - $indent = str_repeat(' ', $tokens[$stackPtr]['column']); |
|
215 | - $phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar.$indent.'*'.$phpcsFile->eolChar); |
|
216 | - $phpcsFile->fixer->endChangeset(); |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - // Break out the tags into groups and check alignment within each. |
|
221 | - // A tag group is one where there are no blank lines between tags. |
|
222 | - // The param tag group is special as it requires all @param tags to be inside. |
|
223 | - $tagGroups = array(); |
|
224 | - $groupid = 0; |
|
225 | - $paramGroupid = null; |
|
226 | - foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
|
227 | - if ($pos > 0) { |
|
228 | - $prev = $phpcsFile->findPrevious( |
|
229 | - T_DOC_COMMENT_STRING, |
|
230 | - ($tag - 1), |
|
231 | - $tokens[$commentStart]['comment_tags'][($pos - 1)] |
|
232 | - ); |
|
233 | - |
|
234 | - if ($prev === false) { |
|
235 | - $prev = $tokens[$commentStart]['comment_tags'][($pos - 1)]; |
|
236 | - } |
|
237 | - |
|
238 | - if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 1)) { |
|
239 | - $groupid++; |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - if ($tokens[$tag]['content'] === '@param') { |
|
244 | - if (($paramGroupid === null |
|
245 | - && empty($tagGroups[$groupid]) === false) |
|
246 | - || ($paramGroupid !== null |
|
247 | - && $paramGroupid !== $groupid) |
|
248 | - ) { |
|
249 | - $error = 'Parameter tags must be grouped together in a doc comment'; |
|
250 | - $phpcsFile->addError($error, $tag, 'ParamGroup'); |
|
251 | - } |
|
252 | - |
|
253 | - if ($paramGroupid === null) { |
|
254 | - $paramGroupid = $groupid; |
|
255 | - } |
|
256 | - } else if ($groupid === $paramGroupid) { |
|
257 | - $error = 'Tag cannot be grouped with parameter tags in a doc comment'; |
|
258 | - $phpcsFile->addError($error, $tag, 'NonParamGroup'); |
|
259 | - }//end if |
|
260 | - |
|
261 | - $tagGroups[$groupid][] = $tag; |
|
262 | - }//end foreach |
|
263 | - |
|
264 | - foreach ($tagGroups as $group) { |
|
265 | - $maxLength = 0; |
|
266 | - $paddings = array(); |
|
267 | - foreach ($group as $pos => $tag) { |
|
268 | - $tagLength = strlen($tokens[$tag]['content']); |
|
269 | - if ($tagLength > $maxLength) { |
|
270 | - $maxLength = $tagLength; |
|
271 | - } |
|
272 | - |
|
273 | - // Check for a value. No value means no padding needed. |
|
274 | - $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
275 | - if ($string !== false && $tokens[$string]['line'] === $tokens[$tag]['line']) { |
|
276 | - $paddings[$tag] = strlen($tokens[($tag + 1)]['content']); |
|
277 | - } |
|
278 | - } |
|
279 | - |
|
280 | - // Check that there was single blank line after the tag block |
|
281 | - // but account for a multi-line tag comments. |
|
282 | - $lastTag = $group[$pos]; |
|
283 | - $next = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($lastTag + 3), $commentEnd); |
|
284 | - if ($next !== false) { |
|
285 | - $prev = $phpcsFile->findPrevious(array(T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING), ($next - 1), $commentStart); |
|
286 | - if ($tokens[$next]['line'] !== ($tokens[$prev]['line'] + 2)) { |
|
287 | - $error = 'There must be a single blank line after a tag group'; |
|
288 | - $fix = $phpcsFile->addFixableError($error, $lastTag, 'SpacingAfterTagGroup'); |
|
289 | - if ($fix === true) { |
|
290 | - $phpcsFile->fixer->beginChangeset(); |
|
291 | - for ($i = ($prev + 1); $i < $next; $i++) { |
|
292 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
293 | - break; |
|
294 | - } |
|
295 | - |
|
296 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
297 | - } |
|
298 | - |
|
299 | - $indent = str_repeat(' ', $tokens[$stackPtr]['column']); |
|
300 | - $phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar.$indent.'*'.$phpcsFile->eolChar); |
|
301 | - $phpcsFile->fixer->endChangeset(); |
|
302 | - } |
|
303 | - } |
|
304 | - }//end if |
|
305 | - |
|
306 | - // Now check paddings. |
|
307 | - foreach ($paddings as $tag => $padding) { |
|
308 | - $required = ($maxLength - strlen($tokens[$tag]['content']) + 1); |
|
309 | - |
|
310 | - if ($padding !== $required) { |
|
311 | - $error = 'Tag value indented incorrectly; expected %s spaces but found %s'; |
|
312 | - $data = array( |
|
313 | - $required, |
|
314 | - $padding, |
|
315 | - ); |
|
316 | - |
|
317 | - $fix = $phpcsFile->addFixableError($error, ($tag + 1), 'TagValueIndent', $data); |
|
318 | - if ($fix === true) { |
|
319 | - $phpcsFile->fixer->replaceToken(($tag + 1), str_repeat(' ', $required)); |
|
320 | - } |
|
321 | - } |
|
322 | - } |
|
323 | - }//end foreach |
|
324 | - |
|
325 | - // If there is a param group, it needs to be first. |
|
326 | - if ($paramGroupid !== null && $paramGroupid !== 0) { |
|
327 | - $error = 'Parameter tags must be defined first in a doc comment'; |
|
328 | - $phpcsFile->addError($error, $tagGroups[$paramGroupid][0], 'ParamNotFirst'); |
|
329 | - } |
|
330 | - |
|
331 | - $foundTags = array(); |
|
332 | - foreach ($tokens[$stackPtr]['comment_tags'] as $pos => $tag) { |
|
333 | - $tagName = $tokens[$tag]['content']; |
|
334 | - if (isset($foundTags[$tagName]) === true) { |
|
335 | - $lastTag = $tokens[$stackPtr]['comment_tags'][($pos - 1)]; |
|
336 | - if ($tokens[$lastTag]['content'] !== $tagName) { |
|
337 | - $error = 'Tags must be grouped together in a doc comment'; |
|
338 | - $phpcsFile->addError($error, $tag, 'TagsNotGrouped'); |
|
339 | - } |
|
340 | - |
|
341 | - continue; |
|
342 | - } |
|
343 | - |
|
344 | - $foundTags[$tagName] = true; |
|
345 | - } |
|
346 | - |
|
347 | - }//end process() |
|
29 | + /** |
|
30 | + * A list of tokenizers this sniff supports. |
|
31 | + * |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + public $supportedTokenizers = array( |
|
35 | + 'PHP', |
|
36 | + 'JS', |
|
37 | + ); |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns an array of tokens this test wants to listen for. |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function register() |
|
46 | + { |
|
47 | + return array(T_DOC_COMMENT_OPEN_TAG); |
|
48 | + |
|
49 | + }//end register() |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * Processes this test, when one of its tokens is encountered. |
|
54 | + * |
|
55 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
56 | + * @param int $stackPtr The position of the current token |
|
57 | + * in the stack passed in $tokens. |
|
58 | + * |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
62 | + { |
|
63 | + $tokens = $phpcsFile->getTokens(); |
|
64 | + $commentStart = $stackPtr; |
|
65 | + $commentEnd = $tokens[$stackPtr]['comment_closer']; |
|
66 | + |
|
67 | + $empty = array( |
|
68 | + T_DOC_COMMENT_WHITESPACE, |
|
69 | + T_DOC_COMMENT_STAR, |
|
70 | + ); |
|
71 | + |
|
72 | + $short = $phpcsFile->findNext($empty, ($stackPtr + 1), $commentEnd, true); |
|
73 | + if ($short === false) { |
|
74 | + // No content at all. |
|
75 | + $error = 'Doc comment is empty'; |
|
76 | + $phpcsFile->addError($error, $stackPtr, 'Empty'); |
|
77 | + return; |
|
78 | + } |
|
79 | + |
|
80 | + // The first line of the comment should just be the /** code. |
|
81 | + if ($tokens[$short]['line'] === $tokens[$stackPtr]['line']) { |
|
82 | + $error = 'The open comment tag must be the only content on the line'; |
|
83 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen'); |
|
84 | + if ($fix === true) { |
|
85 | + $phpcsFile->fixer->beginChangeset(); |
|
86 | + $phpcsFile->fixer->addNewline($stackPtr); |
|
87 | + $phpcsFile->fixer->addContentBefore($short, '* '); |
|
88 | + $phpcsFile->fixer->endChangeset(); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + // The last line of the comment should just be the */ code. |
|
93 | + $prev = $phpcsFile->findPrevious($empty, ($commentEnd - 1), $stackPtr, true); |
|
94 | + if ($tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
|
95 | + $error = 'The close comment tag must be the only content on the line'; |
|
96 | + $fix = $phpcsFile->addFixableError($error, $commentEnd, 'ContentBeforeClose'); |
|
97 | + if ($fix === true) { |
|
98 | + $phpcsFile->fixer->addNewlineBefore($commentEnd); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + // Check for additional blank lines at the end of the comment. |
|
103 | + if ($tokens[$prev]['line'] < ($tokens[$commentEnd]['line'] - 1)) { |
|
104 | + $error = 'Additional blank lines found at end of doc comment'; |
|
105 | + $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter'); |
|
106 | + if ($fix === true) { |
|
107 | + $phpcsFile->fixer->beginChangeset(); |
|
108 | + for ($i = ($prev + 1); $i < $commentEnd; $i++) { |
|
109 | + if ($tokens[($i + 1)]['line'] === $tokens[$commentEnd]['line']) { |
|
110 | + break; |
|
111 | + } |
|
112 | + |
|
113 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
114 | + } |
|
115 | + |
|
116 | + $phpcsFile->fixer->endChangeset(); |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + // Check for a comment description. |
|
121 | + if ($tokens[$short]['code'] !== T_DOC_COMMENT_STRING) { |
|
122 | + $error = 'Missing short description in doc comment'; |
|
123 | + $phpcsFile->addError($error, $stackPtr, 'MissingShort'); |
|
124 | + return; |
|
125 | + } |
|
126 | + |
|
127 | + // No extra newline before short description. |
|
128 | + if ($tokens[$short]['line'] !== ($tokens[$stackPtr]['line'] + 1)) { |
|
129 | + $error = 'Doc comment short description must be on the first line'; |
|
130 | + $fix = $phpcsFile->addFixableError($error, $short, 'SpacingBeforeShort'); |
|
131 | + if ($fix === true) { |
|
132 | + $phpcsFile->fixer->beginChangeset(); |
|
133 | + for ($i = $stackPtr; $i < $short; $i++) { |
|
134 | + if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) { |
|
135 | + continue; |
|
136 | + } else if ($tokens[$i]['line'] === $tokens[$short]['line']) { |
|
137 | + break; |
|
138 | + } |
|
139 | + |
|
140 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
141 | + } |
|
142 | + |
|
143 | + $phpcsFile->fixer->endChangeset(); |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + // Account for the fact that a short description might cover |
|
148 | + // multiple lines. |
|
149 | + $shortContent = $tokens[$short]['content']; |
|
150 | + $shortEnd = $short; |
|
151 | + for ($i = ($short + 1); $i < $commentEnd; $i++) { |
|
152 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
|
153 | + if ($tokens[$i]['line'] === ($tokens[$shortEnd]['line'] + 1)) { |
|
154 | + $shortContent .= $tokens[$i]['content']; |
|
155 | + $shortEnd = $i; |
|
156 | + } else { |
|
157 | + break; |
|
158 | + } |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + if (preg_match('/^\p{Ll}/u', $shortContent) === 1) { |
|
163 | + $error = 'Doc comment short description must start with a capital letter'; |
|
164 | + $phpcsFile->addError($error, $short, 'ShortNotCapital'); |
|
165 | + } |
|
166 | + |
|
167 | + $long = $phpcsFile->findNext($empty, ($shortEnd + 1), ($commentEnd - 1), true); |
|
168 | + if ($long !== false && $tokens[$long]['code'] === T_DOC_COMMENT_STRING) { |
|
169 | + if ($tokens[$long]['line'] !== ($tokens[$shortEnd]['line'] + 2)) { |
|
170 | + $error = 'There must be exactly one blank line between descriptions in a doc comment'; |
|
171 | + $fix = $phpcsFile->addFixableError($error, $long, 'SpacingBetween'); |
|
172 | + if ($fix === true) { |
|
173 | + $phpcsFile->fixer->beginChangeset(); |
|
174 | + for ($i = ($shortEnd + 1); $i < $long; $i++) { |
|
175 | + if ($tokens[$i]['line'] === $tokens[$shortEnd]['line']) { |
|
176 | + continue; |
|
177 | + } else if ($tokens[$i]['line'] === ($tokens[$long]['line'] - 1)) { |
|
178 | + break; |
|
179 | + } |
|
180 | + |
|
181 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
182 | + } |
|
183 | + |
|
184 | + $phpcsFile->fixer->endChangeset(); |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + if (preg_match('/^\p{Ll}/u', $tokens[$long]['content']) === 1) { |
|
189 | + $error = 'Doc comment long description must start with a capital letter'; |
|
190 | + $phpcsFile->addError($error, $long, 'LongNotCapital'); |
|
191 | + } |
|
192 | + }//end if |
|
193 | + |
|
194 | + if (empty($tokens[$commentStart]['comment_tags']) === true) { |
|
195 | + // No tags in the comment. |
|
196 | + return; |
|
197 | + } |
|
198 | + |
|
199 | + $firstTag = $tokens[$commentStart]['comment_tags'][0]; |
|
200 | + $prev = $phpcsFile->findPrevious($empty, ($firstTag - 1), $stackPtr, true); |
|
201 | + if ($tokens[$firstTag]['line'] !== ($tokens[$prev]['line'] + 2)) { |
|
202 | + $error = 'There must be exactly one blank line before the tags in a doc comment'; |
|
203 | + $fix = $phpcsFile->addFixableError($error, $firstTag, 'SpacingBeforeTags'); |
|
204 | + if ($fix === true) { |
|
205 | + $phpcsFile->fixer->beginChangeset(); |
|
206 | + for ($i = ($prev + 1); $i < $firstTag; $i++) { |
|
207 | + if ($tokens[$i]['line'] === $tokens[$firstTag]['line']) { |
|
208 | + break; |
|
209 | + } |
|
210 | + |
|
211 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
212 | + } |
|
213 | + |
|
214 | + $indent = str_repeat(' ', $tokens[$stackPtr]['column']); |
|
215 | + $phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar.$indent.'*'.$phpcsFile->eolChar); |
|
216 | + $phpcsFile->fixer->endChangeset(); |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + // Break out the tags into groups and check alignment within each. |
|
221 | + // A tag group is one where there are no blank lines between tags. |
|
222 | + // The param tag group is special as it requires all @param tags to be inside. |
|
223 | + $tagGroups = array(); |
|
224 | + $groupid = 0; |
|
225 | + $paramGroupid = null; |
|
226 | + foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
|
227 | + if ($pos > 0) { |
|
228 | + $prev = $phpcsFile->findPrevious( |
|
229 | + T_DOC_COMMENT_STRING, |
|
230 | + ($tag - 1), |
|
231 | + $tokens[$commentStart]['comment_tags'][($pos - 1)] |
|
232 | + ); |
|
233 | + |
|
234 | + if ($prev === false) { |
|
235 | + $prev = $tokens[$commentStart]['comment_tags'][($pos - 1)]; |
|
236 | + } |
|
237 | + |
|
238 | + if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 1)) { |
|
239 | + $groupid++; |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + if ($tokens[$tag]['content'] === '@param') { |
|
244 | + if (($paramGroupid === null |
|
245 | + && empty($tagGroups[$groupid]) === false) |
|
246 | + || ($paramGroupid !== null |
|
247 | + && $paramGroupid !== $groupid) |
|
248 | + ) { |
|
249 | + $error = 'Parameter tags must be grouped together in a doc comment'; |
|
250 | + $phpcsFile->addError($error, $tag, 'ParamGroup'); |
|
251 | + } |
|
252 | + |
|
253 | + if ($paramGroupid === null) { |
|
254 | + $paramGroupid = $groupid; |
|
255 | + } |
|
256 | + } else if ($groupid === $paramGroupid) { |
|
257 | + $error = 'Tag cannot be grouped with parameter tags in a doc comment'; |
|
258 | + $phpcsFile->addError($error, $tag, 'NonParamGroup'); |
|
259 | + }//end if |
|
260 | + |
|
261 | + $tagGroups[$groupid][] = $tag; |
|
262 | + }//end foreach |
|
263 | + |
|
264 | + foreach ($tagGroups as $group) { |
|
265 | + $maxLength = 0; |
|
266 | + $paddings = array(); |
|
267 | + foreach ($group as $pos => $tag) { |
|
268 | + $tagLength = strlen($tokens[$tag]['content']); |
|
269 | + if ($tagLength > $maxLength) { |
|
270 | + $maxLength = $tagLength; |
|
271 | + } |
|
272 | + |
|
273 | + // Check for a value. No value means no padding needed. |
|
274 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
275 | + if ($string !== false && $tokens[$string]['line'] === $tokens[$tag]['line']) { |
|
276 | + $paddings[$tag] = strlen($tokens[($tag + 1)]['content']); |
|
277 | + } |
|
278 | + } |
|
279 | + |
|
280 | + // Check that there was single blank line after the tag block |
|
281 | + // but account for a multi-line tag comments. |
|
282 | + $lastTag = $group[$pos]; |
|
283 | + $next = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($lastTag + 3), $commentEnd); |
|
284 | + if ($next !== false) { |
|
285 | + $prev = $phpcsFile->findPrevious(array(T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING), ($next - 1), $commentStart); |
|
286 | + if ($tokens[$next]['line'] !== ($tokens[$prev]['line'] + 2)) { |
|
287 | + $error = 'There must be a single blank line after a tag group'; |
|
288 | + $fix = $phpcsFile->addFixableError($error, $lastTag, 'SpacingAfterTagGroup'); |
|
289 | + if ($fix === true) { |
|
290 | + $phpcsFile->fixer->beginChangeset(); |
|
291 | + for ($i = ($prev + 1); $i < $next; $i++) { |
|
292 | + if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
293 | + break; |
|
294 | + } |
|
295 | + |
|
296 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
297 | + } |
|
298 | + |
|
299 | + $indent = str_repeat(' ', $tokens[$stackPtr]['column']); |
|
300 | + $phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar.$indent.'*'.$phpcsFile->eolChar); |
|
301 | + $phpcsFile->fixer->endChangeset(); |
|
302 | + } |
|
303 | + } |
|
304 | + }//end if |
|
305 | + |
|
306 | + // Now check paddings. |
|
307 | + foreach ($paddings as $tag => $padding) { |
|
308 | + $required = ($maxLength - strlen($tokens[$tag]['content']) + 1); |
|
309 | + |
|
310 | + if ($padding !== $required) { |
|
311 | + $error = 'Tag value indented incorrectly; expected %s spaces but found %s'; |
|
312 | + $data = array( |
|
313 | + $required, |
|
314 | + $padding, |
|
315 | + ); |
|
316 | + |
|
317 | + $fix = $phpcsFile->addFixableError($error, ($tag + 1), 'TagValueIndent', $data); |
|
318 | + if ($fix === true) { |
|
319 | + $phpcsFile->fixer->replaceToken(($tag + 1), str_repeat(' ', $required)); |
|
320 | + } |
|
321 | + } |
|
322 | + } |
|
323 | + }//end foreach |
|
324 | + |
|
325 | + // If there is a param group, it needs to be first. |
|
326 | + if ($paramGroupid !== null && $paramGroupid !== 0) { |
|
327 | + $error = 'Parameter tags must be defined first in a doc comment'; |
|
328 | + $phpcsFile->addError($error, $tagGroups[$paramGroupid][0], 'ParamNotFirst'); |
|
329 | + } |
|
330 | + |
|
331 | + $foundTags = array(); |
|
332 | + foreach ($tokens[$stackPtr]['comment_tags'] as $pos => $tag) { |
|
333 | + $tagName = $tokens[$tag]['content']; |
|
334 | + if (isset($foundTags[$tagName]) === true) { |
|
335 | + $lastTag = $tokens[$stackPtr]['comment_tags'][($pos - 1)]; |
|
336 | + if ($tokens[$lastTag]['content'] !== $tagName) { |
|
337 | + $error = 'Tags must be grouped together in a doc comment'; |
|
338 | + $phpcsFile->addError($error, $tag, 'TagsNotGrouped'); |
|
339 | + } |
|
340 | + |
|
341 | + continue; |
|
342 | + } |
|
343 | + |
|
344 | + $foundTags[$tagName] = true; |
|
345 | + } |
|
346 | + |
|
347 | + }//end process() |
|
348 | 348 | |
349 | 349 | |
350 | 350 | }//end class |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | */ |
45 | 45 | public function register() |
46 | 46 | { |
47 | - return array(T_DOC_COMMENT_OPEN_TAG); |
|
47 | + return array( T_DOC_COMMENT_OPEN_TAG ); |
|
48 | 48 | |
49 | 49 | }//end register() |
50 | 50 | |
@@ -58,59 +58,59 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return void |
60 | 60 | */ |
61 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
61 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
62 | 62 | { |
63 | 63 | $tokens = $phpcsFile->getTokens(); |
64 | 64 | $commentStart = $stackPtr; |
65 | - $commentEnd = $tokens[$stackPtr]['comment_closer']; |
|
65 | + $commentEnd = $tokens[ $stackPtr ][ 'comment_closer' ]; |
|
66 | 66 | |
67 | 67 | $empty = array( |
68 | 68 | T_DOC_COMMENT_WHITESPACE, |
69 | 69 | T_DOC_COMMENT_STAR, |
70 | 70 | ); |
71 | 71 | |
72 | - $short = $phpcsFile->findNext($empty, ($stackPtr + 1), $commentEnd, true); |
|
73 | - if ($short === false) { |
|
72 | + $short = $phpcsFile->findNext( $empty, ( $stackPtr + 1 ), $commentEnd, true ); |
|
73 | + if ( $short === false ) { |
|
74 | 74 | // No content at all. |
75 | 75 | $error = 'Doc comment is empty'; |
76 | - $phpcsFile->addError($error, $stackPtr, 'Empty'); |
|
76 | + $phpcsFile->addError( $error, $stackPtr, 'Empty' ); |
|
77 | 77 | return; |
78 | 78 | } |
79 | 79 | |
80 | 80 | // The first line of the comment should just be the /** code. |
81 | - if ($tokens[$short]['line'] === $tokens[$stackPtr]['line']) { |
|
81 | + if ( $tokens[ $short ][ 'line' ] === $tokens[ $stackPtr ][ 'line' ] ) { |
|
82 | 82 | $error = 'The open comment tag must be the only content on the line'; |
83 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen'); |
|
84 | - if ($fix === true) { |
|
83 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ContentAfterOpen' ); |
|
84 | + if ( $fix === true ) { |
|
85 | 85 | $phpcsFile->fixer->beginChangeset(); |
86 | - $phpcsFile->fixer->addNewline($stackPtr); |
|
87 | - $phpcsFile->fixer->addContentBefore($short, '* '); |
|
86 | + $phpcsFile->fixer->addNewline( $stackPtr ); |
|
87 | + $phpcsFile->fixer->addContentBefore( $short, '* ' ); |
|
88 | 88 | $phpcsFile->fixer->endChangeset(); |
89 | 89 | } |
90 | 90 | } |
91 | 91 | |
92 | 92 | // The last line of the comment should just be the */ code. |
93 | - $prev = $phpcsFile->findPrevious($empty, ($commentEnd - 1), $stackPtr, true); |
|
94 | - if ($tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
|
93 | + $prev = $phpcsFile->findPrevious( $empty, ( $commentEnd - 1 ), $stackPtr, true ); |
|
94 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $commentEnd ][ 'line' ] ) { |
|
95 | 95 | $error = 'The close comment tag must be the only content on the line'; |
96 | - $fix = $phpcsFile->addFixableError($error, $commentEnd, 'ContentBeforeClose'); |
|
97 | - if ($fix === true) { |
|
98 | - $phpcsFile->fixer->addNewlineBefore($commentEnd); |
|
96 | + $fix = $phpcsFile->addFixableError( $error, $commentEnd, 'ContentBeforeClose' ); |
|
97 | + if ( $fix === true ) { |
|
98 | + $phpcsFile->fixer->addNewlineBefore( $commentEnd ); |
|
99 | 99 | } |
100 | 100 | } |
101 | 101 | |
102 | 102 | // Check for additional blank lines at the end of the comment. |
103 | - if ($tokens[$prev]['line'] < ($tokens[$commentEnd]['line'] - 1)) { |
|
103 | + if ( $tokens[ $prev ][ 'line' ] < ( $tokens[ $commentEnd ][ 'line' ] - 1 ) ) { |
|
104 | 104 | $error = 'Additional blank lines found at end of doc comment'; |
105 | - $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter'); |
|
106 | - if ($fix === true) { |
|
105 | + $fix = $phpcsFile->addFixableError( $error, $commentEnd, 'SpacingAfter' ); |
|
106 | + if ( $fix === true ) { |
|
107 | 107 | $phpcsFile->fixer->beginChangeset(); |
108 | - for ($i = ($prev + 1); $i < $commentEnd; $i++) { |
|
109 | - if ($tokens[($i + 1)]['line'] === $tokens[$commentEnd]['line']) { |
|
108 | + for ( $i = ( $prev + 1 ); $i < $commentEnd; $i++ ) { |
|
109 | + if ( $tokens[ ( $i + 1 ) ][ 'line' ] === $tokens[ $commentEnd ][ 'line' ] ) { |
|
110 | 110 | break; |
111 | 111 | } |
112 | 112 | |
113 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
113 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | $phpcsFile->fixer->endChangeset(); |
@@ -118,26 +118,26 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | // Check for a comment description. |
121 | - if ($tokens[$short]['code'] !== T_DOC_COMMENT_STRING) { |
|
121 | + if ( $tokens[ $short ][ 'code' ] !== T_DOC_COMMENT_STRING ) { |
|
122 | 122 | $error = 'Missing short description in doc comment'; |
123 | - $phpcsFile->addError($error, $stackPtr, 'MissingShort'); |
|
123 | + $phpcsFile->addError( $error, $stackPtr, 'MissingShort' ); |
|
124 | 124 | return; |
125 | 125 | } |
126 | 126 | |
127 | 127 | // No extra newline before short description. |
128 | - if ($tokens[$short]['line'] !== ($tokens[$stackPtr]['line'] + 1)) { |
|
128 | + if ( $tokens[ $short ][ 'line' ] !== ( $tokens[ $stackPtr ][ 'line' ] + 1 ) ) { |
|
129 | 129 | $error = 'Doc comment short description must be on the first line'; |
130 | - $fix = $phpcsFile->addFixableError($error, $short, 'SpacingBeforeShort'); |
|
131 | - if ($fix === true) { |
|
130 | + $fix = $phpcsFile->addFixableError( $error, $short, 'SpacingBeforeShort' ); |
|
131 | + if ( $fix === true ) { |
|
132 | 132 | $phpcsFile->fixer->beginChangeset(); |
133 | - for ($i = $stackPtr; $i < $short; $i++) { |
|
134 | - if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) { |
|
133 | + for ( $i = $stackPtr; $i < $short; $i++ ) { |
|
134 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $stackPtr ][ 'line' ] ) { |
|
135 | 135 | continue; |
136 | - } else if ($tokens[$i]['line'] === $tokens[$short]['line']) { |
|
136 | + } else if ( $tokens[ $i ][ 'line' ] === $tokens[ $short ][ 'line' ] ) { |
|
137 | 137 | break; |
138 | 138 | } |
139 | 139 | |
140 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
140 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | $phpcsFile->fixer->endChangeset(); |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | |
147 | 147 | // Account for the fact that a short description might cover |
148 | 148 | // multiple lines. |
149 | - $shortContent = $tokens[$short]['content']; |
|
149 | + $shortContent = $tokens[ $short ][ 'content' ]; |
|
150 | 150 | $shortEnd = $short; |
151 | - for ($i = ($short + 1); $i < $commentEnd; $i++) { |
|
152 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
|
153 | - if ($tokens[$i]['line'] === ($tokens[$shortEnd]['line'] + 1)) { |
|
154 | - $shortContent .= $tokens[$i]['content']; |
|
151 | + for ( $i = ( $short + 1 ); $i < $commentEnd; $i++ ) { |
|
152 | + if ( $tokens[ $i ][ 'code' ] === T_DOC_COMMENT_STRING ) { |
|
153 | + if ( $tokens[ $i ][ 'line' ] === ( $tokens[ $shortEnd ][ 'line' ] + 1 ) ) { |
|
154 | + $shortContent .= $tokens[ $i ][ 'content' ]; |
|
155 | 155 | $shortEnd = $i; |
156 | 156 | } else { |
157 | 157 | break; |
@@ -159,60 +159,60 @@ discard block |
||
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
162 | - if (preg_match('/^\p{Ll}/u', $shortContent) === 1) { |
|
162 | + if ( preg_match( '/^\p{Ll}/u', $shortContent ) === 1 ) { |
|
163 | 163 | $error = 'Doc comment short description must start with a capital letter'; |
164 | - $phpcsFile->addError($error, $short, 'ShortNotCapital'); |
|
164 | + $phpcsFile->addError( $error, $short, 'ShortNotCapital' ); |
|
165 | 165 | } |
166 | 166 | |
167 | - $long = $phpcsFile->findNext($empty, ($shortEnd + 1), ($commentEnd - 1), true); |
|
168 | - if ($long !== false && $tokens[$long]['code'] === T_DOC_COMMENT_STRING) { |
|
169 | - if ($tokens[$long]['line'] !== ($tokens[$shortEnd]['line'] + 2)) { |
|
167 | + $long = $phpcsFile->findNext( $empty, ( $shortEnd + 1 ), ( $commentEnd - 1 ), true ); |
|
168 | + if ( $long !== false && $tokens[ $long ][ 'code' ] === T_DOC_COMMENT_STRING ) { |
|
169 | + if ( $tokens[ $long ][ 'line' ] !== ( $tokens[ $shortEnd ][ 'line' ] + 2 ) ) { |
|
170 | 170 | $error = 'There must be exactly one blank line between descriptions in a doc comment'; |
171 | - $fix = $phpcsFile->addFixableError($error, $long, 'SpacingBetween'); |
|
172 | - if ($fix === true) { |
|
171 | + $fix = $phpcsFile->addFixableError( $error, $long, 'SpacingBetween' ); |
|
172 | + if ( $fix === true ) { |
|
173 | 173 | $phpcsFile->fixer->beginChangeset(); |
174 | - for ($i = ($shortEnd + 1); $i < $long; $i++) { |
|
175 | - if ($tokens[$i]['line'] === $tokens[$shortEnd]['line']) { |
|
174 | + for ( $i = ( $shortEnd + 1 ); $i < $long; $i++ ) { |
|
175 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $shortEnd ][ 'line' ] ) { |
|
176 | 176 | continue; |
177 | - } else if ($tokens[$i]['line'] === ($tokens[$long]['line'] - 1)) { |
|
177 | + } else if ( $tokens[ $i ][ 'line' ] === ( $tokens[ $long ][ 'line' ] - 1 ) ) { |
|
178 | 178 | break; |
179 | 179 | } |
180 | 180 | |
181 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
181 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | $phpcsFile->fixer->endChangeset(); |
185 | 185 | } |
186 | 186 | } |
187 | 187 | |
188 | - if (preg_match('/^\p{Ll}/u', $tokens[$long]['content']) === 1) { |
|
188 | + if ( preg_match( '/^\p{Ll}/u', $tokens[ $long ][ 'content' ] ) === 1 ) { |
|
189 | 189 | $error = 'Doc comment long description must start with a capital letter'; |
190 | - $phpcsFile->addError($error, $long, 'LongNotCapital'); |
|
190 | + $phpcsFile->addError( $error, $long, 'LongNotCapital' ); |
|
191 | 191 | } |
192 | 192 | }//end if |
193 | 193 | |
194 | - if (empty($tokens[$commentStart]['comment_tags']) === true) { |
|
194 | + if ( empty( $tokens[ $commentStart ][ 'comment_tags' ] ) === true ) { |
|
195 | 195 | // No tags in the comment. |
196 | 196 | return; |
197 | 197 | } |
198 | 198 | |
199 | - $firstTag = $tokens[$commentStart]['comment_tags'][0]; |
|
200 | - $prev = $phpcsFile->findPrevious($empty, ($firstTag - 1), $stackPtr, true); |
|
201 | - if ($tokens[$firstTag]['line'] !== ($tokens[$prev]['line'] + 2)) { |
|
199 | + $firstTag = $tokens[ $commentStart ][ 'comment_tags' ][ 0 ]; |
|
200 | + $prev = $phpcsFile->findPrevious( $empty, ( $firstTag - 1 ), $stackPtr, true ); |
|
201 | + if ( $tokens[ $firstTag ][ 'line' ] !== ( $tokens[ $prev ][ 'line' ] + 2 ) ) { |
|
202 | 202 | $error = 'There must be exactly one blank line before the tags in a doc comment'; |
203 | - $fix = $phpcsFile->addFixableError($error, $firstTag, 'SpacingBeforeTags'); |
|
204 | - if ($fix === true) { |
|
203 | + $fix = $phpcsFile->addFixableError( $error, $firstTag, 'SpacingBeforeTags' ); |
|
204 | + if ( $fix === true ) { |
|
205 | 205 | $phpcsFile->fixer->beginChangeset(); |
206 | - for ($i = ($prev + 1); $i < $firstTag; $i++) { |
|
207 | - if ($tokens[$i]['line'] === $tokens[$firstTag]['line']) { |
|
206 | + for ( $i = ( $prev + 1 ); $i < $firstTag; $i++ ) { |
|
207 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $firstTag ][ 'line' ] ) { |
|
208 | 208 | break; |
209 | 209 | } |
210 | 210 | |
211 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
211 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
212 | 212 | } |
213 | 213 | |
214 | - $indent = str_repeat(' ', $tokens[$stackPtr]['column']); |
|
215 | - $phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar.$indent.'*'.$phpcsFile->eolChar); |
|
214 | + $indent = str_repeat( ' ', $tokens[ $stackPtr ][ 'column' ] ); |
|
215 | + $phpcsFile->fixer->addContent( $prev, $phpcsFile->eolChar . $indent . '*' . $phpcsFile->eolChar ); |
|
216 | 216 | $phpcsFile->fixer->endChangeset(); |
217 | 217 | } |
218 | 218 | } |
@@ -223,125 +223,125 @@ discard block |
||
223 | 223 | $tagGroups = array(); |
224 | 224 | $groupid = 0; |
225 | 225 | $paramGroupid = null; |
226 | - foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
|
227 | - if ($pos > 0) { |
|
226 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $pos => $tag ) { |
|
227 | + if ( $pos > 0 ) { |
|
228 | 228 | $prev = $phpcsFile->findPrevious( |
229 | 229 | T_DOC_COMMENT_STRING, |
230 | - ($tag - 1), |
|
231 | - $tokens[$commentStart]['comment_tags'][($pos - 1)] |
|
230 | + ( $tag - 1 ), |
|
231 | + $tokens[ $commentStart ][ 'comment_tags' ][ ( $pos - 1 ) ] |
|
232 | 232 | ); |
233 | 233 | |
234 | - if ($prev === false) { |
|
235 | - $prev = $tokens[$commentStart]['comment_tags'][($pos - 1)]; |
|
234 | + if ( $prev === false ) { |
|
235 | + $prev = $tokens[ $commentStart ][ 'comment_tags' ][ ( $pos - 1 ) ]; |
|
236 | 236 | } |
237 | 237 | |
238 | - if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 1)) { |
|
238 | + if ( $tokens[ $prev ][ 'line' ] !== ( $tokens[ $tag ][ 'line' ] - 1 ) ) { |
|
239 | 239 | $groupid++; |
240 | 240 | } |
241 | 241 | } |
242 | 242 | |
243 | - if ($tokens[$tag]['content'] === '@param') { |
|
244 | - if (($paramGroupid === null |
|
245 | - && empty($tagGroups[$groupid]) === false) |
|
246 | - || ($paramGroupid !== null |
|
247 | - && $paramGroupid !== $groupid) |
|
243 | + if ( $tokens[ $tag ][ 'content' ] === '@param' ) { |
|
244 | + if ( ( $paramGroupid === null |
|
245 | + && empty( $tagGroups[ $groupid ] ) === false ) |
|
246 | + || ( $paramGroupid !== null |
|
247 | + && $paramGroupid !== $groupid ) |
|
248 | 248 | ) { |
249 | 249 | $error = 'Parameter tags must be grouped together in a doc comment'; |
250 | - $phpcsFile->addError($error, $tag, 'ParamGroup'); |
|
250 | + $phpcsFile->addError( $error, $tag, 'ParamGroup' ); |
|
251 | 251 | } |
252 | 252 | |
253 | - if ($paramGroupid === null) { |
|
253 | + if ( $paramGroupid === null ) { |
|
254 | 254 | $paramGroupid = $groupid; |
255 | 255 | } |
256 | - } else if ($groupid === $paramGroupid) { |
|
256 | + } else if ( $groupid === $paramGroupid ) { |
|
257 | 257 | $error = 'Tag cannot be grouped with parameter tags in a doc comment'; |
258 | - $phpcsFile->addError($error, $tag, 'NonParamGroup'); |
|
258 | + $phpcsFile->addError( $error, $tag, 'NonParamGroup' ); |
|
259 | 259 | }//end if |
260 | 260 | |
261 | - $tagGroups[$groupid][] = $tag; |
|
261 | + $tagGroups[ $groupid ][ ] = $tag; |
|
262 | 262 | }//end foreach |
263 | 263 | |
264 | - foreach ($tagGroups as $group) { |
|
264 | + foreach ( $tagGroups as $group ) { |
|
265 | 265 | $maxLength = 0; |
266 | 266 | $paddings = array(); |
267 | - foreach ($group as $pos => $tag) { |
|
268 | - $tagLength = strlen($tokens[$tag]['content']); |
|
269 | - if ($tagLength > $maxLength) { |
|
267 | + foreach ( $group as $pos => $tag ) { |
|
268 | + $tagLength = strlen( $tokens[ $tag ][ 'content' ] ); |
|
269 | + if ( $tagLength > $maxLength ) { |
|
270 | 270 | $maxLength = $tagLength; |
271 | 271 | } |
272 | 272 | |
273 | 273 | // Check for a value. No value means no padding needed. |
274 | - $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
275 | - if ($string !== false && $tokens[$string]['line'] === $tokens[$tag]['line']) { |
|
276 | - $paddings[$tag] = strlen($tokens[($tag + 1)]['content']); |
|
274 | + $string = $phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $commentEnd ); |
|
275 | + if ( $string !== false && $tokens[ $string ][ 'line' ] === $tokens[ $tag ][ 'line' ] ) { |
|
276 | + $paddings[ $tag ] = strlen( $tokens[ ( $tag + 1 ) ][ 'content' ] ); |
|
277 | 277 | } |
278 | 278 | } |
279 | 279 | |
280 | 280 | // Check that there was single blank line after the tag block |
281 | 281 | // but account for a multi-line tag comments. |
282 | - $lastTag = $group[$pos]; |
|
283 | - $next = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($lastTag + 3), $commentEnd); |
|
284 | - if ($next !== false) { |
|
285 | - $prev = $phpcsFile->findPrevious(array(T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING), ($next - 1), $commentStart); |
|
286 | - if ($tokens[$next]['line'] !== ($tokens[$prev]['line'] + 2)) { |
|
282 | + $lastTag = $group[ $pos ]; |
|
283 | + $next = $phpcsFile->findNext( T_DOC_COMMENT_TAG, ( $lastTag + 3 ), $commentEnd ); |
|
284 | + if ( $next !== false ) { |
|
285 | + $prev = $phpcsFile->findPrevious( array( T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING ), ( $next - 1 ), $commentStart ); |
|
286 | + if ( $tokens[ $next ][ 'line' ] !== ( $tokens[ $prev ][ 'line' ] + 2 ) ) { |
|
287 | 287 | $error = 'There must be a single blank line after a tag group'; |
288 | - $fix = $phpcsFile->addFixableError($error, $lastTag, 'SpacingAfterTagGroup'); |
|
289 | - if ($fix === true) { |
|
288 | + $fix = $phpcsFile->addFixableError( $error, $lastTag, 'SpacingAfterTagGroup' ); |
|
289 | + if ( $fix === true ) { |
|
290 | 290 | $phpcsFile->fixer->beginChangeset(); |
291 | - for ($i = ($prev + 1); $i < $next; $i++) { |
|
292 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
291 | + for ( $i = ( $prev + 1 ); $i < $next; $i++ ) { |
|
292 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $next ][ 'line' ] ) { |
|
293 | 293 | break; |
294 | 294 | } |
295 | 295 | |
296 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
296 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
297 | 297 | } |
298 | 298 | |
299 | - $indent = str_repeat(' ', $tokens[$stackPtr]['column']); |
|
300 | - $phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar.$indent.'*'.$phpcsFile->eolChar); |
|
299 | + $indent = str_repeat( ' ', $tokens[ $stackPtr ][ 'column' ] ); |
|
300 | + $phpcsFile->fixer->addContent( $prev, $phpcsFile->eolChar . $indent . '*' . $phpcsFile->eolChar ); |
|
301 | 301 | $phpcsFile->fixer->endChangeset(); |
302 | 302 | } |
303 | 303 | } |
304 | 304 | }//end if |
305 | 305 | |
306 | 306 | // Now check paddings. |
307 | - foreach ($paddings as $tag => $padding) { |
|
308 | - $required = ($maxLength - strlen($tokens[$tag]['content']) + 1); |
|
307 | + foreach ( $paddings as $tag => $padding ) { |
|
308 | + $required = ( $maxLength - strlen( $tokens[ $tag ][ 'content' ] ) + 1 ); |
|
309 | 309 | |
310 | - if ($padding !== $required) { |
|
310 | + if ( $padding !== $required ) { |
|
311 | 311 | $error = 'Tag value indented incorrectly; expected %s spaces but found %s'; |
312 | 312 | $data = array( |
313 | 313 | $required, |
314 | 314 | $padding, |
315 | 315 | ); |
316 | 316 | |
317 | - $fix = $phpcsFile->addFixableError($error, ($tag + 1), 'TagValueIndent', $data); |
|
318 | - if ($fix === true) { |
|
319 | - $phpcsFile->fixer->replaceToken(($tag + 1), str_repeat(' ', $required)); |
|
317 | + $fix = $phpcsFile->addFixableError( $error, ( $tag + 1 ), 'TagValueIndent', $data ); |
|
318 | + if ( $fix === true ) { |
|
319 | + $phpcsFile->fixer->replaceToken( ( $tag + 1 ), str_repeat( ' ', $required ) ); |
|
320 | 320 | } |
321 | 321 | } |
322 | 322 | } |
323 | 323 | }//end foreach |
324 | 324 | |
325 | 325 | // If there is a param group, it needs to be first. |
326 | - if ($paramGroupid !== null && $paramGroupid !== 0) { |
|
326 | + if ( $paramGroupid !== null && $paramGroupid !== 0 ) { |
|
327 | 327 | $error = 'Parameter tags must be defined first in a doc comment'; |
328 | - $phpcsFile->addError($error, $tagGroups[$paramGroupid][0], 'ParamNotFirst'); |
|
328 | + $phpcsFile->addError( $error, $tagGroups[ $paramGroupid ][ 0 ], 'ParamNotFirst' ); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | $foundTags = array(); |
332 | - foreach ($tokens[$stackPtr]['comment_tags'] as $pos => $tag) { |
|
333 | - $tagName = $tokens[$tag]['content']; |
|
334 | - if (isset($foundTags[$tagName]) === true) { |
|
335 | - $lastTag = $tokens[$stackPtr]['comment_tags'][($pos - 1)]; |
|
336 | - if ($tokens[$lastTag]['content'] !== $tagName) { |
|
332 | + foreach ( $tokens[ $stackPtr ][ 'comment_tags' ] as $pos => $tag ) { |
|
333 | + $tagName = $tokens[ $tag ][ 'content' ]; |
|
334 | + if ( isset( $foundTags[ $tagName ] ) === true ) { |
|
335 | + $lastTag = $tokens[ $stackPtr ][ 'comment_tags' ][ ( $pos - 1 ) ]; |
|
336 | + if ( $tokens[ $lastTag ][ 'content' ] !== $tagName ) { |
|
337 | 337 | $error = 'Tags must be grouped together in a doc comment'; |
338 | - $phpcsFile->addError($error, $tag, 'TagsNotGrouped'); |
|
338 | + $phpcsFile->addError( $error, $tag, 'TagsNotGrouped' ); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | continue; |
342 | 342 | } |
343 | 343 | |
344 | - $foundTags[$tagName] = true; |
|
344 | + $foundTags[ $tagName ] = true; |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | }//end process() |
@@ -23,8 +23,7 @@ discard block |
||
23 | 23 | * @version Release: @package_version@ |
24 | 24 | * @link http://pear.php.net/package/PHP_CodeSniffer |
25 | 25 | */ |
26 | -class Generic_Sniffs_Commenting_DocCommentSniff implements PHP_CodeSniffer_Sniff |
|
27 | -{ |
|
26 | +class Generic_Sniffs_Commenting_DocCommentSniff implements PHP_CodeSniffer_Sniff { |
|
28 | 27 | |
29 | 28 | /** |
30 | 29 | * A list of tokenizers this sniff supports. |
@@ -42,8 +41,7 @@ discard block |
||
42 | 41 | * |
43 | 42 | * @return array |
44 | 43 | */ |
45 | - public function register() |
|
46 | - { |
|
44 | + public function register() { |
|
47 | 45 | return array(T_DOC_COMMENT_OPEN_TAG); |
48 | 46 | |
49 | 47 | }//end register() |
@@ -58,8 +56,7 @@ discard block |
||
58 | 56 | * |
59 | 57 | * @return void |
60 | 58 | */ |
61 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
62 | - { |
|
59 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
63 | 60 | $tokens = $phpcsFile->getTokens(); |
64 | 61 | $commentStart = $stackPtr; |
65 | 62 | $commentEnd = $tokens[$stackPtr]['comment_closer']; |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | /** |
56 | 56 | * Returns the token types that this sniff is interested in. |
57 | 57 | * |
58 | - * @return int[] |
|
58 | + * @return integer[] |
|
59 | 59 | */ |
60 | 60 | public function register() |
61 | 61 | { |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @param int $stackPtr The position in the stack where |
72 | 72 | * the token was found. |
73 | 73 | * |
74 | - * @return void |
|
74 | + * @return null|integer |
|
75 | 75 | * @throws PHP_CodeSniffer_Exception If jslint.js could not be run |
76 | 76 | */ |
77 | 77 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
@@ -28,100 +28,100 @@ |
||
28 | 28 | class Generic_Sniffs_Debug_ClosureLinterSniff implements PHP_CodeSniffer_Sniff |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * A list of error codes that should show errors. |
|
33 | - * |
|
34 | - * All other error codes will show warnings. |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public $errorCodes = array(); |
|
39 | - |
|
40 | - /** |
|
41 | - * A list of error codes to ignore. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - public $ignoreCodes = array(); |
|
46 | - |
|
47 | - /** |
|
48 | - * A list of tokenizers this sniff supports. |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - public $supportedTokenizers = array('JS'); |
|
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_OPEN_TAG); |
|
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 | - * @throws PHP_CodeSniffer_Exception If jslint.js could not be run |
|
76 | - */ |
|
77 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
78 | - { |
|
79 | - $fileName = $phpcsFile->getFilename(); |
|
80 | - |
|
81 | - $lintPath = PHP_CodeSniffer::getConfigData('gjslint_path'); |
|
82 | - if ($lintPath === null) { |
|
83 | - return; |
|
84 | - } |
|
85 | - |
|
86 | - $cmd = "$lintPath --nosummary --notime --unix_mode \"$fileName\""; |
|
87 | - $msg = exec($cmd, $output, $retval); |
|
88 | - |
|
89 | - if (is_array($output) === false) { |
|
90 | - return; |
|
91 | - } |
|
92 | - |
|
93 | - foreach ($output as $finding) { |
|
94 | - $matches = array(); |
|
95 | - $numMatches = preg_match('/^(.*):([0-9]+):\(.*?([0-9]+)\)(.*)$/', $finding, $matches); |
|
96 | - if ($numMatches === 0) { |
|
97 | - continue; |
|
98 | - } |
|
99 | - |
|
100 | - // Skip error codes we are ignoring. |
|
101 | - $code = $matches[3]; |
|
102 | - if (in_array($code, $this->ignoreCodes) === true) { |
|
103 | - continue; |
|
104 | - } |
|
105 | - |
|
106 | - $line = (int) $matches[2]; |
|
107 | - $error = trim($matches[4]); |
|
108 | - |
|
109 | - $message = 'gjslint says: (%s) %s'; |
|
110 | - $data = array( |
|
111 | - $code, |
|
112 | - $error, |
|
113 | - ); |
|
114 | - if (in_array($code, $this->errorCodes) === true) { |
|
115 | - $phpcsFile->addErrorOnLine($message, $line, 'ExternalToolError', $data); |
|
116 | - } else { |
|
117 | - $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool', $data); |
|
118 | - } |
|
119 | - }//end foreach |
|
120 | - |
|
121 | - // Ignore the rest of the file. |
|
122 | - return ($phpcsFile->numTokens + 1); |
|
123 | - |
|
124 | - }//end process() |
|
31 | + /** |
|
32 | + * A list of error codes that should show errors. |
|
33 | + * |
|
34 | + * All other error codes will show warnings. |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public $errorCodes = array(); |
|
39 | + |
|
40 | + /** |
|
41 | + * A list of error codes to ignore. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + public $ignoreCodes = array(); |
|
46 | + |
|
47 | + /** |
|
48 | + * A list of tokenizers this sniff supports. |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + public $supportedTokenizers = array('JS'); |
|
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_OPEN_TAG); |
|
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 | + * @throws PHP_CodeSniffer_Exception If jslint.js could not be run |
|
76 | + */ |
|
77 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
78 | + { |
|
79 | + $fileName = $phpcsFile->getFilename(); |
|
80 | + |
|
81 | + $lintPath = PHP_CodeSniffer::getConfigData('gjslint_path'); |
|
82 | + if ($lintPath === null) { |
|
83 | + return; |
|
84 | + } |
|
85 | + |
|
86 | + $cmd = "$lintPath --nosummary --notime --unix_mode \"$fileName\""; |
|
87 | + $msg = exec($cmd, $output, $retval); |
|
88 | + |
|
89 | + if (is_array($output) === false) { |
|
90 | + return; |
|
91 | + } |
|
92 | + |
|
93 | + foreach ($output as $finding) { |
|
94 | + $matches = array(); |
|
95 | + $numMatches = preg_match('/^(.*):([0-9]+):\(.*?([0-9]+)\)(.*)$/', $finding, $matches); |
|
96 | + if ($numMatches === 0) { |
|
97 | + continue; |
|
98 | + } |
|
99 | + |
|
100 | + // Skip error codes we are ignoring. |
|
101 | + $code = $matches[3]; |
|
102 | + if (in_array($code, $this->ignoreCodes) === true) { |
|
103 | + continue; |
|
104 | + } |
|
105 | + |
|
106 | + $line = (int) $matches[2]; |
|
107 | + $error = trim($matches[4]); |
|
108 | + |
|
109 | + $message = 'gjslint says: (%s) %s'; |
|
110 | + $data = array( |
|
111 | + $code, |
|
112 | + $error, |
|
113 | + ); |
|
114 | + if (in_array($code, $this->errorCodes) === true) { |
|
115 | + $phpcsFile->addErrorOnLine($message, $line, 'ExternalToolError', $data); |
|
116 | + } else { |
|
117 | + $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool', $data); |
|
118 | + } |
|
119 | + }//end foreach |
|
120 | + |
|
121 | + // Ignore the rest of the file. |
|
122 | + return ($phpcsFile->numTokens + 1); |
|
123 | + |
|
124 | + }//end process() |
|
125 | 125 | |
126 | 126 | |
127 | 127 | }//end class |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * |
50 | 50 | * @var array |
51 | 51 | */ |
52 | - public $supportedTokenizers = array('JS'); |
|
52 | + public $supportedTokenizers = array( 'JS' ); |
|
53 | 53 | |
54 | 54 | |
55 | 55 | /** |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function register() |
61 | 61 | { |
62 | - return array(T_OPEN_TAG); |
|
62 | + return array( T_OPEN_TAG ); |
|
63 | 63 | |
64 | 64 | }//end register() |
65 | 65 | |
@@ -74,52 +74,52 @@ discard block |
||
74 | 74 | * @return void |
75 | 75 | * @throws PHP_CodeSniffer_Exception If jslint.js could not be run |
76 | 76 | */ |
77 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
77 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
78 | 78 | { |
79 | 79 | $fileName = $phpcsFile->getFilename(); |
80 | 80 | |
81 | - $lintPath = PHP_CodeSniffer::getConfigData('gjslint_path'); |
|
82 | - if ($lintPath === null) { |
|
81 | + $lintPath = PHP_CodeSniffer::getConfigData( 'gjslint_path' ); |
|
82 | + if ( $lintPath === null ) { |
|
83 | 83 | return; |
84 | 84 | } |
85 | 85 | |
86 | 86 | $cmd = "$lintPath --nosummary --notime --unix_mode \"$fileName\""; |
87 | - $msg = exec($cmd, $output, $retval); |
|
87 | + $msg = exec( $cmd, $output, $retval ); |
|
88 | 88 | |
89 | - if (is_array($output) === false) { |
|
89 | + if ( is_array( $output ) === false ) { |
|
90 | 90 | return; |
91 | 91 | } |
92 | 92 | |
93 | - foreach ($output as $finding) { |
|
93 | + foreach ( $output as $finding ) { |
|
94 | 94 | $matches = array(); |
95 | - $numMatches = preg_match('/^(.*):([0-9]+):\(.*?([0-9]+)\)(.*)$/', $finding, $matches); |
|
96 | - if ($numMatches === 0) { |
|
95 | + $numMatches = preg_match( '/^(.*):([0-9]+):\(.*?([0-9]+)\)(.*)$/', $finding, $matches ); |
|
96 | + if ( $numMatches === 0 ) { |
|
97 | 97 | continue; |
98 | 98 | } |
99 | 99 | |
100 | 100 | // Skip error codes we are ignoring. |
101 | - $code = $matches[3]; |
|
102 | - if (in_array($code, $this->ignoreCodes) === true) { |
|
101 | + $code = $matches[ 3 ]; |
|
102 | + if ( in_array( $code, $this->ignoreCodes ) === true ) { |
|
103 | 103 | continue; |
104 | 104 | } |
105 | 105 | |
106 | - $line = (int) $matches[2]; |
|
107 | - $error = trim($matches[4]); |
|
106 | + $line = (int) $matches[ 2 ]; |
|
107 | + $error = trim( $matches[ 4 ] ); |
|
108 | 108 | |
109 | 109 | $message = 'gjslint says: (%s) %s'; |
110 | 110 | $data = array( |
111 | 111 | $code, |
112 | 112 | $error, |
113 | 113 | ); |
114 | - if (in_array($code, $this->errorCodes) === true) { |
|
115 | - $phpcsFile->addErrorOnLine($message, $line, 'ExternalToolError', $data); |
|
114 | + if ( in_array( $code, $this->errorCodes ) === true ) { |
|
115 | + $phpcsFile->addErrorOnLine( $message, $line, 'ExternalToolError', $data ); |
|
116 | 116 | } else { |
117 | - $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool', $data); |
|
117 | + $phpcsFile->addWarningOnLine( $message, $line, 'ExternalTool', $data ); |
|
118 | 118 | } |
119 | 119 | }//end foreach |
120 | 120 | |
121 | 121 | // Ignore the rest of the file. |
122 | - return ($phpcsFile->numTokens + 1); |
|
122 | + return ( $phpcsFile->numTokens + 1 ); |
|
123 | 123 | |
124 | 124 | }//end process() |
125 | 125 |
@@ -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 Generic_Sniffs_Debug_ClosureLinterSniff implements PHP_CodeSniffer_Sniff |
|
29 | -{ |
|
28 | +class Generic_Sniffs_Debug_ClosureLinterSniff implements PHP_CodeSniffer_Sniff { |
|
30 | 29 | |
31 | 30 | /** |
32 | 31 | * A list of error codes that should show errors. |
@@ -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_OPEN_TAG); |
63 | 61 | |
64 | 62 | }//end register() |
@@ -74,8 +72,7 @@ discard block |
||
74 | 72 | * @return void |
75 | 73 | * @throws PHP_CodeSniffer_Exception If jslint.js could not be run |
76 | 74 | */ |
77 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
78 | - { |
|
75 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
79 | 76 | $fileName = $phpcsFile->getFilename(); |
80 | 77 | |
81 | 78 | $lintPath = PHP_CodeSniffer::getConfigData('gjslint_path'); |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | /** |
42 | 42 | * Returns the token types that this sniff is interested in. |
43 | 43 | * |
44 | - * @return int[] |
|
44 | + * @return integer[] |
|
45 | 45 | */ |
46 | 46 | public function register() |
47 | 47 | { |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param int $stackPtr The position in the stack where |
58 | 58 | * the token was found. |
59 | 59 | * |
60 | - * @return void |
|
60 | + * @return null|integer |
|
61 | 61 | * @throws PHP_CodeSniffer_Exception If jshint.js could not be run |
62 | 62 | */ |
63 | 63 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
@@ -30,67 +30,67 @@ |
||
30 | 30 | class Generic_Sniffs_Debug_JSHintSniff implements PHP_CodeSniffer_Sniff |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * A list of tokenizers this sniff supports. |
|
35 | - * |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - public $supportedTokenizers = array('JS'); |
|
33 | + /** |
|
34 | + * A list of tokenizers this sniff supports. |
|
35 | + * |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + public $supportedTokenizers = array('JS'); |
|
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * Returns the token types that this sniff is interested in. |
|
43 | - * |
|
44 | - * @return int[] |
|
45 | - */ |
|
46 | - public function register() |
|
47 | - { |
|
48 | - return array(T_OPEN_TAG); |
|
41 | + /** |
|
42 | + * Returns the token types that this sniff is interested in. |
|
43 | + * |
|
44 | + * @return int[] |
|
45 | + */ |
|
46 | + public function register() |
|
47 | + { |
|
48 | + return array(T_OPEN_TAG); |
|
49 | 49 | |
50 | - }//end register() |
|
50 | + }//end register() |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * Processes the tokens that this sniff is interested in. |
|
55 | - * |
|
56 | - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
57 | - * @param int $stackPtr The position in the stack where |
|
58 | - * the token was found. |
|
59 | - * |
|
60 | - * @return void |
|
61 | - * @throws PHP_CodeSniffer_Exception If jshint.js could not be run |
|
62 | - */ |
|
63 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
64 | - { |
|
65 | - $fileName = $phpcsFile->getFilename(); |
|
53 | + /** |
|
54 | + * Processes the tokens that this sniff is interested in. |
|
55 | + * |
|
56 | + * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
57 | + * @param int $stackPtr The position in the stack where |
|
58 | + * the token was found. |
|
59 | + * |
|
60 | + * @return void |
|
61 | + * @throws PHP_CodeSniffer_Exception If jshint.js could not be run |
|
62 | + */ |
|
63 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
64 | + { |
|
65 | + $fileName = $phpcsFile->getFilename(); |
|
66 | 66 | |
67 | - $rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path'); |
|
68 | - $jshintPath = PHP_CodeSniffer::getConfigData('jshint_path'); |
|
69 | - if ($rhinoPath === null || $jshintPath === null) { |
|
70 | - return; |
|
71 | - } |
|
67 | + $rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path'); |
|
68 | + $jshintPath = PHP_CodeSniffer::getConfigData('jshint_path'); |
|
69 | + if ($rhinoPath === null || $jshintPath === null) { |
|
70 | + return; |
|
71 | + } |
|
72 | 72 | |
73 | - $cmd = "$rhinoPath \"$jshintPath\" \"$fileName\""; |
|
74 | - $msg = exec($cmd, $output, $retval); |
|
73 | + $cmd = "$rhinoPath \"$jshintPath\" \"$fileName\""; |
|
74 | + $msg = exec($cmd, $output, $retval); |
|
75 | 75 | |
76 | - if (is_array($output) === true) { |
|
77 | - foreach ($output as $finding) { |
|
78 | - $matches = array(); |
|
79 | - $numMatches = preg_match('/^(.+)\(.+:([0-9]+).*:[0-9]+\)$/', $finding, $matches); |
|
80 | - if ($numMatches === 0) { |
|
81 | - continue; |
|
82 | - } |
|
76 | + if (is_array($output) === true) { |
|
77 | + foreach ($output as $finding) { |
|
78 | + $matches = array(); |
|
79 | + $numMatches = preg_match('/^(.+)\(.+:([0-9]+).*:[0-9]+\)$/', $finding, $matches); |
|
80 | + if ($numMatches === 0) { |
|
81 | + continue; |
|
82 | + } |
|
83 | 83 | |
84 | - $line = (int) $matches[2]; |
|
85 | - $message = 'jshint says: '.trim($matches[1]); |
|
86 | - $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool'); |
|
87 | - } |
|
88 | - } |
|
84 | + $line = (int) $matches[2]; |
|
85 | + $message = 'jshint says: '.trim($matches[1]); |
|
86 | + $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool'); |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - // Ignore the rest of the file. |
|
91 | - return ($phpcsFile->numTokens + 1); |
|
90 | + // Ignore the rest of the file. |
|
91 | + return ($phpcsFile->numTokens + 1); |
|
92 | 92 | |
93 | - }//end process() |
|
93 | + }//end process() |
|
94 | 94 | |
95 | 95 | |
96 | 96 | }//end class |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * |
36 | 36 | * @var array |
37 | 37 | */ |
38 | - public $supportedTokenizers = array('JS'); |
|
38 | + public $supportedTokenizers = array( 'JS' ); |
|
39 | 39 | |
40 | 40 | |
41 | 41 | /** |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function register() |
47 | 47 | { |
48 | - return array(T_OPEN_TAG); |
|
48 | + return array( T_OPEN_TAG ); |
|
49 | 49 | |
50 | 50 | }//end register() |
51 | 51 | |
@@ -60,35 +60,35 @@ discard block |
||
60 | 60 | * @return void |
61 | 61 | * @throws PHP_CodeSniffer_Exception If jshint.js could not be run |
62 | 62 | */ |
63 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
63 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
64 | 64 | { |
65 | 65 | $fileName = $phpcsFile->getFilename(); |
66 | 66 | |
67 | - $rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path'); |
|
68 | - $jshintPath = PHP_CodeSniffer::getConfigData('jshint_path'); |
|
69 | - if ($rhinoPath === null || $jshintPath === null) { |
|
67 | + $rhinoPath = PHP_CodeSniffer::getConfigData( 'rhino_path' ); |
|
68 | + $jshintPath = PHP_CodeSniffer::getConfigData( 'jshint_path' ); |
|
69 | + if ( $rhinoPath === null || $jshintPath === null ) { |
|
70 | 70 | return; |
71 | 71 | } |
72 | 72 | |
73 | 73 | $cmd = "$rhinoPath \"$jshintPath\" \"$fileName\""; |
74 | - $msg = exec($cmd, $output, $retval); |
|
74 | + $msg = exec( $cmd, $output, $retval ); |
|
75 | 75 | |
76 | - if (is_array($output) === true) { |
|
77 | - foreach ($output as $finding) { |
|
76 | + if ( is_array( $output ) === true ) { |
|
77 | + foreach ( $output as $finding ) { |
|
78 | 78 | $matches = array(); |
79 | - $numMatches = preg_match('/^(.+)\(.+:([0-9]+).*:[0-9]+\)$/', $finding, $matches); |
|
80 | - if ($numMatches === 0) { |
|
79 | + $numMatches = preg_match( '/^(.+)\(.+:([0-9]+).*:[0-9]+\)$/', $finding, $matches ); |
|
80 | + if ( $numMatches === 0 ) { |
|
81 | 81 | continue; |
82 | 82 | } |
83 | 83 | |
84 | - $line = (int) $matches[2]; |
|
85 | - $message = 'jshint says: '.trim($matches[1]); |
|
86 | - $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool'); |
|
84 | + $line = (int) $matches[ 2 ]; |
|
85 | + $message = 'jshint says: ' . trim( $matches[ 1 ] ); |
|
86 | + $phpcsFile->addWarningOnLine( $message, $line, 'ExternalTool' ); |
|
87 | 87 | } |
88 | 88 | } |
89 | 89 | |
90 | 90 | // Ignore the rest of the file. |
91 | - return ($phpcsFile->numTokens + 1); |
|
91 | + return ( $phpcsFile->numTokens + 1 ); |
|
92 | 92 | |
93 | 93 | }//end process() |
94 | 94 |
@@ -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 Generic_Sniffs_Debug_JSHintSniff implements PHP_CodeSniffer_Sniff |
|
31 | -{ |
|
30 | +class Generic_Sniffs_Debug_JSHintSniff implements PHP_CodeSniffer_Sniff { |
|
32 | 31 | |
33 | 32 | /** |
34 | 33 | * A list of tokenizers this sniff supports. |
@@ -43,8 +42,7 @@ discard block |
||
43 | 42 | * |
44 | 43 | * @return int[] |
45 | 44 | */ |
46 | - public function register() |
|
47 | - { |
|
45 | + public function register() { |
|
48 | 46 | return array(T_OPEN_TAG); |
49 | 47 | |
50 | 48 | }//end register() |
@@ -60,8 +58,7 @@ discard block |
||
60 | 58 | * @return void |
61 | 59 | * @throws PHP_CodeSniffer_Exception If jshint.js could not be run |
62 | 60 | */ |
63 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
64 | - { |
|
61 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
65 | 62 | $fileName = $phpcsFile->getFilename(); |
66 | 63 | |
67 | 64 | $rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path'); |
@@ -32,7 +32,7 @@ |
||
32 | 32 | /** |
33 | 33 | * Returns an array of tokens this test wants to listen for. |
34 | 34 | * |
35 | - * @return array |
|
35 | + * @return string[] |
|
36 | 36 | */ |
37 | 37 | public function register() |
38 | 38 | { |
@@ -29,74 +29,74 @@ |
||
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_SEMICOLON); |
|
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_SEMICOLON); |
|
40 | 40 | |
41 | - }//end register() |
|
41 | + }//end register() |
|
42 | 42 | |
43 | 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 | - $tokens = $phpcsFile->getTokens(); |
|
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 | + $tokens = $phpcsFile->getTokens(); |
|
56 | 56 | |
57 | - $prev = $phpcsFile->findPrevious(array(T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO), ($stackPtr - 1)); |
|
58 | - if ($prev === false |
|
59 | - || $tokens[$prev]['code'] === T_OPEN_TAG |
|
60 | - || $tokens[$prev]['code'] === T_OPEN_TAG_WITH_ECHO |
|
61 | - ) { |
|
62 | - $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no'); |
|
63 | - return; |
|
64 | - } |
|
57 | + $prev = $phpcsFile->findPrevious(array(T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO), ($stackPtr - 1)); |
|
58 | + if ($prev === false |
|
59 | + || $tokens[$prev]['code'] === T_OPEN_TAG |
|
60 | + || $tokens[$prev]['code'] === T_OPEN_TAG_WITH_ECHO |
|
61 | + ) { |
|
62 | + $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no'); |
|
63 | + return; |
|
64 | + } |
|
65 | 65 | |
66 | - // Ignore multiple statements in a FOR condition. |
|
67 | - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
68 | - foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) { |
|
69 | - if (isset($tokens[$bracket]['parenthesis_owner']) === false) { |
|
70 | - // Probably a closure sitting inside a function call. |
|
71 | - continue; |
|
72 | - } |
|
66 | + // Ignore multiple statements in a FOR condition. |
|
67 | + if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
68 | + foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) { |
|
69 | + if (isset($tokens[$bracket]['parenthesis_owner']) === false) { |
|
70 | + // Probably a closure sitting inside a function call. |
|
71 | + continue; |
|
72 | + } |
|
73 | 73 | |
74 | - $owner = $tokens[$bracket]['parenthesis_owner']; |
|
75 | - if ($tokens[$owner]['code'] === T_FOR) { |
|
76 | - return; |
|
77 | - } |
|
78 | - } |
|
79 | - } |
|
74 | + $owner = $tokens[$bracket]['parenthesis_owner']; |
|
75 | + if ($tokens[$owner]['code'] === T_FOR) { |
|
76 | + return; |
|
77 | + } |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
|
82 | - $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'yes'); |
|
81 | + if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
|
82 | + $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'yes'); |
|
83 | 83 | |
84 | - $error = 'Each PHP statement must be on a line by itself'; |
|
85 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SameLine'); |
|
86 | - if ($fix === true) { |
|
87 | - $phpcsFile->fixer->beginChangeset(); |
|
88 | - $phpcsFile->fixer->addNewline($prev); |
|
89 | - if ($tokens[($prev + 1)]['code'] === T_WHITESPACE) { |
|
90 | - $phpcsFile->fixer->replaceToken(($prev + 1), ''); |
|
91 | - } |
|
84 | + $error = 'Each PHP statement must be on a line by itself'; |
|
85 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SameLine'); |
|
86 | + if ($fix === true) { |
|
87 | + $phpcsFile->fixer->beginChangeset(); |
|
88 | + $phpcsFile->fixer->addNewline($prev); |
|
89 | + if ($tokens[($prev + 1)]['code'] === T_WHITESPACE) { |
|
90 | + $phpcsFile->fixer->replaceToken(($prev + 1), ''); |
|
91 | + } |
|
92 | 92 | |
93 | - $phpcsFile->fixer->endChangeset(); |
|
94 | - } |
|
95 | - } else { |
|
96 | - $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no'); |
|
97 | - } |
|
93 | + $phpcsFile->fixer->endChangeset(); |
|
94 | + } |
|
95 | + } else { |
|
96 | + $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no'); |
|
97 | + } |
|
98 | 98 | |
99 | - }//end process() |
|
99 | + }//end process() |
|
100 | 100 | |
101 | 101 | |
102 | 102 | }//end class |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | */ |
37 | 37 | public function register() |
38 | 38 | { |
39 | - return array(T_SEMICOLON); |
|
39 | + return array( T_SEMICOLON ); |
|
40 | 40 | |
41 | 41 | }//end register() |
42 | 42 | |
@@ -50,50 +50,50 @@ 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 | 55 | $tokens = $phpcsFile->getTokens(); |
56 | 56 | |
57 | - $prev = $phpcsFile->findPrevious(array(T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO), ($stackPtr - 1)); |
|
58 | - if ($prev === false |
|
59 | - || $tokens[$prev]['code'] === T_OPEN_TAG |
|
60 | - || $tokens[$prev]['code'] === T_OPEN_TAG_WITH_ECHO |
|
57 | + $prev = $phpcsFile->findPrevious( array( T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO ), ( $stackPtr - 1 ) ); |
|
58 | + if ( $prev === false |
|
59 | + || $tokens[ $prev ][ 'code' ] === T_OPEN_TAG |
|
60 | + || $tokens[ $prev ][ 'code' ] === T_OPEN_TAG_WITH_ECHO |
|
61 | 61 | ) { |
62 | - $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no'); |
|
62 | + $phpcsFile->recordMetric( $stackPtr, 'Multiple statements on same line', 'no' ); |
|
63 | 63 | return; |
64 | 64 | } |
65 | 65 | |
66 | 66 | // Ignore multiple statements in a FOR condition. |
67 | - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
68 | - foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) { |
|
69 | - if (isset($tokens[$bracket]['parenthesis_owner']) === false) { |
|
67 | + if ( isset( $tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === true ) { |
|
68 | + foreach ( $tokens[ $stackPtr ][ 'nested_parenthesis' ] as $bracket ) { |
|
69 | + if ( isset( $tokens[ $bracket ][ 'parenthesis_owner' ] ) === false ) { |
|
70 | 70 | // Probably a closure sitting inside a function call. |
71 | 71 | continue; |
72 | 72 | } |
73 | 73 | |
74 | - $owner = $tokens[$bracket]['parenthesis_owner']; |
|
75 | - if ($tokens[$owner]['code'] === T_FOR) { |
|
74 | + $owner = $tokens[ $bracket ][ 'parenthesis_owner' ]; |
|
75 | + if ( $tokens[ $owner ][ 'code' ] === T_FOR ) { |
|
76 | 76 | return; |
77 | 77 | } |
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
81 | - if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
|
82 | - $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'yes'); |
|
81 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $stackPtr ][ 'line' ] ) { |
|
82 | + $phpcsFile->recordMetric( $stackPtr, 'Multiple statements on same line', 'yes' ); |
|
83 | 83 | |
84 | 84 | $error = 'Each PHP statement must be on a line by itself'; |
85 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SameLine'); |
|
86 | - if ($fix === true) { |
|
85 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SameLine' ); |
|
86 | + if ( $fix === true ) { |
|
87 | 87 | $phpcsFile->fixer->beginChangeset(); |
88 | - $phpcsFile->fixer->addNewline($prev); |
|
89 | - if ($tokens[($prev + 1)]['code'] === T_WHITESPACE) { |
|
90 | - $phpcsFile->fixer->replaceToken(($prev + 1), ''); |
|
88 | + $phpcsFile->fixer->addNewline( $prev ); |
|
89 | + if ( $tokens[ ( $prev + 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
90 | + $phpcsFile->fixer->replaceToken( ( $prev + 1 ), '' ); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | $phpcsFile->fixer->endChangeset(); |
94 | 94 | } |
95 | 95 | } else { |
96 | - $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no'); |
|
96 | + $phpcsFile->recordMetric( $stackPtr, 'Multiple statements on same line', 'no' ); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | }//end process() |
@@ -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 Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff implements PHP_CodeSniffer_Sniff |
|
29 | -{ |
|
28 | +class Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff 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_SEMICOLON); |
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 | $tokens = $phpcsFile->getTokens(); |
56 | 53 | |
57 | 54 | $prev = $phpcsFile->findPrevious(array(T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO), ($stackPtr - 1)); |
@@ -32,7 +32,7 @@ |
||
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 | { |
@@ -29,131 +29,131 @@ |
||
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( |
|
40 | - T_STRING, |
|
41 | - T_VARIABLE, |
|
42 | - ); |
|
43 | - |
|
44 | - }//end register() |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Processes this test, when one of its tokens is encountered. |
|
49 | - * |
|
50 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
51 | - * @param int $stackPtr The position of the current token |
|
52 | - * in the stack passed in $tokens. |
|
53 | - * |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
57 | - { |
|
58 | - $tokens = $phpcsFile->getTokens(); |
|
59 | - |
|
60 | - $findTokens = array_merge( |
|
61 | - PHP_CodeSniffer_Tokens::$emptyTokens, |
|
62 | - array(T_BITWISE_AND) |
|
63 | - ); |
|
64 | - |
|
65 | - $prev = $phpcsFile->findPrevious($findTokens, ($stackPtr - 1), null, true); |
|
66 | - |
|
67 | - // Skip tokens that are the names of functions or classes |
|
68 | - // within their definitions. For example: function myFunction... |
|
69 | - // "myFunction" is T_STRING but we should skip because it is not a |
|
70 | - // function or method *call*. |
|
71 | - $prevCode = $tokens[$prev]['code']; |
|
72 | - if ($prevCode === T_FUNCTION || $prevCode === T_CLASS) { |
|
73 | - return; |
|
74 | - } |
|
75 | - |
|
76 | - // If the next non-whitespace token after the function or method call |
|
77 | - // is not an opening parenthesis then it cant really be a *call*. |
|
78 | - $functionName = $stackPtr; |
|
79 | - $openBracket = $phpcsFile->findNext( |
|
80 | - PHP_CodeSniffer_Tokens::$emptyTokens, |
|
81 | - ($functionName + 1), |
|
82 | - null, |
|
83 | - true |
|
84 | - ); |
|
85 | - |
|
86 | - if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
|
87 | - return; |
|
88 | - } |
|
89 | - |
|
90 | - if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { |
|
91 | - return; |
|
92 | - } |
|
93 | - |
|
94 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
95 | - |
|
96 | - $nextSeparator = $openBracket; |
|
97 | - $find = array( |
|
98 | - T_VARIABLE, |
|
99 | - T_OPEN_SHORT_ARRAY, |
|
100 | - ); |
|
101 | - |
|
102 | - while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) { |
|
103 | - if (isset($tokens[$nextSeparator]['nested_parenthesis']) === false) { |
|
104 | - continue; |
|
105 | - } |
|
106 | - |
|
107 | - if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) { |
|
108 | - $nextSeparator = $tokens[$nextSeparator]['bracket_closer']; |
|
109 | - continue; |
|
110 | - } |
|
111 | - |
|
112 | - // Make sure the variable belongs directly to this function call |
|
113 | - // and is not inside a nested function call or array. |
|
114 | - $brackets = $tokens[$nextSeparator]['nested_parenthesis']; |
|
115 | - $lastBracket = array_pop($brackets); |
|
116 | - if ($lastBracket !== $closeBracket) { |
|
117 | - continue; |
|
118 | - } |
|
119 | - |
|
120 | - // Checking this: $value = my_function(...[*]$arg...). |
|
121 | - $tokenBefore = $phpcsFile->findPrevious( |
|
122 | - PHP_CodeSniffer_Tokens::$emptyTokens, |
|
123 | - ($nextSeparator - 1), |
|
124 | - null, |
|
125 | - true |
|
126 | - ); |
|
127 | - |
|
128 | - if ($tokens[$tokenBefore]['code'] === T_BITWISE_AND) { |
|
129 | - // Checking this: $value = my_function(...[*]&$arg...). |
|
130 | - $tokenBefore = $phpcsFile->findPrevious( |
|
131 | - PHP_CodeSniffer_Tokens::$emptyTokens, |
|
132 | - ($tokenBefore - 1), |
|
133 | - null, |
|
134 | - true |
|
135 | - ); |
|
136 | - |
|
137 | - // We have to exclude all uses of T_BITWISE_AND that are not |
|
138 | - // references. We use a blacklist approach as we prefer false |
|
139 | - // positives to not identifying a pass-by-reference call at all. |
|
140 | - $tokenCode = $tokens[$tokenBefore]['code']; |
|
141 | - if ($tokenCode === T_VARIABLE |
|
142 | - || $tokenCode === T_CLOSE_PARENTHESIS |
|
143 | - || $tokenCode === T_CLOSE_SQUARE_BRACKET |
|
144 | - || $tokenCode === T_LNUMBER |
|
145 | - || isset(PHP_CodeSniffer_Tokens::$assignmentTokens[$tokenCode]) === true |
|
146 | - ) { |
|
147 | - continue; |
|
148 | - } |
|
149 | - |
|
150 | - // T_BITWISE_AND represents a pass-by-reference. |
|
151 | - $error = 'Call-time pass-by-reference calls are prohibited'; |
|
152 | - $phpcsFile->addError($error, $tokenBefore, 'NotAllowed'); |
|
153 | - }//end if |
|
154 | - }//end while |
|
155 | - |
|
156 | - }//end process() |
|
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( |
|
40 | + T_STRING, |
|
41 | + T_VARIABLE, |
|
42 | + ); |
|
43 | + |
|
44 | + }//end register() |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Processes this test, when one of its tokens is encountered. |
|
49 | + * |
|
50 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
51 | + * @param int $stackPtr The position of the current token |
|
52 | + * in the stack passed in $tokens. |
|
53 | + * |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
57 | + { |
|
58 | + $tokens = $phpcsFile->getTokens(); |
|
59 | + |
|
60 | + $findTokens = array_merge( |
|
61 | + PHP_CodeSniffer_Tokens::$emptyTokens, |
|
62 | + array(T_BITWISE_AND) |
|
63 | + ); |
|
64 | + |
|
65 | + $prev = $phpcsFile->findPrevious($findTokens, ($stackPtr - 1), null, true); |
|
66 | + |
|
67 | + // Skip tokens that are the names of functions or classes |
|
68 | + // within their definitions. For example: function myFunction... |
|
69 | + // "myFunction" is T_STRING but we should skip because it is not a |
|
70 | + // function or method *call*. |
|
71 | + $prevCode = $tokens[$prev]['code']; |
|
72 | + if ($prevCode === T_FUNCTION || $prevCode === T_CLASS) { |
|
73 | + return; |
|
74 | + } |
|
75 | + |
|
76 | + // If the next non-whitespace token after the function or method call |
|
77 | + // is not an opening parenthesis then it cant really be a *call*. |
|
78 | + $functionName = $stackPtr; |
|
79 | + $openBracket = $phpcsFile->findNext( |
|
80 | + PHP_CodeSniffer_Tokens::$emptyTokens, |
|
81 | + ($functionName + 1), |
|
82 | + null, |
|
83 | + true |
|
84 | + ); |
|
85 | + |
|
86 | + if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
|
87 | + return; |
|
88 | + } |
|
89 | + |
|
90 | + if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { |
|
91 | + return; |
|
92 | + } |
|
93 | + |
|
94 | + $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
95 | + |
|
96 | + $nextSeparator = $openBracket; |
|
97 | + $find = array( |
|
98 | + T_VARIABLE, |
|
99 | + T_OPEN_SHORT_ARRAY, |
|
100 | + ); |
|
101 | + |
|
102 | + while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) { |
|
103 | + if (isset($tokens[$nextSeparator]['nested_parenthesis']) === false) { |
|
104 | + continue; |
|
105 | + } |
|
106 | + |
|
107 | + if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) { |
|
108 | + $nextSeparator = $tokens[$nextSeparator]['bracket_closer']; |
|
109 | + continue; |
|
110 | + } |
|
111 | + |
|
112 | + // Make sure the variable belongs directly to this function call |
|
113 | + // and is not inside a nested function call or array. |
|
114 | + $brackets = $tokens[$nextSeparator]['nested_parenthesis']; |
|
115 | + $lastBracket = array_pop($brackets); |
|
116 | + if ($lastBracket !== $closeBracket) { |
|
117 | + continue; |
|
118 | + } |
|
119 | + |
|
120 | + // Checking this: $value = my_function(...[*]$arg...). |
|
121 | + $tokenBefore = $phpcsFile->findPrevious( |
|
122 | + PHP_CodeSniffer_Tokens::$emptyTokens, |
|
123 | + ($nextSeparator - 1), |
|
124 | + null, |
|
125 | + true |
|
126 | + ); |
|
127 | + |
|
128 | + if ($tokens[$tokenBefore]['code'] === T_BITWISE_AND) { |
|
129 | + // Checking this: $value = my_function(...[*]&$arg...). |
|
130 | + $tokenBefore = $phpcsFile->findPrevious( |
|
131 | + PHP_CodeSniffer_Tokens::$emptyTokens, |
|
132 | + ($tokenBefore - 1), |
|
133 | + null, |
|
134 | + true |
|
135 | + ); |
|
136 | + |
|
137 | + // We have to exclude all uses of T_BITWISE_AND that are not |
|
138 | + // references. We use a blacklist approach as we prefer false |
|
139 | + // positives to not identifying a pass-by-reference call at all. |
|
140 | + $tokenCode = $tokens[$tokenBefore]['code']; |
|
141 | + if ($tokenCode === T_VARIABLE |
|
142 | + || $tokenCode === T_CLOSE_PARENTHESIS |
|
143 | + || $tokenCode === T_CLOSE_SQUARE_BRACKET |
|
144 | + || $tokenCode === T_LNUMBER |
|
145 | + || isset(PHP_CodeSniffer_Tokens::$assignmentTokens[$tokenCode]) === true |
|
146 | + ) { |
|
147 | + continue; |
|
148 | + } |
|
149 | + |
|
150 | + // T_BITWISE_AND represents a pass-by-reference. |
|
151 | + $error = 'Call-time pass-by-reference calls are prohibited'; |
|
152 | + $phpcsFile->addError($error, $tokenBefore, 'NotAllowed'); |
|
153 | + }//end if |
|
154 | + }//end while |
|
155 | + |
|
156 | + }//end process() |
|
157 | 157 | |
158 | 158 | |
159 | 159 | }//end class |
@@ -53,23 +53,23 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return void |
55 | 55 | */ |
56 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
56 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
57 | 57 | { |
58 | 58 | $tokens = $phpcsFile->getTokens(); |
59 | 59 | |
60 | 60 | $findTokens = array_merge( |
61 | 61 | PHP_CodeSniffer_Tokens::$emptyTokens, |
62 | - array(T_BITWISE_AND) |
|
62 | + array( T_BITWISE_AND ) |
|
63 | 63 | ); |
64 | 64 | |
65 | - $prev = $phpcsFile->findPrevious($findTokens, ($stackPtr - 1), null, true); |
|
65 | + $prev = $phpcsFile->findPrevious( $findTokens, ( $stackPtr - 1 ), null, true ); |
|
66 | 66 | |
67 | 67 | // Skip tokens that are the names of functions or classes |
68 | 68 | // within their definitions. For example: function myFunction... |
69 | 69 | // "myFunction" is T_STRING but we should skip because it is not a |
70 | 70 | // function or method *call*. |
71 | - $prevCode = $tokens[$prev]['code']; |
|
72 | - if ($prevCode === T_FUNCTION || $prevCode === T_CLASS) { |
|
71 | + $prevCode = $tokens[ $prev ][ 'code' ]; |
|
72 | + if ( $prevCode === T_FUNCTION || $prevCode === T_CLASS ) { |
|
73 | 73 | return; |
74 | 74 | } |
75 | 75 | |
@@ -78,20 +78,20 @@ discard block |
||
78 | 78 | $functionName = $stackPtr; |
79 | 79 | $openBracket = $phpcsFile->findNext( |
80 | 80 | PHP_CodeSniffer_Tokens::$emptyTokens, |
81 | - ($functionName + 1), |
|
81 | + ( $functionName + 1 ), |
|
82 | 82 | null, |
83 | 83 | true |
84 | 84 | ); |
85 | 85 | |
86 | - if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
|
86 | + if ( $tokens[ $openBracket ][ 'code' ] !== T_OPEN_PARENTHESIS ) { |
|
87 | 87 | return; |
88 | 88 | } |
89 | 89 | |
90 | - if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { |
|
90 | + if ( isset( $tokens[ $openBracket ][ 'parenthesis_closer' ] ) === false ) { |
|
91 | 91 | return; |
92 | 92 | } |
93 | 93 | |
94 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
94 | + $closeBracket = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
95 | 95 | |
96 | 96 | $nextSeparator = $openBracket; |
97 | 97 | $find = array( |
@@ -99,37 +99,37 @@ discard block |
||
99 | 99 | T_OPEN_SHORT_ARRAY, |
100 | 100 | ); |
101 | 101 | |
102 | - while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) { |
|
103 | - if (isset($tokens[$nextSeparator]['nested_parenthesis']) === false) { |
|
102 | + while ( ( $nextSeparator = $phpcsFile->findNext( $find, ( $nextSeparator + 1 ), $closeBracket ) ) !== false ) { |
|
103 | + if ( isset( $tokens[ $nextSeparator ][ 'nested_parenthesis' ] ) === false ) { |
|
104 | 104 | continue; |
105 | 105 | } |
106 | 106 | |
107 | - if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) { |
|
108 | - $nextSeparator = $tokens[$nextSeparator]['bracket_closer']; |
|
107 | + if ( $tokens[ $nextSeparator ][ 'code' ] === T_OPEN_SHORT_ARRAY ) { |
|
108 | + $nextSeparator = $tokens[ $nextSeparator ][ 'bracket_closer' ]; |
|
109 | 109 | continue; |
110 | 110 | } |
111 | 111 | |
112 | 112 | // Make sure the variable belongs directly to this function call |
113 | 113 | // and is not inside a nested function call or array. |
114 | - $brackets = $tokens[$nextSeparator]['nested_parenthesis']; |
|
115 | - $lastBracket = array_pop($brackets); |
|
116 | - if ($lastBracket !== $closeBracket) { |
|
114 | + $brackets = $tokens[ $nextSeparator ][ 'nested_parenthesis' ]; |
|
115 | + $lastBracket = array_pop( $brackets ); |
|
116 | + if ( $lastBracket !== $closeBracket ) { |
|
117 | 117 | continue; |
118 | 118 | } |
119 | 119 | |
120 | 120 | // Checking this: $value = my_function(...[*]$arg...). |
121 | 121 | $tokenBefore = $phpcsFile->findPrevious( |
122 | 122 | PHP_CodeSniffer_Tokens::$emptyTokens, |
123 | - ($nextSeparator - 1), |
|
123 | + ( $nextSeparator - 1 ), |
|
124 | 124 | null, |
125 | 125 | true |
126 | 126 | ); |
127 | 127 | |
128 | - if ($tokens[$tokenBefore]['code'] === T_BITWISE_AND) { |
|
128 | + if ( $tokens[ $tokenBefore ][ 'code' ] === T_BITWISE_AND ) { |
|
129 | 129 | // Checking this: $value = my_function(...[*]&$arg...). |
130 | 130 | $tokenBefore = $phpcsFile->findPrevious( |
131 | 131 | PHP_CodeSniffer_Tokens::$emptyTokens, |
132 | - ($tokenBefore - 1), |
|
132 | + ( $tokenBefore - 1 ), |
|
133 | 133 | null, |
134 | 134 | true |
135 | 135 | ); |
@@ -137,19 +137,19 @@ discard block |
||
137 | 137 | // We have to exclude all uses of T_BITWISE_AND that are not |
138 | 138 | // references. We use a blacklist approach as we prefer false |
139 | 139 | // positives to not identifying a pass-by-reference call at all. |
140 | - $tokenCode = $tokens[$tokenBefore]['code']; |
|
141 | - if ($tokenCode === T_VARIABLE |
|
140 | + $tokenCode = $tokens[ $tokenBefore ][ 'code' ]; |
|
141 | + if ( $tokenCode === T_VARIABLE |
|
142 | 142 | || $tokenCode === T_CLOSE_PARENTHESIS |
143 | 143 | || $tokenCode === T_CLOSE_SQUARE_BRACKET |
144 | 144 | || $tokenCode === T_LNUMBER |
145 | - || isset(PHP_CodeSniffer_Tokens::$assignmentTokens[$tokenCode]) === true |
|
145 | + || isset( PHP_CodeSniffer_Tokens::$assignmentTokens[ $tokenCode ] ) === true |
|
146 | 146 | ) { |
147 | 147 | continue; |
148 | 148 | } |
149 | 149 | |
150 | 150 | // T_BITWISE_AND represents a pass-by-reference. |
151 | 151 | $error = 'Call-time pass-by-reference calls are prohibited'; |
152 | - $phpcsFile->addError($error, $tokenBefore, 'NotAllowed'); |
|
152 | + $phpcsFile->addError( $error, $tokenBefore, 'NotAllowed' ); |
|
153 | 153 | }//end if |
154 | 154 | }//end while |
155 | 155 |
@@ -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 Generic_Sniffs_Functions_CallTimePassByReferenceSniff implements PHP_CodeSniffer_Sniff |
|
29 | -{ |
|
28 | +class Generic_Sniffs_Functions_CallTimePassByReferenceSniff 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( |
40 | 38 | T_STRING, |
41 | 39 | T_VARIABLE, |
@@ -53,8 +51,7 @@ discard block |
||
53 | 51 | * |
54 | 52 | * @return void |
55 | 53 | */ |
56 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
57 | - { |
|
54 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
58 | 55 | $tokens = $phpcsFile->getTokens(); |
59 | 56 | |
60 | 57 | $findTokens = array_merge( |