Skip to content

Commit

Permalink
Merge pull request #2271 from WordPress/feature/contexthelper-make-sa…
Browse files Browse the repository at this point in the history
…fe-casts-prop-private

ContextHelper::$safe_casts: make `private`
  • Loading branch information
dingo-d authored Jul 4, 2023
2 parents 94004df + 3900d24 commit 3b320f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
15 changes: 13 additions & 2 deletions WordPress/Helpers/ContextHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ final class ContextHelper {
*
* @since 1.1.0
* @since 3.0.0 - Moved from the Sniff class to this class.
* - The property visibility was changed from `protected` to `public static`.
* - The property visibility was changed from `protected` to `private static`.
*
* @var array
*/
public static $safe_casts = array(
private static $safe_casts = array(
\T_INT_CAST => true,
\T_DOUBLE_CAST => true,
\T_BOOL_CAST => true,
Expand Down Expand Up @@ -306,6 +306,17 @@ public static function is_in_isset_or_empty( File $phpcsFile, $stackPtr ) {
return false;
}

/**
* Retrieve a list of the tokens which are regarded as "safe casts".
*
* @since 3.0.0
*
* @return array<string, bool>
*/
public static function get_safe_cast_tokens() {
return self::$safe_casts;
}

/**
* Check if something is being casted to a safe value.
*
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/Security/EscapeOutputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public function process_token( $stackPtr ) {
$watch = false;

// Allow int/double/bool casted variables.
if ( isset( ContextHelper::$safe_casts[ $this->tokens[ $i ]['code'] ] ) ) {
if ( isset( ContextHelper::get_safe_cast_tokens()[ $this->tokens[ $i ]['code'] ] ) ) {
$in_cast = true;
continue;
}
Expand Down
1 change: 1 addition & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @since 1.0.0 This sniff has been moved from the `XSS` category to the `Security` category.
*
* @covers \WordPressCS\WordPress\Helpers\ArrayWalkingFunctionsHelper
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::get_safe_cast_tokens
* @covers \WordPressCS\WordPress\Helpers\ConstantsHelper::is_use_of_global_constant
* @covers \WordPressCS\WordPress\Helpers\EscapingFunctionsTrait
* @covers \WordPressCS\WordPress\Helpers\PrintingFunctionsTrait
Expand Down

0 comments on commit 3b320f6

Please sign in to comment.