Skip to content

Commit

Permalink
ContextHelper::$safe_casts: make private
Browse files Browse the repository at this point in the history
Follow up on 2232.

This property was previously not made `private` as it is used by the `EscapeOutput` sniff. For consistency with other classes and to better protect the value of the property, I'm proposing to make it `private` now anyway and add a `get_safe_cast_tokens()` method to retrieve the list.

This prevents potential problems if external standards would attempt to adjust the list (which they could while the property was `public static`, even though the class is `final`).
  • Loading branch information
jrfnl committed Jun 29, 2023
1 parent 7ab5947 commit 3900d24
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 3900d24

Please sign in to comment.