From 57d9e89f4d5b49ba08b43f03b2041467438c75c3 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 23 Feb 2024 11:31:13 -0300 Subject: [PATCH] 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. --- src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php b/src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php index d014908320..5a1fde6f34 100644 --- a/src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php +++ b/src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php @@ -49,13 +49,13 @@ public function register() * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * - * @return void + * @return int */ public function process(File $phpcsFile, $stackPtr) { // The BOM will be the very first token in the file. if ($stackPtr !== 0) { - return; + return $phpcsFile->numTokens; } $tokens = $phpcsFile->getTokens(); @@ -68,12 +68,14 @@ public function process(File $phpcsFile, $stackPtr) $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'); - return; + return $phpcsFile->numTokens; } } $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'no'); + return $phpcsFile->numTokens; + }//end process()