Skip to content

Commit

Permalink
Various sniffs: simplify skipping the rest of the file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Nov 8, 2024
1 parent 4673027 commit 47c73a8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Yoast/Sniffs/Commenting/FileCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
}
Expand All @@ -136,6 +136,6 @@ public function process( File $phpcsFile, $stackPtr ) {
'Unnecessary'
);

return ( $phpcsFile->numTokens + 1 );
return $phpcsFile->numTokens;
}
}
6 changes: 3 additions & 3 deletions Yoast/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Yoast/Sniffs/Files/TestDoublesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand All @@ -81,7 +81,7 @@ public function process( File $phpcsFile, $stackPtr ) {
'MissingBasePath'
);

return ( $phpcsFile->numTokens + 1 );
return $phpcsFile->numTokens;
}

if ( empty( $this->doubles_path ) ) {
Expand All @@ -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' ) ) {
Expand Down

0 comments on commit 47c73a8

Please sign in to comment.