Documentation

MinimumWPVersionTrait

Helper utilities for sniffs which take the minimum supported WP version of the code under examination into account.

Usage instructions:

  • Add appropriate use statement(s) to the file/class which intends to use this functionality.
  • Call the MinimumWPVersionTrait::set_minimum_wp_version() method in the process()/process_token() method.
  • After that, the MinimumWPVersionTrait::$minimum_wp_version property can be freely used in the sniff.
Tags
since
3.0.0

The property and method in this trait were previously contained in the WordPressCS\WordPress\Sniff class and have been moved here.

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
since
0.14.0

Previously the individual sniffs each contained this property.

since
3.0.0
  • Moved from the Sniff class to this dedicated Trait.
  • The property has been renamed from $minimum_supported_version to $minimum_wp_version.
  • The CLI option has been renamed from minimum_supported_wp_version to minimum_wp_version.
used-by
PreparedSQLPlaceholdersSniff
used-by
AlternativeFunctionsSniff
used-by
CapabilitiesSniff
used-by
DeprecatedClassesSniff
used-by
DeprecatedFunctionsSniff
used-by
DeprecatedParametersSniff
used-by
DeprecatedParameterValuesSniff

$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
since
3.0.0

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
since
0.14.0
since
3.0.0
  • Moved from the Sniff class to this dedicated Trait.
  • Renamed from get_wp_version_from_cl() to set_minimum_wp_version().

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
since
3.0.0
Return values
bool

normalize_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.

Tags
since
3.0.0
Return values
string

        
On this page

Search results