comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/PostStatementCommentSniff.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * Drupal_Sniffs_Commenting_PostStatementCommentSniff.
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Largely copied from Squiz_Sniffs_Commenting_PostStatementCommentSniff but we want
12 * the fixer to move the comment to the previous line.
13 *
14 * @category PHP
15 * @package PHP_CodeSniffer
16 * @link http://pear.php.net/package/PHP_CodeSniffer
17 */
18 class Drupal_Sniffs_Commenting_PostStatementCommentSniff implements PHP_CodeSniffer_Sniff
19 {
20
21 /**
22 * A list of tokenizers this sniff supports.
23 *
24 * @var array
25 */
26 public $supportedTokenizers = array('PHP');
27
28
29 /**
30 * Returns an array of tokens this test wants to listen for.
31 *
32 * @return array
33 */
34 public function register()
35 {
36 return array(T_COMMENT);
37
38 }//end register()
39
40
41 /**
42 * Processes this sniff, when one of its tokens is encountered.
43 *
44 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
45 * @param int $stackPtr The position of the current token in the
46 * stack passed in $tokens.
47 *
48 * @return void
49 */
50 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
51 {
52 $tokens = $phpcsFile->getTokens();
53
54 if (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') {
55 return;
56 }
57
58 $commentLine = $tokens[$stackPtr]['line'];
59 $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
60
61 if ($tokens[$lastContent]['line'] !== $commentLine) {
62 return;
63 }
64
65 if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
66 return;
67 }
68
69 // Special case for JS files.
70 if ($tokens[$lastContent]['code'] === T_COMMA
71 || $tokens[$lastContent]['code'] === T_SEMICOLON
72 ) {
73 $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($lastContent - 1), null, true);
74 if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
75 return;
76 }
77 }
78
79 $error = 'Comments may not appear after statements';
80 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
81 if ($fix === true) {
82 if ($tokens[$lastContent]['code'] === T_OPEN_TAG) {
83 $phpcsFile->fixer->addNewlineBefore($stackPtr);
84 return;
85 }
86
87 $lineStart = $stackPtr;
88 while ($tokens[$lineStart]['line'] === $tokens[$stackPtr]['line']
89 && $tokens[$lineStart]['code'] !== T_OPEN_TAG
90 ) {
91 $lineStart--;
92 }
93
94 $phpcsFile->fixer->beginChangeset();
95 $phpcsFile->fixer->addContent($lineStart, $tokens[$stackPtr]['content']);
96 $phpcsFile->fixer->replaceToken($stackPtr, $phpcsFile->eolChar);
97 $phpcsFile->fixer->endChangeset();
98 }
99
100 }//end process()
101
102
103 }//end class