Source for file ByteOrderMarkSniff.php
Documentation is available at ByteOrderMarkSniff.php
* A simple sniff for detecting BOMs that may corrupt application work.
* @author Piotr Karas <office@mediaself.pl>
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2010-2014 mediaSELF Sp. z o.o.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Files;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class ByteOrderMarkSniff implements Sniff
* List of supported BOM definitions.
* Use encoding names as keys and hex BOM representations as values.
protected $bomDefinitions = array (
* Returns an array of tokens this test wants to listen for.
public function register ()
return array (T_INLINE_HTML );
* Processes this sniff, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
public function process (File $phpcsFile, $stackPtr)
// The BOM will be the very first token in the file.
$tokens = $phpcsFile->getTokens ();
foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
$bomByteLength = (strlen($expectedBomHex) / 2 );
$htmlBomHex = bin2hex(substr($tokens[$stackPtr]['content'], 0 , $bomByteLength));
if ($htmlBomHex === $expectedBomHex) {
$errorData = array ($bomName);
$error = 'File contains %s byte order mark, which may corrupt your application';
$phpcsFile->addError ($error, $stackPtr, 'Found', $errorData);
$phpcsFile->recordMetric ($stackPtr, 'Using byte order mark', 'yes');
$phpcsFile->recordMetric ($stackPtr, 'Using byte order mark', 'no');
Documentation generated on Mon, 11 Mar 2019 15:27:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|