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

Performance: more selective sniffing for efficiency #584

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ZoninatorSniff extends Sniff {
* @return array(int)
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}


Expand Down
12 changes: 6 additions & 6 deletions WordPressVIPMinimum/Sniffs/Functions/CheckReturnValueSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CheckReturnValueSniff extends Sniff {
* @return array(int)
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand All @@ -86,7 +86,7 @@ public function process_token( $stackPtr ) {
*/
private function isFunctionCall( $stackPtr ) {

if ( in_array( $this->tokens[ $stackPtr ]['code'], Tokens::$functionNameTokens, true ) === false ) {
if ( $this->tokens[ $stackPtr ]['code'] !== T_STRING ) {
return false;
}

Expand Down Expand Up @@ -162,14 +162,14 @@ public function findDirectFunctionCalls( $stackPtr ) {
$closeBracket = $this->tokens[ $openBracket ]['parenthesis_closer'];

$startNext = $openBracket + 1;
$next = $this->phpcsFile->findNext( Tokens::$functionNameTokens, $startNext, $closeBracket, false, null, true );
$next = $this->phpcsFile->findNext( T_STRING, $startNext, $closeBracket, false, null, true );
while ( $next ) {
if ( in_array( $this->tokens[ $next ]['content'], $this->catch[ $functionName ], true ) === true ) {
$message = "`%s`'s return type must be checked before calling `%s` using that value.";
$data = [ $this->tokens[ $next ]['content'], $functionName ];
$this->phpcsFile->addError( $message, $next, 'DirectFunctionCall', $data );
}
$next = $this->phpcsFile->findNext( Tokens::$functionNameTokens, $next + 1, $closeBracket, false, null, true );
$next = $this->phpcsFile->findNext( T_STRING, $next + 1, $closeBracket, false, null, true );
}
}

Expand Down Expand Up @@ -269,7 +269,7 @@ public function findNonCheckedVariables( $stackPtr ) {
foreach ( $callees as $callee ) {
$notFunctionsCallee = array_key_exists( $callee, $this->notFunctions ) ? (array) $this->notFunctions[ $callee ] : [];
// Check whether the found token is one of the function calls (or foreach call) we are interested in.
if ( in_array( $this->tokens[ $nextFunctionCallWithVariable ]['code'], array_merge( Tokens::$functionNameTokens, $notFunctionsCallee ), true ) === true
if ( in_array( $this->tokens[ $nextFunctionCallWithVariable ]['code'], array_merge( [ T_STRING ], $notFunctionsCallee ), true ) === true
&& $this->tokens[ $nextFunctionCallWithVariable ]['content'] === $callee
) {
$this->addNonCheckedVariableError( $nextFunctionCallWithVariable, $variableName, $callee );
Expand All @@ -278,7 +278,7 @@ public function findNonCheckedVariables( $stackPtr ) {

$search = array_merge( Tokens::$emptyTokens, [ T_EQUAL ] );
$next = $this->phpcsFile->findNext( $search, $nextVariableOccurrence + 1, null, true );
if ( in_array( $this->tokens[ $next ]['code'], Tokens::$functionNameTokens, true ) === true
if ( $this->tokens[ $next ]['code'] === T_STRING
&& $this->tokens[ $next ]['content'] === $callee
) {
$this->addNonCheckedVariableError( $next, $variableName, $callee );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AlwaysReturnInFilterSniff extends Sniff {
* @return array(int)
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ private function processString( $stackPtr, $start = 0, $end = null ) {
$callbackFunctionName = substr( $this->tokens[ $stackPtr ]['content'], 1, -1 );

$callbackFunctionPtr = $this->phpcsFile->findNext(
Tokens::$functionNameTokens,
T_STRING,
$start,
$end,
false,
Expand Down
6 changes: 3 additions & 3 deletions WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PreGetPostsSniff extends Sniff {
* @return array(int)
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down Expand Up @@ -120,7 +120,7 @@ private function processString( $stackPtr ) {
$callbackFunctionName = substr( $this->tokens[ $stackPtr ]['content'], 1, -1 );

$callbackFunctionPtr = $this->phpcsFile->findNext(
Tokens::$functionNameTokens,
T_STRING,
0,
null,
false,
Expand Down Expand Up @@ -394,7 +394,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) {
true
);

return $next && in_array( $this->tokens[ $next ]['code'], Tokens::$functionNameTokens, true ) === true && $method === $this->tokens[ $next ]['content'];
return $next && $this->tokens[ $next ]['code'] === T_STRING && $method === $this->tokens[ $next ]['content'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CacheValueOverrideSniff extends Sniff {
* @return array(int)
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}


Expand Down Expand Up @@ -97,10 +97,6 @@ public function process_token( $stackPtr ) {
*/
private function isFunctionCall( $stackPtr ) {

if ( in_array( $this->tokens[ $stackPtr ]['code'], Tokens::$functionNameTokens, true ) === false ) {
return false;
}

// Find the next non-empty token.
$openBracket = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FetchingRemoteDataSniff extends Sniff {
* @return array
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TaxonomyMetaInOptionsSniff extends Sniff {
* @return array
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ExitAfterRedirectSniff extends Sniff {
* @return array
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProperEscapingFunctionSniff extends Sniff {
* @return array
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StaticStrreplaceSniff extends Sniff {
* @return array
*/
public function register() {
return Tokens::$functionNameTokens;
return [ T_STRING ];
}

/**
Expand Down