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