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

Source for file LineEndingsSniff.php

Documentation is available at LineEndingsSniff.php

  1. <?php
  2. /**
  3.  * Checks that end of line characters are correct.
  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 LineEndingsSniff 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.      * The valid EOL character.
  31.      *
  32.      * @var string 
  33.      */
  34.     public $eolChar '\n';
  35.  
  36.  
  37.     /**
  38.      * Returns an array of tokens this test wants to listen for.
  39.      *
  40.      * @return array 
  41.      */
  42.     public function register()
  43.     {
  44.         return array(T_OPEN_TAG);
  45.  
  46.     }//end register()
  47.  
  48.  
  49.     /**
  50.      * Processes this sniff, when one of its tokens is encountered.
  51.      *
  52.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  53.      * @param int                         $stackPtr  The position of the current token in
  54.      *                                                the stack passed in $tokens.
  55.      *
  56.      * @return int 
  57.      */
  58.     public function process(File $phpcsFile$stackPtr)
  59.     {
  60.         $found $phpcsFile->eolChar;
  61.         $found str_replace("\n"'\n'$found);
  62.         $found str_replace("\r"'\r'$found);
  63.  
  64.         $phpcsFile->recordMetric($stackPtr'EOL char'$found);
  65.  
  66.         if ($found === $this->eolChar{
  67.             // Ignore the rest of the file.
  68.             return ($phpcsFile->numTokens + 1);
  69.         }
  70.  
  71.         // Check for single line files without an EOL. This is a very special
  72.         // case and the EOL char is set to \n when this happens.
  73.         if ($found === '\n'{
  74.             $tokens    $phpcsFile->getTokens();
  75.             $lastToken ($phpcsFile->numTokens - 1);
  76.             if ($tokens[$lastToken]['line'=== 1
  77.                 && $tokens[$lastToken]['content'!== "\n"
  78.             {
  79.                 return;
  80.             }
  81.         }
  82.  
  83.         $error    'End of line character is invalid; expected "%s" but found "%s"';
  84.         $expected $this->eolChar;
  85.         $expected str_replace("\n"'\n'$expected);
  86.         $expected str_replace("\r"'\r'$expected);
  87.         $data     = array(
  88.                      $expected,
  89.                      $found,
  90.                     );
  91.  
  92.         // Errors are always reported on line 1, no matter where the first PHP tag is.
  93.         $fix $phpcsFile->addFixableError($error0'InvalidEOLChar'$data);
  94.  
  95.         if ($fix === true{
  96.             $tokens $phpcsFile->getTokens();
  97.             switch ($this->eolChar{
  98.             case '\n':
  99.                 $eolChar "\n";
  100.                 break;
  101.             case '\r':
  102.                 $eolChar "\r";
  103.                 break;
  104.             case '\r\n':
  105.                 $eolChar "\r\n";
  106.                 break;
  107.             default:
  108.                 $eolChar $this->eolChar;
  109.                 break;
  110.             }
  111.  
  112.             for ($i = 0; $i $phpcsFile->numTokens; $i++{
  113.                 if (isset($tokens[($i + 1)]=== false
  114.                     || $tokens[($i + 1)]['line'$tokens[$i]['line']
  115.                 {
  116.                     // Token is the last on a line.
  117.                     if (isset($tokens[$i]['orig_content']=== true{
  118.                         $tokenContent $tokens[$i]['orig_content'];
  119.                     else {
  120.                         $tokenContent $tokens[$i]['content'];
  121.                     }
  122.  
  123.                     $newContent  rtrim($tokenContent"\r\n");
  124.                     $newContent .= $eolChar;
  125.                     $phpcsFile->fixer->replaceToken($i$newContent);
  126.                 }
  127.             }
  128.         }//end if
  129.  
  130.         // Ignore the rest of the file.
  131.         return ($phpcsFile->numTokens + 1);
  132.  
  133.     }//end process()
  134.  
  135.  
  136. }//end class

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