From 80442d21264a9e540cd36d19992694c973b2a554 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 7 Aug 2017 08:34:38 +0200 Subject: [PATCH] Allow for setting the minimum supported WP version from the command line This change allows for setting the `minimum_supported_version` property for all three `WP.Deprecated...` sniffs in one go, either via the command-line or by setting a config variable in a custom ruleset. As this config variable is no longer directly related to a sniff, I've chosen to make the name slightly more specific/descriptive: `minimum_supported_wp_version` <= note the `_wp_`. Usage: ```bash phpcs . --standard=WordPress --runtime-set minimum_supported_wp_version 4.5 ``` or in a ruleset: ```xml ``` The above one liner can replace the custom property which would otherwise need to be set for all three sniffs individually. Overrule order: 1. Value passed via the command line 2. `` property in the ruleset 3. Individual sniff ``'s set via the ruleset 4. Default value Simplest way to test this is by running PHPCS over the `WP.DeprecatedParameters` sniff unit tests: ```bash phpcs -p -s ./WordPress/Tests/DeprecatedParametersUnitTest.inc --standard=WordPress --sniffs=WordPress.WP.DeprecatedParameters phpcs -p -s ./WordPress/Tests/DeprecatedParametersUnitTest.inc --standard=WordPress --sniffs=WordPress.WP.DeprecatedParameters --runtime-set minimum_supported_wp_version 4.8 phpcs -p -s ./WordPress/Tests/DeprecatedParametersUnitTest.inc --standard=WordPress --sniffs=WordPress.WP.DeprecatedParameters --runtime-set minimum_supported_wp_version 3.8 ``` Additional notes: * I've moved the `$custom_test_class_whitelist` property up in the `WordPress\Sniff` class, so both `public` properties are now at the top of the file. --- WordPress/Sniff.php | 100 ++++++++++++++---- .../Sniffs/WP/DeprecatedClassesSniff.php | 36 +++---- .../Sniffs/WP/DeprecatedFunctionsSniff.php | 36 +++---- .../Sniffs/WP/DeprecatedParametersSniff.php | 27 ++--- 4 files changed, 114 insertions(+), 85 deletions(-) diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index f776a8f55d..de5f2369c7 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -12,6 +12,7 @@ use PHP_CodeSniffer_Sniff as PHPCS_Sniff; use PHP_CodeSniffer_File as File; use PHP_CodeSniffer_Tokens as Tokens; +use WordPress\PHPCSHelper; /** * Represents a PHP_CodeSniffer sniff for sniffing WordPress coding standards. @@ -34,6 +35,64 @@ */ abstract class Sniff implements PHPCS_Sniff { + /** + * Minimum supported WordPress version. + * + * Currently used by the `WordPress.WP.DeprecatedClasses`, + * `WordPress.WP.DeprecatedFunctions` and the `WordPress.WP.DeprecatedParameter` sniff. + * + * These sniffs will throw an error when usage of a deprecated class/function/parameter + * is detected if the class/function/parameter was deprecated before the minimum + * supported WP version; a warning otherwise. + * By default, it is set to presume that a project will support the current + * WP version and up to three releases before. + * + * This property allows changing the minimum supported WP version used by + * these sniffs by setting a property in a custom phpcs.xml ruleset. + * This property will need to be set for each sniff which uses it. + * + * Example usage: + * + * + * + * + * + * + * Alternatively, the value can be passed in one go for all sniff using it via + * the command line or by setting a `` value in a custom phpcs.xml ruleset. + * Note: the `_wp_` in the command line property name! + * + * CL: `phpcs --runtime-set minimum_supported_wp_version 4.5` + * Ruleset: `` + * + * @since 0.14.0 Previously the individual sniffs each contained this property. + * + * @var string WordPress version. + */ + public $minimum_supported_version = '4.5'; + + /** + * Custom list of classes which test classes can extend. + * + * This property allows end-users to add to the $test_class_whitelist via their ruleset. + * This property will need to be set for each sniff which uses the + * `is_test_class()` method. + * Currently the method is used by the `WordPress.Variables.GlobalVariables`, + * `WordPress.NamingConventions.PrefixAllGlobals` and the `WordPress.Files.Filename` sniffs. + * + * Example usage: + * + * + * + * + * + * + * @since 0.11.0 + * + * @var string|string[] + */ + public $custom_test_class_whitelist = array(); + /** * List of the functions which verify nonces. * @@ -775,28 +834,6 @@ abstract class Sniff implements PHPCS_Sniff { 'PHPUnit\Framework\TestCase' => true, ); - /** - * Custom list of classes which test classes can extend. - * - * This property allows end-users to add to the $test_class_whitelist via their ruleset. - * This property will need to be set for each sniff which uses the - * `is_test_class()` method. - * Currently the method is used by the `WordPress.Variables.GlobalVariables`, - * `WordPress.NamingConventions.PrefixAllGlobals` and the `WordPress.Files.Filename` sniffs. - * - * Example usage: - * - * - * - * - * - * - * @since 0.11.0 - * - * @var string|string[] - */ - public $custom_test_class_whitelist = array(); - /** * The current file being sniffed. * @@ -1042,6 +1079,25 @@ protected function get_last_ptr_on_line( $stackPtr ) { return $lastPtr; } + /** + * Overrule the minimum supported WordPress version with a command-line/config value. + * + * Handle setting the minimum supported WP version in one go for all sniffs which + * expect it via the command line or via a `` variable in a ruleset. + * The config variable overrules the default `$minimum_supported_version` and/or a + * `$minimum_supported_version` set for individual sniffs through the ruleset. + * + * @since 0.14.0 + */ + protected function get_wp_version_from_cl() { + $cl_supported_version = trim( PHPCSHelper::get_config_data( 'minimum_supported_wp_version' ) ); + if ( ! empty( $cl_supported_version ) + && filter_var( $cl_supported_version, FILTER_VALIDATE_FLOAT ) !== false + ) { + $this->minimum_supported_version = $cl_supported_version; + } + } + /** * Find whitelisting comment. * diff --git a/WordPress/Sniffs/WP/DeprecatedClassesSniff.php b/WordPress/Sniffs/WP/DeprecatedClassesSniff.php index 05d0428f11..de9b36dabb 100644 --- a/WordPress/Sniffs/WP/DeprecatedClassesSniff.php +++ b/WordPress/Sniffs/WP/DeprecatedClassesSniff.php @@ -14,35 +14,24 @@ /** * Restricts the use of deprecated WordPress classes and suggests alternatives. * + * This sniff will throw an error when usage of a deprecated class is detected + * if the class was deprecated before the minimum supported WP version; + * a warning otherwise. + * By default, it is set to presume that a project will support the current + * WP version and up to three releases before. + * * @package WPCS\WordPressCodingStandards * * @since 0.12.0 * @since 0.13.0 Class name changed: this class is now namespaced. + * @since 0.14.0 Now has the ability to handle minimum supported WP version + * being provided via the command-line or as as value + * in a custom ruleset. + * + * @uses \WordPress\Sniff::$minimum_supported_version */ class DeprecatedClassesSniff extends AbstractClassRestrictionsSniff { - /** - * Minimum WordPress version. - * - * This sniff will throw an error when usage of a deprecated class is - * detected if the class was deprecated before the minimum supported - * WP version; a warning otherwise. - * By default, it is set to presume that a project will support the current - * WP version and up to three releases before. - * This variable allows changing the minimum supported WP version used by - * this sniff by setting a property in a custom phpcs.xml ruleset. - * - * Example usage: - * - * - * - * - * - * - * @var string WordPress versions. - */ - public $minimum_supported_version = '4.5'; - /** * List of deprecated classes with alternative when available. * @@ -92,6 +81,9 @@ public function getGroups() { * @return void */ public function process_matched_token( $stackPtr, $group_name, $matched_content ) { + + $this->get_wp_version_from_cl(); + $class_name = ltrim( strtolower( $matched_content ), '\\' ); $message = 'The %s class has been deprecated since WordPress version %s.'; diff --git a/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php b/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php index d2cc749104..97f2f0475d 100644 --- a/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php +++ b/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php @@ -14,35 +14,24 @@ /** * Restricts the use of various deprecated WordPress functions and suggests alternatives. * + * This sniff will throw an error when usage of deprecated functions is detected + * if the function was deprecated before the minimum supported WP version; + * a warning otherwise. + * By default, it is set to presume that a project will support the current + * WP version and up to three releases before. + * * @package WPCS\WordPressCodingStandards * * @since 0.11.0 * @since 0.13.0 Class name changed: this class is now namespaced. + * @since 0.14.0 Now has the ability to handle minimum supported WP version + * being provided via the command-line or as as value + * in a custom ruleset. + * + * @uses \WordPress\Sniff::$minimum_supported_version */ class DeprecatedFunctionsSniff extends AbstractFunctionRestrictionsSniff { - /** - * Minimum WordPress version. - * - * This sniff will throw an error when usage of deprecated functions is - * detected if the function was deprecated before the minimum supported - * WP version; a warning otherwise. - * By default, it is set to presume that a project will support the current - * WP version and up to three releases before. - * This variable allows changing the minimum supported WP version used by - * this sniff by setting a property in a custom phpcs.xml ruleset. - * - * Example usage: - * - * - * - * - * - * - * @var string WordPress versions. - */ - public $minimum_supported_version = '4.5'; - /** * List of deprecated functions with alternative when available. * @@ -1341,6 +1330,9 @@ public function getGroups() { * @return void */ public function process_matched_token( $stackPtr, $group_name, $matched_content ) { + + $this->get_wp_version_from_cl(); + $function_name = strtolower( $matched_content ); $message = '%s() has been deprecated since WordPress version %s.'; diff --git a/WordPress/Sniffs/WP/DeprecatedParametersSniff.php b/WordPress/Sniffs/WP/DeprecatedParametersSniff.php index 7f0c26e04b..7512c40c55 100644 --- a/WordPress/Sniffs/WP/DeprecatedParametersSniff.php +++ b/WordPress/Sniffs/WP/DeprecatedParametersSniff.php @@ -24,6 +24,11 @@ * * @since 0.12.0 * @since 0.13.0 Class name changed: this class is now namespaced. + * @since 0.14.0 Now has the ability to handle minimum supported WP version + * being provided via the command-line or as as value + * in a custom ruleset. + * + * @uses \WordPress\Sniff::$minimum_supported_version */ class DeprecatedParametersSniff extends AbstractFunctionParameterSniff { @@ -36,25 +41,6 @@ class DeprecatedParametersSniff extends AbstractFunctionParameterSniff { */ protected $group_name = 'wp_deprecated_parameters'; - /** - * Minimum WordPress version. - * - * This variable allows changing the minimum supported WP version used by - * this sniff by setting a property in a custom ruleset XML file. - * - * Example usage: - * - * - * - * - * - * - * @since 0.12.0 - * - * @var string WordPress version. - */ - public $minimum_supported_version = 4.5; - /** * Array of function, argument, and default value for deprecated argument. * @@ -293,6 +279,9 @@ class DeprecatedParametersSniff extends AbstractFunctionParameterSniff { * @return void */ public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) { + + $this->get_wp_version_from_cl(); + $paramCount = count( $parameters ); foreach ( $this->target_functions[ $matched_content ] as $position => $parameter_args ) {