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

Source for file ByteOrderMarkSniff.php

Documentation is available at ByteOrderMarkSniff.php

  1. <?php
  2. /**
  3.  * A simple sniff for detecting BOMs that may corrupt application work.
  4.  *
  5.  * @author    Piotr Karas <office@mediaself.pl>
  6.  * @author    Greg Sherwood <gsherwood@squiz.net>
  7.  * @copyright 2010-2014 mediaSELF Sp. z o.o.
  8.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  9.  */
  10.  
  11. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Files;
  12.  
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class ByteOrderMarkSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * List of supported BOM definitions.
  21.      *
  22.      * Use encoding names as keys and hex BOM representations as values.
  23.      *
  24.      * @var array 
  25.      */
  26.     protected $bomDefinitions = array(
  27.                                  'UTF-8'       => 'efbbbf',
  28.                                  'UTF-16 (BE)' => 'feff',
  29.                                  'UTF-16 (LE)' => 'fffe',
  30.                                 );
  31.  
  32.  
  33.     /**
  34.      * Returns an array of tokens this test wants to listen for.
  35.      *
  36.      * @return array 
  37.      */
  38.     public function register()
  39.     {
  40.         return array(T_INLINE_HTML);
  41.  
  42.     }//end register()
  43.  
  44.  
  45.     /**
  46.      * Processes this sniff, when one of its tokens is encountered.
  47.      *
  48.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  49.      * @param int                         $stackPtr  The position of the current token in
  50.      *                                                the stack passed in $tokens.
  51.      *
  52.      * @return void 
  53.      */
  54.     public function process(File $phpcsFile$stackPtr)
  55.     {
  56.         // The BOM will be the very first token in the file.
  57.         if ($stackPtr !== 0{
  58.             return;
  59.         }
  60.  
  61.         $tokens $phpcsFile->getTokens();
  62.  
  63.         foreach ($this->bomDefinitions as $bomName => $expectedBomHex{
  64.             $bomByteLength (strlen($expectedBomHex/ 2);
  65.             $htmlBomHex    bin2hex(substr($tokens[$stackPtr]['content']0$bomByteLength));
  66.             if ($htmlBomHex === $expectedBomHex{
  67.                 $errorData = array($bomName);
  68.                 $error     'File contains %s byte order mark, which may corrupt your application';
  69.                 $phpcsFile->addError($error$stackPtr'Found'$errorData);
  70.                 $phpcsFile->recordMetric($stackPtr'Using byte order mark''yes');
  71.                 return;
  72.             }
  73.         }
  74.  
  75.         $phpcsFile->recordMetric($stackPtr'Using byte order mark''no');
  76.  
  77.     }//end process()
  78.  
  79.  
  80. }//end class

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