Skip to content

Commit

Permalink
Helpers: add some more defensive coding
Browse files Browse the repository at this point in the history
The `Helper` classes containing stand-alone `static` methods can be used by both WPCS itself as well as external standards extending WPCS.

As these methods have been made stand-alone, they should probably contain a little more defensive coding in select places.

Note: this code will not be "covered" by tests at this moment as the WPCS code as-is will never hit these conditions.

At a later point in time, we may want to add dedicated tests for the Helper methods which aren't moving to PHPCSUtils. When we do, we can make sure this defensive code gets covered too.
  • Loading branch information
jrfnl committed Jun 28, 2023
1 parent 7ab5947 commit cee7da5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
12 changes: 12 additions & 0 deletions WordPress/Helpers/ContextHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ final class ContextHelper {
*/
public static function has_object_operator_before( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

$before = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );

return isset( Collections::objectOperators()[ $tokens[ $before ]['code'] ] );
Expand All @@ -151,6 +155,10 @@ public static function has_object_operator_before( File $phpcsFile, $stackPtr )
*/
public static function is_token_namespaced( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

$prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );

Check warning on line 162 in WordPress/Helpers/ContextHelper.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Equals sign not aligned correctly; expected 1 space but found 3 spaces

Check warning on line 162 in WordPress/Helpers/ContextHelper.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Equals sign not aligned correctly; expected 1 space but found 3 spaces

if ( \T_NS_SEPARATOR !== $tokens[ $prev ]['code'] ) {
Expand Down Expand Up @@ -320,6 +328,10 @@ public static function is_in_isset_or_empty( File $phpcsFile, $stackPtr ) {
*/
public static function is_safe_casted( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

$prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );

Check warning on line 335 in WordPress/Helpers/ContextHelper.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Equals sign not aligned correctly; expected 1 space but found 3 spaces

Check warning on line 335 in WordPress/Helpers/ContextHelper.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Equals sign not aligned correctly; expected 1 space but found 3 spaces

return isset( self::$safe_casts[ $tokens[ $prev ]['code'] ] );
Expand Down
6 changes: 5 additions & 1 deletion WordPress/Helpers/DeprecationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ final class DeprecationHelper {
* @return bool
*/
public static function is_function_deprecated( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

$ignore = Tokens::$methodPrefixes;
$ignore[ \T_WHITESPACE ] = \T_WHITESPACE;

Expand Down
2 changes: 1 addition & 1 deletion WordPress/Helpers/ListHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function get_list_variables( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

// Is this one of the tokens this function handles ?
if ( isset( Collections::listOpenTokensBC()[ $tokens[ $stackPtr ]['code'] ] ) === false ) {
if ( isset( $tokens[ $stackPtr ], Collections::listOpenTokensBC()[ $tokens[ $stackPtr ]['code'] ] ) === false ) {
return array();
}

Expand Down
3 changes: 3 additions & 0 deletions WordPress/Helpers/ValidationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ final class ValidationHelper {
*/
public static function is_validated( File $phpcsFile, $stackPtr, $array_keys = array(), $in_condition_only = false ) {
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

if ( $in_condition_only ) {
/*
Expand Down
13 changes: 11 additions & 2 deletions WordPress/Helpers/VariableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public static function get_array_access_keys( File $phpcsFile, $stackPtr, $all =
$tokens = $phpcsFile->getTokens();
$keys = array();

if ( \T_VARIABLE !== $tokens[ $stackPtr ]['code'] ) {
if ( isset( $tokens[ $stackPtr ] ) === false
|| \T_VARIABLE !== $tokens[ $stackPtr ]['code']
) {
return $keys;
}

Expand Down Expand Up @@ -140,7 +142,11 @@ public static function get_array_access_key( File $phpcsFile, $stackPtr ) {
* @return bool Whether this is a comparison.
*/
public static function is_comparison( File $phpcsFile, $stackPtr, $include_coalesce = true ) {
$tokens = $phpcsFile->getTokens();
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

$comparisonTokens = Tokens::$comparisonTokens;
if ( false === $include_coalesce ) {
unset( $comparisonTokens[ \T_COALESCE ] );
Expand Down Expand Up @@ -203,6 +209,9 @@ public static function is_comparison( File $phpcsFile, $stackPtr, $include_coale
*/
public static function is_assignment( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

static $valid = array(
\T_VARIABLE => true,
Expand Down
4 changes: 3 additions & 1 deletion WordPress/Helpers/WPDBTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ trait WPDBTrait {
* @return bool Whether this is a $wpdb method call.
*/
protected function is_wpdb_method_call( File $phpcsFile, $stackPtr, $target_methods ) {

$tokens = $phpcsFile->getTokens();
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

// Check for wpdb.
if ( ( \T_VARIABLE === $tokens[ $stackPtr ]['code'] && '$wpdb' !== $tokens[ $stackPtr ]['content'] )
Expand Down

0 comments on commit cee7da5

Please sign in to comment.