Skip to content

Commit

Permalink
Merge pull request #1469 from WordPress-Coding-Standards/feature/is_s…
Browse files Browse the repository at this point in the history
…afe_casted-minor-efficiency-fix

Sniff::is_safe_casted(): minor efficiency fixes
  • Loading branch information
JDGrimes authored Aug 28, 2018
2 parents 4513849 + 05a1b59 commit 206b30c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,19 @@ abstract class Sniff implements PHPCS_Sniff {
'sanitize_key' => true,
);

/**
* Token which when they preceed code indicate the value is safely casted.
*
* @since 1.1.0
*
* @var array
*/
protected $safe_casts = array(
\T_INT_CAST => true,
\T_DOUBLE_CAST => true,
\T_BOOL_CAST => true,
);

/**
* Functions that format strings.
*
Expand Down Expand Up @@ -1560,8 +1573,12 @@ protected function is_safe_casted( $stackPtr ) {
true
);

if ( false === $prev ) {
return false;
}

// Check if it is a safe cast.
return \in_array( $this->tokens[ $prev ]['code'], array( \T_INT_CAST, \T_DOUBLE_CAST, \T_BOOL_CAST ), true );
return isset( $this->safe_casts[ $this->tokens[ $prev ]['code'] ] );
}

/**
Expand Down

0 comments on commit 206b30c

Please sign in to comment.