Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move "property handling" related utilities to dedicated RulesetPropertyHelper #2157

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion WordPress/AbstractArrayAssignmentRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPCSUtils\Utils\MessageHelper;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniff;

/**
Expand Down Expand Up @@ -134,7 +135,7 @@ protected function setup_groups() {
*/
public function process_token( $stackPtr ) {

$this->excluded_groups = $this->merge_custom_array( $this->exclude );
$this->excluded_groups = RulesetPropertyHelper::merge_custom_array( $this->exclude );
if ( array_diff_key( $this->groups_cache, $this->excluded_groups ) === array() ) {
// All groups have been excluded.
// Don't remove the listener as the exclude property can be changed inline.
Expand Down
3 changes: 2 additions & 1 deletion WordPress/AbstractClassRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPCSUtils\Utils\Namespaces;
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;

/**
* Restricts usage of some classes.
Expand Down Expand Up @@ -93,7 +94,7 @@ public function process_token( $stackPtr ) {
// Reset the temporary storage before processing the token.
unset( $this->classname );

$this->excluded_groups = $this->merge_custom_array( $this->exclude );
$this->excluded_groups = RulesetPropertyHelper::merge_custom_array( $this->exclude );
if ( array_diff_key( $this->groups, $this->excluded_groups ) === array() ) {
// All groups have been excluded.
// Don't remove the listener as the exclude property can be changed inline.
Expand Down
3 changes: 2 additions & 1 deletion WordPress/AbstractFunctionRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\MessageHelper;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniff;

/**
Expand Down Expand Up @@ -183,7 +184,7 @@ protected function setup_groups( $key ) {
*/
public function process_token( $stackPtr ) {

$this->excluded_groups = $this->merge_custom_array( $this->exclude );
$this->excluded_groups = RulesetPropertyHelper::merge_custom_array( $this->exclude );
if ( array_diff_key( $this->groups, $this->excluded_groups ) === array() ) {
// All groups have been excluded.
// Don't remove the listener as the exclude property can be changed inline.
Expand Down
4 changes: 2 additions & 2 deletions WordPress/Helpers/IsUnitTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\ObjectDeclarations;
use WordPressCS\WordPress\Sniff as WPCS_Sniff;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;

/**
* Helper utilities for sniffs which need to take into account whether the
Expand Down Expand Up @@ -127,7 +127,7 @@ protected function is_test_class( File $phpcsFile, $stackPtr ) {
}

// Add any potentially extra custom test classes to the known test classes list.
$known_test_classes = WPCS_Sniff::merge_custom_array(
$known_test_classes = RulesetPropertyHelper::merge_custom_array(
$this->custom_test_classes,
$this->known_test_classes
);
Expand Down
72 changes: 72 additions & 0 deletions WordPress/Helpers/RulesetPropertyHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/

namespace WordPressCS\WordPress\Helpers;

/**
* Helper utilities for working with user provided property values.
*
* ---------------------------------------------------------------------------------------------
* This class is only intended for internal use by WordPressCS and is not part of the public API.
* This also means that it has no promise of backward compatibility. Use at your own risk.
* ---------------------------------------------------------------------------------------------
*
* @package WPCS\WordPressCodingStandards
* @since 3.0.0 The method in this class was previously contained in the
* `WordPressCS\WordPress\Sniff` class and has been moved here.
*/
final class RulesetPropertyHelper {

/**
* Merge a pre-set array with a ruleset provided array.
*
* - By default flips custom lists to allow for using `isset()` instead
* of `in_array()`.
* - When `$flip` is true:
* * Presumes the base array is in a `'value' => true` format.
* * Any custom items will be given the value `false` to be able to
* distinguish them from pre-set (base array) values.
* * Will filter previously added custom items out from the base array
* before merging/returning to allow for resetting to the base array.
*
* {@internal Function is static as it doesn't use any of the properties or others
* methods anyway.}
*
* @since 0.11.0
* @since 2.0.0 No longer supports custom array properties which were incorrectly
* passed as a string.
* @since 3.0.0 Moved from the Sniff class to this class.
*
* @param array $custom Custom list as provided via a ruleset.
* @param array $base Optional. Base list. Defaults to an empty array.
* Expects `value => true` format when `$flip` is true.
* @param bool $flip Optional. Whether or not to flip the custom list.
* Defaults to true.
* @return array
*/
public static function merge_custom_array( $custom, $base = array(), $flip = true ) {
if ( true === $flip ) {
$base = array_filter( $base );
}

if ( empty( $custom ) || ! \is_array( $custom ) ) {
return $base;
}

if ( true === $flip ) {
$custom = array_fill_keys( $custom, false );
}

if ( empty( $base ) ) {
return $custom;
}

return array_merge( $base, $custom );
}
}
47 changes: 0 additions & 47 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,53 +763,6 @@ public static function get_snake_case_name_suggestion( $name ) {
return $suggested;
}

/**
* Merge a pre-set array with a ruleset provided array.
*
* - By default flips custom lists to allow for using `isset()` instead
* of `in_array()`.
* - When `$flip` is true:
* * Presumes the base array is in a `'value' => true` format.
* * Any custom items will be given the value `false` to be able to
* distinguish them from pre-set (base array) values.
* * Will filter previously added custom items out from the base array
* before merging/returning to allow for resetting to the base array.
*
* {@internal Function is static as it doesn't use any of the properties or others
* methods anyway and this way the `WordPress.NamingConventions.ValidVariableName` sniff
* which extends an upstream sniff can also use it.}}
*
* @since 0.11.0
* @since 2.0.0 No longer supports custom array properties which were incorrectly
* passed as a string.
*
* @param array $custom Custom list as provided via a ruleset.
* @param array $base Optional. Base list. Defaults to an empty array.
* Expects `value => true` format when `$flip` is true.
* @param bool $flip Optional. Whether or not to flip the custom list.
* Defaults to true.
* @return array
*/
public static function merge_custom_array( $custom, $base = array(), $flip = true ) {
if ( true === $flip ) {
$base = array_filter( $base );
}

if ( empty( $custom ) || ! \is_array( $custom ) ) {
return $base;
}

if ( true === $flip ) {
$custom = array_fill_keys( $custom, false );
}

if ( empty( $base ) ) {
return $custom;
}

return array_merge( $base, $custom );
}

/**
* Get the last pointer in a line.
*
Expand Down
9 changes: 5 additions & 4 deletions WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

namespace WordPressCS\WordPress\Sniffs\DB;

use WordPressCS\WordPress\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniff;

/**
* Flag Database direct queries.
Expand Down Expand Up @@ -275,7 +276,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customCacheGetFunctions !== $this->addedCustomFunctions['cacheget'] ) {
$this->cacheGetFunctions = $this->merge_custom_array(
$this->cacheGetFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customCacheGetFunctions,
$this->cacheGetFunctions
);
Expand All @@ -284,7 +285,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customCacheSetFunctions !== $this->addedCustomFunctions['cacheset'] ) {
$this->cacheSetFunctions = $this->merge_custom_array(
$this->cacheSetFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customCacheSetFunctions,
$this->cacheSetFunctions
);
Expand All @@ -293,7 +294,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customCacheDeleteFunctions !== $this->addedCustomFunctions['cachedelete'] ) {
$this->cacheDeleteFunctions = $this->merge_custom_array(
$this->cacheDeleteFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customCacheDeleteFunctions,
$this->cacheDeleteFunctions
);
Expand Down
3 changes: 2 additions & 1 deletion WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
use WordPressCS\WordPress\Helpers\DeprecationHelper;
use WordPressCS\WordPress\Helpers\IsUnitTestTrait;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Helpers\WPHookHelper;

/**
Expand Down Expand Up @@ -283,7 +284,7 @@ public function process_token( $stackPtr ) {
}
}

$this->prefixes = $this->merge_custom_array( $this->prefixes, array(), false );
$this->prefixes = RulesetPropertyHelper::merge_custom_array( $this->prefixes, array(), false );
if ( empty( $this->prefixes ) ) {
// No prefixes passed, nothing to do.
return;
Expand Down
5 changes: 3 additions & 2 deletions WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff as PHPCS_AbstractVariableSniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniff;

/**
Expand Down Expand Up @@ -288,9 +289,9 @@ public static function isSnakeCase( $var_name ) {
protected function merge_allow_lists() {
if ( $this->allowed_custom_properties !== $this->addedCustomProperties['properties'] ) {
// Fix property potentially passed as comma-delimited string.
$customProperties = Sniff::merge_custom_array( $this->allowed_custom_properties, array(), false );
$customProperties = RulesetPropertyHelper::merge_custom_array( $this->allowed_custom_properties, array(), false );

$this->allowed_mixed_case_member_var_names = Sniff::merge_custom_array(
$this->allowed_mixed_case_member_var_names = RulesetPropertyHelper::merge_custom_array(
$customProperties,
$this->allowed_mixed_case_member_var_names
);
Expand Down
5 changes: 3 additions & 2 deletions WordPress/Sniffs/PHP/NoSilencedErrorsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

namespace WordPressCS\WordPress\Sniffs\PHP;

use WordPressCS\WordPress\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniff;

/**
* Discourage the use of the PHP error silencing operator.
Expand Down Expand Up @@ -183,7 +184,7 @@ public function register() {
*/
public function process_token( $stackPtr ) {
// Handle the user-defined custom function list.
$this->customAllowedFunctionsList = $this->merge_custom_array( $this->customAllowedFunctionsList, array(), false );
$this->customAllowedFunctionsList = RulesetPropertyHelper::merge_custom_array( $this->customAllowedFunctionsList, array(), false );
$this->customAllowedFunctionsList = array_map( 'strtolower', $this->customAllowedFunctionsList );

/*
Expand Down
9 changes: 5 additions & 4 deletions WordPress/Sniffs/Security/EscapeOutputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\PassedParameters;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Helpers\VariableHelper;
use WordPressCS\WordPress\Sniff;

Expand Down Expand Up @@ -478,9 +479,9 @@ public function process_token( $stackPtr ) {
*/
protected function mergeFunctionLists() {
if ( $this->customEscapingFunctions !== $this->addedCustomFunctions['escape'] ) {
$customEscapeFunctions = $this->merge_custom_array( $this->customEscapingFunctions, array(), false );
$customEscapeFunctions = RulesetPropertyHelper::merge_custom_array( $this->customEscapingFunctions, array(), false );

$this->escapingFunctions = $this->merge_custom_array(
$this->escapingFunctions = RulesetPropertyHelper::merge_custom_array(
$customEscapeFunctions,
$this->escapingFunctions
);
Expand All @@ -489,7 +490,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customAutoEscapedFunctions !== $this->addedCustomFunctions['autoescape'] ) {
$this->autoEscapedFunctions = $this->merge_custom_array(
$this->autoEscapedFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customAutoEscapedFunctions,
$this->autoEscapedFunctions
);
Expand All @@ -499,7 +500,7 @@ protected function mergeFunctionLists() {

if ( $this->customPrintingFunctions !== $this->addedCustomFunctions['print'] ) {

$this->printingFunctions = $this->merge_custom_array(
$this->printingFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customPrintingFunctions,
$this->printingFunctions
);
Expand Down
7 changes: 4 additions & 3 deletions WordPress/Sniffs/Security/NonceVerificationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\MessageHelper;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Helpers\VariableHelper;
use WordPressCS\WordPress\Sniff;

Expand Down Expand Up @@ -296,7 +297,7 @@ private function has_nonce_check( $stackPtr ) {
*/
protected function mergeFunctionLists() {
if ( $this->customNonceVerificationFunctions !== $this->addedCustomFunctions['nonce'] ) {
$this->nonceVerificationFunctions = $this->merge_custom_array(
$this->nonceVerificationFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customNonceVerificationFunctions,
$this->nonceVerificationFunctions
);
Expand All @@ -305,7 +306,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customSanitizingFunctions !== $this->addedCustomFunctions['sanitize'] ) {
$this->sanitizingFunctions = $this->merge_custom_array(
$this->sanitizingFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customSanitizingFunctions,
$this->sanitizingFunctions
);
Expand All @@ -314,7 +315,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customUnslashingSanitizingFunctions !== $this->addedCustomFunctions['unslashsanitize'] ) {
$this->unslashingSanitizingFunctions = $this->merge_custom_array(
$this->unslashingSanitizingFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customUnslashingSanitizingFunctions,
$this->unslashingSanitizingFunctions
);
Expand Down
5 changes: 3 additions & 2 deletions WordPress/Sniffs/Security/ValidatedSanitizedInputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Helpers\VariableHelper;
use WordPressCS\WordPress\Sniff;

Expand Down Expand Up @@ -212,7 +213,7 @@ function( $embed ) {
*/
protected function mergeFunctionLists() {
if ( $this->customSanitizingFunctions !== $this->addedCustomFunctions['sanitize'] ) {
$this->sanitizingFunctions = $this->merge_custom_array(
$this->sanitizingFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customSanitizingFunctions,
$this->sanitizingFunctions
);
Expand All @@ -221,7 +222,7 @@ protected function mergeFunctionLists() {
}

if ( $this->customUnslashingSanitizingFunctions !== $this->addedCustomFunctions['unslashsanitize'] ) {
$this->unslashingSanitizingFunctions = $this->merge_custom_array(
$this->unslashingSanitizingFunctions = RulesetPropertyHelper::merge_custom_array(
$this->customUnslashingSanitizingFunctions,
$this->unslashingSanitizingFunctions
);
Expand Down
Loading