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

Add process() method to WordPress_Sniff … #833

Merged
merged 1 commit into from
Feb 10, 2017
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
7 changes: 3 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ Example usage:
```php
class WordPress_Sniffs_CSRF_NonceVerificationSniff extends WordPress_Sniff {

public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );

public function process_token( $stackPtr ) {

// Check something.

if ( $this->has_whitelist_comment( 'CSRF', $stackPtr ) ) {
return;
}

$phpcsFile->addError( ... );
$this->phpcsFile->addError( ... );
}
}
```
Expand Down
23 changes: 9 additions & 14 deletions WordPress/AbstractArrayAssignmentRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,16 @@ abstract public function getGroups();
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
public function process_token( $stackPtr ) {

$groups = $this->getGroups();

if ( empty( $groups ) ) {
$phpcsFile->removeTokenListener( $this, $this->register() );
$this->phpcsFile->removeTokenListener( $this, $this->register() );
return;
}

Expand All @@ -106,13 +104,10 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
return;
}

// Make phpcsFile and tokens available as properties.
$this->init( $phpcsFile );

$token = $this->tokens[ $stackPtr ];

if ( in_array( $token['code'], array( T_CLOSE_SQUARE_BRACKET ), true ) ) {
$equal = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true );
$equal = $this->phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true );
if ( T_EQUAL !== $this->tokens[ $equal ]['code'] ) {
return; // This is not an assignment!
}
Expand All @@ -129,15 +124,15 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
if ( in_array( $token['code'], array( T_CLOSE_SQUARE_BRACKET, T_DOUBLE_ARROW ), true ) ) {
$operator = $stackPtr; // T_DOUBLE_ARROW.
if ( T_CLOSE_SQUARE_BRACKET === $token['code'] ) {
$operator = $phpcsFile->findNext( array( T_EQUAL ), ( $stackPtr + 1 ) );
$operator = $this->phpcsFile->findNext( array( T_EQUAL ), ( $stackPtr + 1 ) );
}

$keyIdx = $phpcsFile->findPrevious( array( T_WHITESPACE, T_CLOSE_SQUARE_BRACKET ), ( $operator - 1 ), null, true );
$keyIdx = $this->phpcsFile->findPrevious( array( T_WHITESPACE, T_CLOSE_SQUARE_BRACKET ), ( $operator - 1 ), null, true );
if ( ! is_numeric( $this->tokens[ $keyIdx ]['content'] ) ) {
$key = $this->strip_quotes( $this->tokens[ $keyIdx ]['content'] );
$valStart = $phpcsFile->findNext( array( T_WHITESPACE ), ( $operator + 1 ), null, true );
$valEnd = $phpcsFile->findNext( array( T_COMMA, T_SEMICOLON ), ( $valStart + 1 ), null, false, null, true );
$val = $phpcsFile->getTokensAsString( $valStart, ( $valEnd - $valStart ) );
$valStart = $this->phpcsFile->findNext( array( T_WHITESPACE ), ( $operator + 1 ), null, true );
$valEnd = $this->phpcsFile->findNext( array( T_COMMA, T_SEMICOLON ), ( $valStart + 1 ), null, false, null, true );
$val = $this->phpcsFile->getTokensAsString( $valStart, ( $valEnd - $valStart ) );
$val = $this->strip_quotes( $val );
$inst[ $key ][] = array( $val, $token['line'] );
}
Expand Down
8 changes: 3 additions & 5 deletions WordPress/AbstractClassRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,16 @@ public function register() {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
public function process_token( $stackPtr ) {
// Reset the temporary storage before processing the token.
unset( $this->classname );

return parent::process( $phpcsFile, $stackPtr );
return parent::process_token( $stackPtr );
}

/**
Expand Down
9 changes: 2 additions & 7 deletions WordPress/AbstractFunctionRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,12 @@ protected function setup_groups( $key ) {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
public function process_token( $stackPtr ) {

$this->excluded_groups = array_flip( explode( ',', $this->exclude ) );
if ( array_diff_key( $this->groups, $this->excluded_groups ) === array() ) {
Expand All @@ -166,9 +164,6 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
return;
}

// Make phpcsFile and tokens available as properties.
$this->init( $phpcsFile );

if ( true === $this->is_targetted_token( $stackPtr ) ) {
return $this->check_for_matches( $stackPtr );
}
Expand Down
23 changes: 9 additions & 14 deletions WordPress/AbstractVariableRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,18 @@ abstract public function getGroups();
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {

// Make phpcsFile and tokens available as properties.
$this->init( $phpcsFile );
public function process_token( $stackPtr ) {

$token = $this->tokens[ $stackPtr ];
$groups = $this->getGroups();

if ( empty( $groups ) ) {
$phpcsFile->removeTokenListener( $this, $this->register() );
$this->phpcsFile->removeTokenListener( $this, $this->register() );
return;
}

Expand All @@ -115,8 +110,8 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {

// Check if it is a function not a variable.
if ( in_array( $token['code'], array( T_OBJECT_OPERATOR, T_DOUBLE_COLON ), true ) ) { // This only works for object vars and array members.
$method = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true );
$possible_parenthesis = $phpcsFile->findNext( T_WHITESPACE, ( $method + 1 ), null, true );
$method = $this->phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true );
$possible_parenthesis = $this->phpcsFile->findNext( T_WHITESPACE, ( $method + 1 ), null, true );
if ( T_OPEN_PARENTHESIS === $this->tokens[ $possible_parenthesis ]['code'] ) {
return; // So .. it is a function after all !
}
Expand All @@ -139,16 +134,16 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
// Object var, ex: $foo->bar / $foo::bar / Foo::bar / Foo::$bar .
$patterns = array_merge( $patterns, $group['object_vars'] );

$owner = $phpcsFile->findPrevious( array( T_VARIABLE, T_STRING ), $stackPtr );
$child = $phpcsFile->findNext( array( T_STRING, T_VAR, T_VARIABLE ), $stackPtr );
$owner = $this->phpcsFile->findPrevious( array( T_VARIABLE, T_STRING ), $stackPtr );
$child = $this->phpcsFile->findNext( array( T_STRING, T_VAR, T_VARIABLE ), $stackPtr );
$var = implode( '', array( $this->tokens[ $owner ]['content'], $token['content'], $this->tokens[ $child ]['content'] ) );

} elseif ( in_array( $token['code'], array( T_OPEN_SQUARE_BRACKET, T_DOUBLE_QUOTED_STRING ), true ) && ! empty( $group['array_members'] ) ) {
// Array members.
$patterns = array_merge( $patterns, $group['array_members'] );

$owner = $phpcsFile->findPrevious( array( T_VARIABLE ), $stackPtr );
$inside = $phpcsFile->getTokensAsString( $stackPtr, ( $token['bracket_closer'] - $stackPtr + 1 ) );
$owner = $this->phpcsFile->findPrevious( array( T_VARIABLE ), $stackPtr );
$inside = $this->phpcsFile->getTokensAsString( $stackPtr, ( $token['bracket_closer'] - $stackPtr + 1 ) );
$var = implode( '', array( $this->tokens[ $owner ]['content'], $inside ) );
} else {
continue;
Expand Down
29 changes: 29 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,35 @@ abstract class WordPress_Sniff implements PHP_CodeSniffer_Sniff {
'$_SERVER',
);

/**
* Set sniff properties and hand off to child class for processing of the token.
*
* @since 0.11.0
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );
return $this->process_token( $stackPtr );
}

/**
* Processes a sniff when one of its tokens is encountered.
*
* @since 0.11.0
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
abstract public function process_token( $stackPtr );

/**
* Initialize the class for the current process.
*
Expand Down
8 changes: 2 additions & 6 deletions WordPress/Sniffs/CSRF/NonceVerificationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ public function register() {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
public function process_token( $stackPtr ) {

// Merge any custom functions with the defaults, if we haven't already.
if ( ! self::$addedCustomFunctions ) {
Expand All @@ -89,8 +87,6 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
self::$addedCustomFunctions = true;
}

$this->init( $phpcsFile );

$instance = $this->tokens[ $stackPtr ];

$superglobals = array_merge(
Expand Down
9 changes: 3 additions & 6 deletions WordPress/Sniffs/PHP/StrictComparisonsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ public function register() {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );
public function process_token( $stackPtr ) {

if ( ! $this->has_whitelist_comment( 'loose comparison', $stackPtr ) ) {
$error = 'Found: ' . $this->tokens[ $stackPtr ]['content'] . '. Use strict comparisons (=== or !==).';
$phpcsFile->addWarning( $error, $stackPtr, 'LooseComparison' );
$this->phpcsFile->addWarning( $error, $stackPtr, 'LooseComparison' );
}

}
Expand Down
18 changes: 8 additions & 10 deletions WordPress/Sniffs/VIP/AdminBarRemovalSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,19 @@ public function register() {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );
public function process_token( $stackPtr ) {

$file_name = $phpcsFile->getFileName();
$file_name = $this->phpcsFile->getFileName();
$file_extension = substr( strrchr( $file_name, '.' ), 1 );

if ( 'css' === $file_extension ) {
if ( T_STYLE === $this->tokens[ $stackPtr ]['code'] ) {
$this->process_css_style( $stackPtr );
return $this->process_css_style( $stackPtr );
}
} elseif ( isset( $this->string_tokens[ $this->tokens[ $stackPtr ]['code'] ] ) ) {
/*
Expand All @@ -189,10 +187,10 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->in_target_selector[ $file_name ] = false;
}

$this->process_text_for_style( $stackPtr, $file_name );
return $this->process_text_for_style( $stackPtr, $file_name );

} else {
parent::process( $phpcsFile, $stackPtr );
return parent::process_token( $stackPtr );
}

} // End process().
Expand Down
30 changes: 13 additions & 17 deletions WordPress/Sniffs/VIP/CronIntervalSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,19 @@ public function register() {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );

$token = $this->tokens[ $stackPtr ];
public function process_token( $stackPtr ) {
$token = $this->tokens[ $stackPtr ];

if ( 'cron_schedules' !== $this->strip_quotes( $token['content'] ) ) {
return;
}

// If within add_filter.
$functionPtr = $phpcsFile->findPrevious( T_STRING, key( $token['nested_parenthesis'] ) );
$functionPtr = $this->phpcsFile->findPrevious( T_STRING, key( $token['nested_parenthesis'] ) );
if ( 'add_filter' !== $this->tokens[ $functionPtr ]['content'] ) {
return;
}
Expand All @@ -77,7 +73,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
}

// Detect callback function name.
$callbackArrayPtr = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, $callback['start'], ( $callback['end'] + 1 ), true );
$callbackArrayPtr = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, $callback['start'], ( $callback['end'] + 1 ), true );

// If callback is array, get second element.
if ( false !== $callbackArrayPtr
Expand All @@ -95,7 +91,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
unset( $functionPtr );

// Search for the function in tokens.
$callbackFunctionPtr = $phpcsFile->findNext( array( T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING, T_CLOSURE ), $callback['start'], ( $callback['end'] + 1 ) );
$callbackFunctionPtr = $this->phpcsFile->findNext( array( T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING, T_CLOSURE ), $callback['start'], ( $callback['end'] + 1 ) );

if ( false === $callbackFunctionPtr ) {
$this->confused( $stackPtr );
Expand All @@ -107,9 +103,9 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
} else {
$functionName = $this->strip_quotes( $this->tokens[ $callbackFunctionPtr ]['content'] );

for ( $ptr = 0; $ptr < $phpcsFile->numTokens; $ptr++ ) {
for ( $ptr = 0; $ptr < $this->phpcsFile->numTokens; $ptr++ ) {
if ( T_FUNCTION === $this->tokens[ $ptr ]['code'] ) {
$foundName = $phpcsFile->getDeclarationName( $ptr );
$foundName = $this->phpcsFile->getDeclarationName( $ptr );
if ( $foundName === $functionName ) {
$functionPtr = $ptr;
break;
Expand All @@ -136,15 +132,15 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {

if ( in_array( $this->tokens[ $i ]['code'], array( T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING ), true ) ) {
if ( 'interval' === $this->strip_quotes( $this->tokens[ $i ]['content'] ) ) {
$operator = $phpcsFile->findNext( T_DOUBLE_ARROW, $i, null, false, null, true );
$operator = $this->phpcsFile->findNext( T_DOUBLE_ARROW, $i, null, false, null, true );
if ( false === $operator ) {
$this->confused( $stackPtr );
return;
}

$valueStart = $phpcsFile->findNext( T_WHITESPACE, ( $operator + 1 ), null, true, null, true );
$valueEnd = $phpcsFile->findNext( array( T_COMMA, T_CLOSE_PARENTHESIS ), ( $valueStart + 1 ) );
$value = $phpcsFile->getTokensAsString( $valueStart, ( $valueEnd - $valueStart ) );
$valueStart = $this->phpcsFile->findNext( T_WHITESPACE, ( $operator + 1 ), null, true, null, true );
$valueEnd = $this->phpcsFile->findNext( array( T_COMMA, T_CLOSE_PARENTHESIS ), ( $valueStart + 1 ) );
$value = $this->phpcsFile->getTokensAsString( $valueStart, ( $valueEnd - $valueStart ) );

if ( is_numeric( $value ) ) {
$interval = $value;
Expand All @@ -167,7 +163,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
}

if ( isset( $interval ) && $interval < 900 ) {
$phpcsFile->addError( 'Scheduling crons at %s sec ( less than 15 min ) is prohibited.', $stackPtr, 'CronSchedulesInterval', array( $interval ) );
$this->phpcsFile->addError( 'Scheduling crons at %s sec ( less than 15 min ) is prohibited.', $stackPtr, 'CronSchedulesInterval', array( $interval ) );
return;
}

Expand Down
Loading