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

Allow for setting the minimum supported WP version from the command line #1094

Merged
merged 1 commit into from
Aug 7, 2017
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
100 changes: 78 additions & 22 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
* <rule ref="WordPress.WP.DeprecatedClasses">
* <properties>
* <property name="minimum_supported_version" value="4.3"/>
* </properties>
* </rule>
*
* Alternatively, the value can be passed in one go for all sniff using it via
* the command line or by setting a `<config>` 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: `<config name="minimum_supported_wp_version" value="4.5"/>`
*
* @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:
* <rule ref="WordPress.[Subset].[Sniffname]">
* <properties>
* <property name="custom_test_class_whitelist" type="array" value="My_Plugin_First_Test_Class,My_Plugin_Second_Test_Class"/>
* </properties>
* </rule>
*
* @since 0.11.0
*
* @var string|string[]
*/
public $custom_test_class_whitelist = array();

/**
* List of the functions which verify nonces.
*
Expand Down Expand Up @@ -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:
* <rule ref="WordPress.[Subset].[Sniffname]">
* <properties>
* <property name="custom_test_class_whitelist" type="array" value="My_Plugin_First_Test_Class,My_Plugin_Second_Test_Class"/>
* </properties>
* </rule>
*
* @since 0.11.0
*
* @var string|string[]
*/
public $custom_test_class_whitelist = array();

/**
* The current file being sniffed.
*
Expand Down Expand Up @@ -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 `<config>` 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.
*
Expand Down
36 changes: 14 additions & 22 deletions WordPress/Sniffs/WP/DeprecatedClassesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <config> 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:
* <rule ref="WordPress.WP.DeprecatedClasses">
* <properties>
* <property name="minimum_supported_version" value="4.3"/>
* </properties>
* </rule>
*
* @var string WordPress versions.
*/
public $minimum_supported_version = '4.5';

/**
* List of deprecated classes with alternative when available.
*
Expand Down Expand Up @@ -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.';
Expand Down
36 changes: 14 additions & 22 deletions WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <config> 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:
* <rule ref="WordPress.WP.DeprecatedFunctions">
* <properties>
* <property name="minimum_supported_version" value="4.3"/>
* </properties>
* </rule>
*
* @var string WordPress versions.
*/
public $minimum_supported_version = '4.5';

/**
* List of deprecated functions with alternative when available.
*
Expand Down Expand Up @@ -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.';
Expand Down
27 changes: 8 additions & 19 deletions WordPress/Sniffs/WP/DeprecatedParametersSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <config> value
* in a custom ruleset.
*
* @uses \WordPress\Sniff::$minimum_supported_version
*/
class DeprecatedParametersSniff extends AbstractFunctionParameterSniff {

Expand All @@ -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:
* <rule ref="WordPress.WP.DeprecatedParameters">
* <properties>
* <property name="minimum_supported_version" value="4.5"/>
* </properties>
* </rule>
*
* @since 0.12.0
*
* @var string WordPress version.
*/
public $minimum_supported_version = 4.5;

/**
* Array of function, argument, and default value for deprecated argument.
*
Expand Down Expand Up @@ -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 ) {

Expand Down