annotate vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Files/EndFileNewlineSniff.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Drupal_Sniffs_Files_EndFileNewlineSniff.
Chris@0 4 *
Chris@0 5 * @category PHP
Chris@0 6 * @package PHP_CodeSniffer
Chris@0 7 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 8 */
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Ensures the file ends with a newline character.
Chris@0 12 *
Chris@0 13 * Largely copied from PSR2, but we need to run it on *.txt files and templates as
Chris@0 14 * well.
Chris@0 15 *
Chris@0 16 * @category PHP
Chris@0 17 * @package PHP_CodeSniffer
Chris@0 18 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 19 */
Chris@0 20 class Drupal_Sniffs_Files_EndFileNewlineSniff implements PHP_CodeSniffer_Sniff
Chris@0 21 {
Chris@0 22
Chris@0 23
Chris@0 24 /**
Chris@0 25 * A list of tokenizers this sniff supports.
Chris@0 26 *
Chris@0 27 * @var array
Chris@0 28 */
Chris@0 29 public $supportedTokenizers = array(
Chris@0 30 'PHP',
Chris@0 31 'JS',
Chris@0 32 'CSS',
Chris@0 33 );
Chris@0 34
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Returns an array of tokens this test wants to listen for.
Chris@0 38 *
Chris@0 39 * @return array
Chris@0 40 */
Chris@0 41 public function register()
Chris@0 42 {
Chris@0 43 return array(
Chris@0 44 T_OPEN_TAG,
Chris@0 45 T_INLINE_HTML,
Chris@0 46 );
Chris@0 47
Chris@0 48 }//end register()
Chris@0 49
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Processes this sniff, when one of its tokens is encountered.
Chris@0 53 *
Chris@0 54 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
Chris@0 55 * @param int $stackPtr The position of the current token in
Chris@0 56 * the stack passed in $tokens.
Chris@0 57 *
Chris@0 58 * @return void
Chris@0 59 */
Chris@0 60 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
Chris@0 61 {
Chris@0 62 // Skip to the end of the file.
Chris@0 63 $tokens = $phpcsFile->getTokens();
Chris@0 64 if ($phpcsFile->tokenizerType === 'PHP') {
Chris@0 65 $lastToken = ($phpcsFile->numTokens - 1);
Chris@0 66 } else {
Chris@0 67 // JS and CSS have an artificial token at the end which we have to
Chris@0 68 // ignore.
Chris@0 69 $lastToken = ($phpcsFile->numTokens - 2);
Chris@0 70 }
Chris@0 71
Chris@0 72 // Hard-coding the expected \n in this sniff as it is PSR-2 specific and
Chris@0 73 // PSR-2 enforces the use of unix style newlines.
Chris@0 74 if (substr($tokens[$lastToken]['content'], -1) !== "\n") {
Chris@0 75 $error = 'Expected 1 newline at end of file; 0 found';
Chris@0 76 $fix = $phpcsFile->addFixableError($error, $lastToken, 'NoneFound');
Chris@0 77 if ($fix === true) {
Chris@0 78 $phpcsFile->fixer->addNewline($lastToken);
Chris@0 79 }
Chris@0 80
Chris@0 81 $phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', '0');
Chris@0 82 return ($phpcsFile->numTokens + 1);
Chris@0 83 }
Chris@0 84
Chris@0 85 // Go looking for the last non-empty line.
Chris@0 86 $lastLine = $tokens[$lastToken]['line'];
Chris@0 87 if ($tokens[$lastToken]['code'] === T_WHITESPACE) {
Chris@0 88 $lastCode = $phpcsFile->findPrevious(T_WHITESPACE, ($lastToken - 1), null, true);
Chris@0 89 } else if ($tokens[$lastToken]['code'] === T_INLINE_HTML) {
Chris@0 90 $lastCode = $lastToken;
Chris@0 91 while ($lastCode > 0 && trim($tokens[$lastCode]['content']) === '') {
Chris@0 92 $lastCode--;
Chris@0 93 }
Chris@0 94 } else {
Chris@0 95 $lastCode = $lastToken;
Chris@0 96 }
Chris@0 97
Chris@0 98 $lastCodeLine = $tokens[$lastCode]['line'];
Chris@0 99 $blankLines = ($lastLine - $lastCodeLine + 1);
Chris@0 100 $phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', $blankLines);
Chris@0 101
Chris@0 102 if ($blankLines > 1) {
Chris@0 103 $error = 'Expected 1 newline at end of file; %s found';
Chris@0 104 $data = array($blankLines);
Chris@0 105 $fix = $phpcsFile->addFixableError($error, $lastCode, 'TooMany', $data);
Chris@0 106
Chris@0 107 if ($fix === true) {
Chris@0 108 $phpcsFile->fixer->beginChangeset();
Chris@0 109 $phpcsFile->fixer->replaceToken($lastCode, rtrim($tokens[$lastCode]['content']));
Chris@0 110 for ($i = ($lastCode + 1); $i < $lastToken; $i++) {
Chris@0 111 $phpcsFile->fixer->replaceToken($i, '');
Chris@0 112 }
Chris@0 113
Chris@0 114 $phpcsFile->fixer->replaceToken($lastToken, $phpcsFile->eolChar);
Chris@0 115 $phpcsFile->fixer->endChangeset();
Chris@0 116 }
Chris@0 117 }
Chris@0 118
Chris@0 119 // Skip the rest of the file.
Chris@0 120 return ($phpcsFile->numTokens + 1);
Chris@0 121
Chris@0 122 }//end process()
Chris@0 123
Chris@0 124
Chris@0 125 }//end class