From 47c73a88d21a87628bf3fb51aac2b220ebcdfe31 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 7 Jun 2024 01:50:37 +0200 Subject: [PATCH] Various sniffs: simplify skipping the rest of the file This commit updates various sniffs to use `return $phpcsFile->numTokens` instead of `return ($phpcsFile->numTokens + 1)`. If a sniff file contains 50 tokens, the last `$stackPtr` will be 49, so returning `50` will already get us passed the end of the token stack and the `+ 1` is redundant. Includes changing a plain `return` statements to `return $phpcsFile->numTokens` for efficiency. --- Yoast/Sniffs/Commenting/FileCommentSniff.php | 6 +++--- Yoast/Sniffs/Files/FileNameSniff.php | 6 +++--- Yoast/Sniffs/Files/TestDoublesSniff.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Yoast/Sniffs/Commenting/FileCommentSniff.php b/Yoast/Sniffs/Commenting/FileCommentSniff.php index b7f42de..d814001 100644 --- a/Yoast/Sniffs/Commenting/FileCommentSniff.php +++ b/Yoast/Sniffs/Commenting/FileCommentSniff.php @@ -93,7 +93,7 @@ public function process( File $phpcsFile, $stackPtr ) { if ( $comment_start === false || $tokens[ $comment_start ]['code'] !== \T_DOC_COMMENT_OPEN_TAG ) { // No file comment found, we're good. - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } // Respect phpcs:disable comments in the file docblock. @@ -110,7 +110,7 @@ public function process( File $phpcsFile, $stackPtr ) { || isset( $tokens[ $i ]['sniffCodes']['Yoast.Commenting.FileComment.Unnecessary'] ) === true ) { // Applicable disable annotation found. - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } } } @@ -136,6 +136,6 @@ public function process( File $phpcsFile, $stackPtr ) { 'Unnecessary' ); - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } } diff --git a/Yoast/Sniffs/Files/FileNameSniff.php b/Yoast/Sniffs/Files/FileNameSniff.php index 6192c2e..bfb6a8a 100644 --- a/Yoast/Sniffs/Files/FileNameSniff.php +++ b/Yoast/Sniffs/Files/FileNameSniff.php @@ -144,7 +144,7 @@ public function process( File $phpcsFile, $stackPtr ) { $file = TextStrings::stripQuotes( $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { - return ( $phpcsFile->numTokens + 1 ); // @codeCoverageIgnore + return $phpcsFile->numTokens; // @codeCoverageIgnore } // Respect phpcs:disable comments as long as they are not accompanied by an enable. @@ -167,7 +167,7 @@ public function process( File $phpcsFile, $stackPtr ) { if ( $i === false ) { // The entire (rest of the) file is disabled. - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } } } @@ -278,7 +278,7 @@ public function process( File $phpcsFile, $stackPtr ) { } // Only run this sniff once per file, no need to run it again. - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } /** diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 1cd18f3..58def16 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -71,7 +71,7 @@ public function process( File $phpcsFile, $stackPtr ) { $file = TextStrings::stripQuotes( $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { - return; // @codeCoverageIgnore + return $phpcsFile->numTokens; // @codeCoverageIgnore } if ( ! isset( $phpcsFile->config->basepath ) ) { @@ -81,7 +81,7 @@ public function process( File $phpcsFile, $stackPtr ) { 'MissingBasePath' ); - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } if ( empty( $this->doubles_path ) ) { @@ -92,7 +92,7 @@ public function process( File $phpcsFile, $stackPtr ) { 'NoDoublesPathProperty' ); - return ( $phpcsFile->numTokens + 1 ); + return $phpcsFile->numTokens; } if ( ! isset( $this->target_paths ) || \defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {