PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file EndFileNewlineSniff.php

Documentation is available at EndFileNewlineSniff.php

  1. <?php
  2. /**
  3.  * Ensures the file ends with a newline character.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Files;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class EndFileNewlineSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(T_OPEN_TAG);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this sniff, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token in
  36.      *                                                the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         if ($phpcsFile->findNext(T_INLINE_HTML($stackPtr + 1)) !== false{
  43.             return ($phpcsFile->numTokens + 1);
  44.         }
  45.  
  46.         // Skip to the end of the file.
  47.         $tokens    $phpcsFile->getTokens();
  48.         $lastToken ($phpcsFile->numTokens - 1);
  49.  
  50.         if ($tokens[$lastToken]['content'=== ''{
  51.             $lastToken--;
  52.         }
  53.  
  54.         // Hard-coding the expected \n in this sniff as it is PSR-2 specific and
  55.         // PSR-2 enforces the use of unix style newlines.
  56.         if (substr($tokens[$lastToken]['content']-1!== "\n"{
  57.             $error 'Expected 1 newline at end of file; 0 found';
  58.             $fix   $phpcsFile->addFixableError($error$lastToken'NoneFound');
  59.             if ($fix === true{
  60.                 $phpcsFile->fixer->addNewline($lastToken);
  61.             }
  62.  
  63.             $phpcsFile->recordMetric($stackPtr'Number of newlines at EOF''0');
  64.             return ($phpcsFile->numTokens + 1);
  65.         }
  66.  
  67.         // Go looking for the last non-empty line.
  68.         $lastLine $tokens[$lastToken]['line'];
  69.         if ($tokens[$lastToken]['code'=== T_WHITESPACE
  70.             || $tokens[$lastToken]['code'=== T_DOC_COMMENT_WHITESPACE
  71.         {
  72.             $lastCode $phpcsFile->findPrevious(array(T_WHITESPACET_DOC_COMMENT_WHITESPACE)($lastToken - 1)nulltrue);
  73.         else {
  74.             $lastCode $lastToken;
  75.         }
  76.  
  77.         $lastCodeLine $tokens[$lastCode]['line'];
  78.         $blankLines   ($lastLine $lastCodeLine + 1);
  79.         $phpcsFile->recordMetric($stackPtr'Number of newlines at EOF'$blankLines);
  80.  
  81.         if ($blankLines > 1{
  82.             $error 'Expected 1 blank line at end of file; %s found';
  83.             $data  = array($blankLines);
  84.             $fix   $phpcsFile->addFixableError($error$lastCode'TooMany'$data);
  85.  
  86.             if ($fix === true{
  87.                 $phpcsFile->fixer->beginChangeset();
  88.                 $phpcsFile->fixer->replaceToken($lastCodertrim($tokens[$lastCode]['content']));
  89.                 for ($i ($lastCode + 1)$i $lastToken$i++{
  90.                     $phpcsFile->fixer->replaceToken($i'');
  91.                 }
  92.  
  93.                 $phpcsFile->fixer->replaceToken($lastToken$phpcsFile->eolChar);
  94.                 $phpcsFile->fixer->endChangeset();
  95.             }
  96.         }
  97.  
  98.         // Skip the rest of the file.
  99.         return ($phpcsFile->numTokens + 1);
  100.  
  101.     }//end process()
  102.  
  103.  
  104. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.