MinimumWPVersionTrait
Helper utilities for sniffs which take the minimum supported WP version of the code under examination into account.
Usage instructions:
- Add appropriate
usestatement(s) to the file/class which intends to use this functionality. - Call the
MinimumWPVersionTrait::set_minimum_wp_version()method in theprocess()/process_token()method. - After that, the
MinimumWPVersionTrait::$minimum_wp_versionproperty can be freely used in the sniff.
Tags
Table of Contents
Properties
- $minimum_wp_version : string
- Minimum supported WordPress version.
- $default_minimum_wp_version : string
- Default minimum supported WordPress version.
Methods
- set_minimum_wp_version() : void
- Overrule the minimum supported WordPress version with a command-line/config value.
- wp_version_compare() : bool
- Compares two version numbers.
- normalize_version_number() : string
- Normalize a version number.
Properties
$minimum_wp_version
Minimum supported WordPress version.
public
string
$minimum_wp_version
Currently used by the WordPress.Security.PreparedSQLPlaceholders,
WordPress.WP.AlternativeFunctions, WordPress.WP.Capabilities,
WordPress.WP.DeprecatedClasses, WordPress.WP.DeprecatedFunctions,
WordPress.WP.DeprecatedParameter and the WordPress.WP.DeprecatedParameterValues sniff.
These sniffs will adapt their behaviour based on the minimum supported WP version indicated. 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 sniffs using it via
the command line or by setting a <config> value in a custom phpcs.xml ruleset.
CL: phpcs --runtime-set minimum_wp_version 5.7
Ruleset: <config name="minimum_wp_version" value="6.0"/>
WordPress version.
Tags
$default_minimum_wp_version
Default minimum supported WordPress version.
private
string
$default_minimum_wp_version
= '6.2'
By default, the minimum_wp_version presumes that a project will support the current WP version and up to three releases before.
}
WordPress version.
Tags
Methods
set_minimum_wp_version()
Overrule the minimum supported WordPress version with a command-line/config value.
protected
final set_minimum_wp_version() : void
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_wp_version and/or a
$minimum_wp_version set for individual sniffs through the ruleset.
Tags
wp_version_compare()
Compares two version numbers.
protected
final wp_version_compare(string $version1, string $version2, string $operator) : bool
Parameters
- $version1 : string
-
First version number.
- $version2 : string
-
Second version number.
- $operator : string
-
Comparison operator.
Tags
Return values
boolnormalize_version_number()
Normalize a version number.
private
normalize_version_number(string $version) : string
Ensures that a version number is comparable via the PHP version_compare() function by making sure it complies with the minimum "PHP-standardized" version number requirements.
Presumes the input is a numeric version number string. The behaviour with other input is undefined.
Parameters
- $version : string
-
Version number.