|
11 | 11 |
|
12 | 12 | use PHP_CodeSniffer\Sniffs\Sniff; |
13 | 13 | use PHP_CodeSniffer\Files\File; |
| 14 | +use PHP_CodeSniffer\Util\Tokens; |
14 | 15 |
|
15 | 16 | class ClosingTagSniff implements Sniff |
16 | 17 | { |
@@ -51,13 +52,22 @@ public function process(File $phpcsFile, $stackPtr) |
51 | 52 | $error = 'A closing tag is not permitted at the end of a PHP file'; |
52 | 53 | $fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); |
53 | 54 | if ($fix === true) { |
54 | | - $phpcsFile->fixer->replaceToken($last, ''); |
| 55 | + $phpcsFile->fixer->beginChangeset(); |
| 56 | + $phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar); |
| 57 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($last - 1), null, true); |
| 58 | + if ($tokens[$prev]['code'] !== T_SEMICOLON |
| 59 | + && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET |
| 60 | + ) { |
| 61 | + $phpcsFile->fixer->addContent($prev, ';'); |
| 62 | + } |
| 63 | + |
| 64 | + $phpcsFile->fixer->endChangeset(); |
55 | 65 | } |
56 | 66 |
|
57 | 67 | $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at EOF', 'yes'); |
58 | 68 | } else { |
59 | 69 | $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at EOF', 'no'); |
60 | | - } |
| 70 | + }//end if |
61 | 71 |
|
62 | 72 | // Ignore the rest of the file. |
63 | 73 | return ($phpcsFile->numTokens + 1); |
|
0 commit comments