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\Generic\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.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                    'CSS',
  27.                                   );
  28.  
  29.  
  30.     /**
  31.      * Returns an array of tokens this test wants to listen for.
  32.      *
  33.      * @return array 
  34.      */
  35.     public function register()
  36.     {
  37.         return array(T_OPEN_TAG);
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this sniff, when one of its tokens is encountered.
  44.      *
  45.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  46.      * @param int                         $stackPtr  The position of the current token in
  47.      *                                                the stack passed in $tokens.
  48.      *
  49.      * @return int 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         // Skip to the end of the file.
  54.         $tokens   $phpcsFile->getTokens();
  55.         $stackPtr ($phpcsFile->numTokens - 1);
  56.  
  57.         if ($tokens[$stackPtr]['content'=== ''{
  58.             $stackPtr--;
  59.         }
  60.  
  61.         $eolCharLen strlen($phpcsFile->eolChar);
  62.         $lastChars  substr($tokens[$stackPtr]['content']($eolCharLen * -1));
  63.         if ($lastChars !== $phpcsFile->eolChar{
  64.             $phpcsFile->recordMetric($stackPtr'Newline at EOF''no');
  65.  
  66.             $error 'File must end with a newline character';
  67.             $fix   $phpcsFile->addFixableError($error$stackPtr'NotFound');
  68.             if ($fix === true{
  69.                 $phpcsFile->fixer->addNewline($stackPtr);
  70.             }
  71.         else {
  72.             $phpcsFile->recordMetric($stackPtr'Newline at EOF''yes');
  73.         }
  74.  
  75.         // Ignore the rest of the file.
  76.         return ($phpcsFile->numTokens + 1);
  77.  
  78.     }//end process()
  79.  
  80.  
  81. }//end class

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