Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.7.2

Request #18194 A sniff detecting BOM (byte order mark) for *.php files
Submitted: 2011-01-13 20:18 UTC
From: ryba Assigned: squiz
Status: Closed Package: PHP_CodeSniffer (version SVN)
PHP Version: Irrelevant OS: Ubuntu Linux 10.10
Roadmaps: (Not assigned)    
Subscription  


 [2011-01-13 20:18 UTC] ryba (Piotr Karas)
Description: ------------ Perhaps some folks would find it useful to have BOMs automatically detected in their applications? I attach my own implementation, feel free to use it and/or improve and/or add it to generic sniffs.

Comments

 [2011-01-13 20:21 UTC] ryba (Piotr Karas)
/** * A simple sniff detecting BOMs that may corrupt application work. * * @see http://en.wikipedia.org/wiki/Byte_order_mark * @author Piotr Kara? * @copyright 2010-2011 mediaSELF Sp. z o.o. */ class msCollection_Sniffs_Files_ByteOrderMarkSniff implements PHP_CodeSniffer_Sniff { /* (non-PHPdoc) * @see PHP_CodeSniffer_Sniff::register() */ public function register() { return array( T_INLINE_HTML ); } /** * List of supported BOM definitions. * * Use encoding names as keys and hex BOM representations as values. * * @return array(string=>string) */ protected function bomDefinitions() { return array( 'UTF-8' => 'efbbbf', 'UTF-16 (BE)' => 'feff', 'UTF-16 (LE)' => 'fffe' ); } /** * Process tokens. * * Actually, only proceed when we're at index 0, this should be the only case * that will contain BOM. Then check if BOM definition matches what * we've found as file's inline HTML. Inline HTML could be longer than just BOM * so make sure you test as much as needed. * * @param PHP_CodeSniffer_File $phpcsFile * @param unknown_type $stackPtr */ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { if ( $stackPtr !== 0 ) { return; } $tokens = $phpcsFile->getTokens(); $html = $tokens[$stackPtr]['content']; foreach ( $this->bomDefinitions() as $bomName => $expectedBomHex ) { $bomByteLength = strlen( $expectedBomHex ) / 2; $htmlBomHex = bin2hex( substr( $html, 0, $bomByteLength ) ); if ( $htmlBomHex === $expectedBomHex ) { $errorData = array( $bomName ); $error = 'File contains %s byte order mark (BOM) - this may corrupt your application'; $phpcsFile->addError( $error, $stackPtr, 'Found', $errorData ); break; } } } }
 [2011-04-15 09:07 UTC] squiz (Greg Sherwood)
-Status: Open +Status: Closed -Assigned To: +Assigned To: squiz
This bug has been fixed in SVN. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. Thanks a lot for the contribution.