Skip to content

Commit

Permalink
Merge pull request #2269 from WordPress/feature/helpers-add-extra-def…
Browse files Browse the repository at this point in the history
…ensive-coding

Helpers: add some more defensive coding
  • Loading branch information
dingo-d authored Jul 4, 2023
2 parents ca8a41a + 2e0c47a commit b42708d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
16 changes: 14 additions & 2 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,7 +155,11 @@ public static function has_object_operator_before( File $phpcsFile, $stackPtr )
*/
public static function is_token_namespaced( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

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

if ( \T_NS_SEPARATOR !== $tokens[ $prev ]['code'] ) {
return false;
Expand Down Expand Up @@ -331,7 +339,11 @@ public static function get_safe_cast_tokens() {
*/
public static function is_safe_casted( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
if ( isset( $tokens[ $stackPtr ] ) === false ) {
return false;
}

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

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 b42708d

Please sign in to comment.