Skip to content

Commit

Permalink
Verify the extended classes
Browse files Browse the repository at this point in the history
Verified that classes which extend a parent actually *use* that parent and if not, removed the `extend`.
If they use the parent, leverage the properties available in the parent and remove duplicate function calls.
  • Loading branch information
jrfnl committed Jul 29, 2016
1 parent de73645 commit 8760c44
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 138 deletions.
9 changes: 8 additions & 1 deletion WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,14 @@ abstract class WordPress_Sniff implements PHP_CodeSniffer_Sniff {
*
* @var string[]
*/
protected static $input_superglobals = array( '$_COOKIE', '$_GET', '$_FILES', '$_POST', '$_REQUEST', '$_SERVER' );
protected static $input_superglobals = array(
'$_COOKIE',
'$_GET',
'$_FILES',
'$_POST',
'$_REQUEST',
'$_SERVER',
);

/**
* Initialize the class for the current process.
Expand Down
3 changes: 1 addition & 2 deletions WordPress/Sniffs/CSRF/NonceVerificationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {

$this->init( $phpcsFile );

$tokens = $phpcsFile->getTokens();
$instance = $tokens[ $stackPtr ];
$instance = $this->tokens[ $stackPtr ];

$superglobals = array_merge(
$this->errorForSuperGlobals
Expand Down
3 changes: 1 addition & 2 deletions WordPress/Sniffs/PHP/StrictComparisonsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );

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

Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/PHP/StrictInArraySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @category PHP
* @package PHP_CodeSniffer
*/
class WordPress_Sniffs_PHP_StrictInArraySniff extends WordPress_Sniff {
class WordPress_Sniffs_PHP_StrictInArraySniff implements PHP_CodeSniffer_Sniff {

/**
* List of array functions to which a $strict parameter can be passed.
Expand Down
6 changes: 1 addition & 5 deletions WordPress/Sniffs/VIP/SessionVariableUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/
*/

if ( ! class_exists( 'Generic_Sniffs_PHP_ForbiddenFunctionsSniff', true ) ) {
throw new PHP_CodeSniffer_Exception( 'Class Generic_Sniffs_PHP_ForbiddenFunctionsSniff not found' );
}

/**
* WordPress_Sniffs_VIP_SessionVariableUsageSniff
*
Expand All @@ -23,7 +19,7 @@
* @package PHP_CodeSniffer
* @author Shady Sharaf <shady@x-team.com>
*/
class WordPress_Sniffs_VIP_SessionVariableUsageSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff {
class WordPress_Sniffs_VIP_SessionVariableUsageSniff implements PHP_CodeSniffer_Sniff {

/**
* Returns an array of tokens this test wants to listen for.
Expand Down
5 changes: 2 additions & 3 deletions WordPress/Sniffs/VIP/SuperGlobalInputUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ public function register() {
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );
$tokens = $phpcsFile->getTokens();

// Check for global input variable.
if ( ! in_array( $tokens[ $stackPtr ]['content'], WordPress_Sniff::$input_superglobals, true ) ) {
if ( ! in_array( $this->tokens[ $stackPtr ]['content'], self::$input_superglobals, true ) ) {
return;
}

$varName = $tokens[ $stackPtr ]['content'];
$varName = $this->tokens[ $stackPtr ]['content'];

// If we're overriding a superglobal with an assignment, no need to test.
if ( $this->is_assignment( $stackPtr ) ) {
Expand Down
21 changes: 10 additions & 11 deletions WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,37 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
// Merge any custom functions with the defaults, if we haven't already.
if ( ! self::$addedCustomFunctions ) {

WordPress_Sniff::$sanitizingFunctions = array_merge(
WordPress_Sniff::$sanitizingFunctions,
self::$sanitizingFunctions = array_merge(
self::$sanitizingFunctions,
array_flip( $this->customSanitizingFunctions )
);

WordPress_Sniff::$unslashingSanitizingFunctions = array_merge(
WordPress_Sniff::$unslashingSanitizingFunctions,
self::$unslashingSanitizingFunctions = array_merge(
self::$unslashingSanitizingFunctions,
array_flip( $this->customUnslashingSanitizingFunctions )
);

self::$addedCustomFunctions = true;
}

$this->init( $phpcsFile );
$tokens = $phpcsFile->getTokens();
$superglobals = WordPress_Sniff::$input_superglobals;
$superglobals = self::$input_superglobals;

// Handling string interpolation.
if ( T_DOUBLE_QUOTED_STRING === $tokens[ $stackPtr ]['code'] ) {
if ( T_DOUBLE_QUOTED_STRING === $this->tokens[ $stackPtr ]['code'] ) {
$interpolated_variables = array_map(
create_function( '$symbol', 'return "$" . $symbol;' ), // Replace with closure when 5.3 is minimum requirement for PHPCS.
$this->get_interpolated_variables( $tokens[ $stackPtr ]['content'] )
$this->get_interpolated_variables( $this->tokens[ $stackPtr ]['content'] )
);
foreach ( array_intersect( $interpolated_variables, $superglobals ) as $bad_variable ) {
$phpcsFile->addError( 'Detected usage of a non-sanitized, non-validated input variable %s: %s', $stackPtr, 'InputNotValidatedNotSanitized', array( $bad_variable, $tokens[ $stackPtr ]['content'] ) );
$phpcsFile->addError( 'Detected usage of a non-sanitized, non-validated input variable %s: %s', $stackPtr, 'InputNotValidatedNotSanitized', array( $bad_variable, $this->tokens[ $stackPtr ]['content'] ) );
}

return;
}

// Check if this is a superglobal.
if ( ! in_array( $tokens[ $stackPtr ]['content'], $superglobals, true ) ) {
if ( ! in_array( $this->tokens[ $stackPtr ]['content'], $superglobals, true ) ) {
return;
}

Expand All @@ -129,7 +128,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
return;
}

$error_data = array( $tokens[ $stackPtr ]['content'] );
$error_data = array( $this->tokens[ $stackPtr ]['content'] );

// Check for validation first.
if ( ! $this->is_validated( $stackPtr, $array_key, $this->check_validation_in_scope_only ) ) {
Expand Down
19 changes: 9 additions & 10 deletions WordPress/Sniffs/Variables/GlobalVariablesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,28 +282,27 @@ public function register() {
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->init( $phpcsFile );
$tokens = $phpcsFile->getTokens();
$token = $tokens[ $stackPtr ];
$token = $this->tokens[ $stackPtr ];

$search = array(); // Array of globals to watch for.

if ( T_VARIABLE === $token['code'] && '$GLOBALS' === $token['content'] ) {
$bracketPtr = $phpcsFile->findNext( array( T_WHITESPACE ), ( $stackPtr + 1 ), null, true );

if ( T_OPEN_SQUARE_BRACKET !== $tokens[ $bracketPtr ]['code'] ) {
if ( T_OPEN_SQUARE_BRACKET !== $this->tokens[ $bracketPtr ]['code'] ) {
return;
}

$varPtr = $phpcsFile->findNext( T_WHITESPACE, ( $bracketPtr + 1 ), $tokens[ $bracketPtr ]['bracket_closer'], true );
$varToken = $tokens[ $varPtr ];
$varPtr = $phpcsFile->findNext( T_WHITESPACE, ( $bracketPtr + 1 ), $this->tokens[ $bracketPtr ]['bracket_closer'], true );
$varToken = $this->tokens[ $varPtr ];

if ( ! in_array( trim( $varToken['content'], '\'"' ), $this->globals, true ) ) {
return;
}

$assignment = $phpcsFile->findNext( T_WHITESPACE, ( $tokens[ $bracketPtr ]['bracket_closer'] + 1 ), null, true );
$assignment = $phpcsFile->findNext( T_WHITESPACE, ( $this->tokens[ $bracketPtr ]['bracket_closer'] + 1 ), null, true );

if ( $assignment && T_EQUAL === $tokens[ $assignment ]['code'] ) {
if ( $assignment && T_EQUAL === $this->tokens[ $assignment ]['code'] ) {
if ( ! $this->has_whitelist_comment( 'override', $assignment ) ) {
$phpcsFile->addError( 'Overriding WordPress globals is prohibited', $stackPtr, 'OverrideProhibited' );
return;
Expand All @@ -316,7 +315,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$ptr = ( $stackPtr + 1 );
while ( $ptr ) {
$ptr++;
$var = $tokens[ $ptr ];
$var = $this->tokens[ $ptr ];
if ( T_VARIABLE === $var['code'] ) {
$varname = substr( $var['content'], 1 );
if ( in_array( $varname, $this->globals, true ) ) {
Expand All @@ -333,10 +332,10 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
}

// Check for assignments to collected global vars.
foreach ( $tokens as $ptr => $token ) {
foreach ( $this->tokens as $ptr => $token ) {
if ( T_VARIABLE === $token['code'] && in_array( substr( $token['content'], 1 ), $search, true ) ) {
$next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $ptr + 1 ), null, true, null, true );
if ( T_EQUAL === $tokens[ $next ]['code'] ) {
if ( T_EQUAL === $this->tokens[ $next ]['code'] ) {
if ( ! $this->has_whitelist_comment( 'override', $next ) ) {
$phpcsFile->addError( 'Overriding WordPress globals is prohibited', $ptr, 'OverrideProhibited' );
}
Expand Down
28 changes: 13 additions & 15 deletions WordPress/Sniffs/WP/PreparedSQLSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,13 @@ public function register() {
*/
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {

$tokens = $phpcsFile->getTokens();
$this->init( $phpcsFile );

// Check for $wpdb variable.
if ( '$wpdb' !== $tokens[ $stackPtr ]['content'] ) {
if ( '$wpdb' !== $this->tokens[ $stackPtr ]['content'] ) {
return;
}

$this->init( $phpcsFile );

if ( ! $this->is_wpdb_method_call( $stackPtr ) ) {
return;
}
Expand All @@ -120,14 +118,14 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {

for ( $this->i; $this->i < $this->end; $this->i++ ) {

if ( isset( $this->ignored_tokens[ $tokens[ $this->i ]['code'] ] ) ) {
if ( isset( $this->ignored_tokens[ $this->tokens[ $this->i ]['code'] ] ) ) {
continue;
}

if ( T_DOUBLE_QUOTED_STRING === $tokens[ $this->i ]['code'] ) {
if ( T_DOUBLE_QUOTED_STRING === $this->tokens[ $this->i ]['code'] ) {

$bad_variables = array_filter(
$this->get_interpolated_variables( $tokens[ $this->i ]['content'] ),
$this->get_interpolated_variables( $this->tokens[ $this->i ]['content'] ),
create_function( '$symbol', 'return ! in_array( $symbol, array( "wpdb" ), true );' ) // Replace this with closure once 5.3 is minimum requirement.
);

Expand All @@ -138,25 +136,25 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
'NotPrepared',
array(
$bad_variable,
$tokens[ $this->i ]['content'],
$this->tokens[ $this->i ]['content'],
)
);
}
continue;
}

if ( T_VARIABLE === $tokens[ $this->i ]['code'] ) {
if ( '$wpdb' === $tokens[ $this->i ]['content'] ) {
if ( T_VARIABLE === $this->tokens[ $this->i ]['code'] ) {
if ( '$wpdb' === $this->tokens[ $this->i ]['content'] ) {
$this->is_wpdb_method_call( $this->i );
continue;
}
}

if ( T_STRING === $tokens[ $this->i ]['code'] ) {
if ( T_STRING === $this->tokens[ $this->i ]['code'] ) {

if (
isset( self::$SQLEscapingFunctions[ $tokens[ $this->i ]['content'] ] )
|| isset( self::$SQLAutoEscapedFunctions[ $tokens[ $this->i ]['content'] ] )
isset( self::$SQLEscapingFunctions[ $this->tokens[ $this->i ]['content'] ] )
|| isset( self::$SQLAutoEscapedFunctions[ $this->tokens[ $this->i ]['content'] ] )
) {

// Find the opening parenthesis.
Expand All @@ -171,7 +169,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$this->i = $this->tokens[ $opening_paren ]['parenthesis_closer'];
continue;
}
} elseif ( isset( self::$formattingFunctions[ $tokens[ $this->i ]['content'] ] ) ) {
} elseif ( isset( self::$formattingFunctions[ $this->tokens[ $this->i ]['content'] ] ) ) {
continue;
}
}
Expand All @@ -180,7 +178,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
'Use placeholders and $wpdb->prepare(); found %s',
$this->i,
'NotPrepared',
array( $tokens[ $this->i ]['content'] )
array( $this->tokens[ $this->i ]['content'] )
);
}

Expand Down
Loading

0 comments on commit 8760c44

Please sign in to comment.