Skip to content

Commit 669323b

Browse files
committed
Generic/ByteOrderMark: small performance improvement
The BOM character must be the first character of the file. This means that this sniff only needs to check the file once, but its code was being executed for each occurrence of the T_INLINE_HTML token. This commit changes the sniff code to return the number of tokens in all of its exit points to ensure that PHPCS executes it just a single time per file.
1 parent 45c61f5 commit 669323b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function register()
4949
* @param int $stackPtr The position of the current token in
5050
* the stack passed in $tokens.
5151
*
52-
* @return void
52+
* @return int
5353
*/
5454
public function process(File $phpcsFile, $stackPtr)
5555
{
5656
// The BOM will be the very first token in the file.
5757
if ($stackPtr !== 0) {
58-
return;
58+
return $phpcsFile->numTokens;
5959
}
6060

6161
$tokens = $phpcsFile->getTokens();
@@ -68,12 +68,14 @@ public function process(File $phpcsFile, $stackPtr)
6868
$error = 'File contains %s byte order mark, which may corrupt your application';
6969
$phpcsFile->addError($error, $stackPtr, 'Found', $errorData);
7070
$phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'yes');
71-
return;
71+
return $phpcsFile->numTokens;
7272
}
7373
}
7474

7575
$phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'no');
7676

77+
return $phpcsFile->numTokens;
78+
7779
}//end process()
7880

7981

0 commit comments

Comments
 (0)