Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various sniffs: simplify skipping the rest of the file #2506

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 few plain `return` statements to `return $phpcsFile->numTokens` for efficiency.
  • Loading branch information
jrfnl committed Nov 8, 2024
commit 382448c4ed3ab0526503bb164d6e8a50361333dd
8 changes: 4 additions & 4 deletions WordPress/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ public function process_token( $stackPtr ) {
// Usage of `stripQuotes` is to ensure `stdin_path` passed by IDEs does not include quotes.
$file = TextStrings::stripQuotes( $this->phpcsFile->getFileName() );
if ( 'STDIN' === $file ) {
return;
return $this->phpcsFile->numTokens;
}

$class_ptr = $this->phpcsFile->findNext( \T_CLASS, $stackPtr );
@@ -163,7 +163,7 @@ public function process_token( $stackPtr ) {
* This rule should not be applied to test classes (at all).
* @link https://github.com/WordPress/WordPress-Coding-Standards/issues/1995
*/
return;
return $this->phpcsFile->numTokens;
}

// Respect phpcs:disable comments as long as they are not accompanied by an enable.
@@ -184,7 +184,7 @@ public function process_token( $stackPtr ) {

if ( false === $i ) {
// The entire (rest of the) file is disabled.
return;
return $this->phpcsFile->numTokens;
}
}
}
@@ -204,7 +204,7 @@ public function process_token( $stackPtr ) {
}

// Only run this sniff once per file, no need to run it again.
return ( $this->phpcsFile->numTokens + 1 );
return $this->phpcsFile->numTokens;
}

/**
12 changes: 6 additions & 6 deletions WordPress/Sniffs/Utils/I18nTextDomainFixerSniff.php
Original file line number Diff line number Diff line change
@@ -394,7 +394,7 @@ public function process_token( $stackPtr ) {
if ( ! is_string( $this->new_text_domain )
|| '' === $this->new_text_domain
) {
return ( $this->phpcsFile->numTokens + 1 );
return $this->phpcsFile->numTokens;
}

if ( isset( $this->old_text_domain ) ) {
@@ -403,7 +403,7 @@ public function process_token( $stackPtr ) {
if ( ! is_array( $this->old_text_domain )
|| array() === $this->old_text_domain
) {
return ( $this->phpcsFile->numTokens + 1 );
return $this->phpcsFile->numTokens;
}
}

@@ -421,7 +421,7 @@ public function process_token( $stackPtr ) {
array( $this->new_text_domain )
);

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

if ( preg_match( '`^[a-z0-9-]+$`', $this->new_text_domain ) !== 1 ) {
@@ -432,14 +432,14 @@ public function process_token( $stackPtr ) {
array( $this->new_text_domain )
);

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

// If the text domain passed both validations, it should be considered valid.
$this->is_valid = true;

} elseif ( false === $this->is_valid ) {
return ( $this->phpcsFile->numTokens + 1 );
return $this->phpcsFile->numTokens;
}

if ( isset( $this->tab_width ) === false ) {
@@ -685,7 +685,7 @@ public function process_comments( $stackPtr ) {
if ( isset( $this->phpcsFile->tokenizerType ) && 'CSS' === $this->phpcsFile->tokenizerType ) {
if ( 'style.css' !== $file_name && ! defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {
// CSS files only need to be examined for the file header.
return ( $this->phpcsFile->numTokens + 1 );
return $this->phpcsFile->numTokens;
}

$regex = $this->theme_header_regex;
Loading