Documentation

AlternativeFunctionsSniff extends AbstractFunctionRestrictionsSniff uses MinimumWPVersionTrait

FinalYes

Discourages the use of various functions and suggests (WordPress) alternatives.

Tags
since
0.11.0
since
0.13.0

Class name changed: this class is now namespaced.

since
1.0.0
  • Takes the minimum supported WP version into account.
  • Takes exceptions based on passed parameters into account.
uses
MinimumWPVersionTrait::$minimum_wp_version

Table of Contents

Properties

$exclude  : array<string|int, mixed>
Exclude groups.
$minimum_wp_version  : string
Minimum supported WordPress version.
$unittest_groups  : array<string|int, mixed>
Groups of function data to check against.
$excluded_groups  : array<string|int, mixed>
Cache for the excluded groups information.
$groups  : array<string|int, mixed>
Cache for the group information.
$phpcsFile  : File
The current file being sniffed.
$regex_pattern  : string
Regex pattern with placeholder for the function names.
$tokens  : array<string|int, mixed>
The list of tokens in the current file being sniffed.
$allowed_local_stream_constants  : array<string|int, mixed>
Local input stream constants which should not be flagged for the file system function checks.
$allowed_local_stream_partials  : array<string|int, mixed>
Local input streams which should not be flagged for the file system function checks if the $filename starts with them.
$allowed_local_streams  : array<string|int, mixed>
Local input streams which should not be flagged for the file system function checks.
$default_minimum_wp_version  : string
Default minimum supported WordPress version.

Methods

check_for_matches()  : int|void
Verify if the current token is one of the targetted functions.
getGroups()  : array<string|int, mixed>
Groups of functions to restrict.
is_targetted_token()  : bool
Verify is the current token is a function call.
process()  : int|void
Set sniff properties and hand off to child class for processing of the token.
process_matched_token()  : int|void
Process a matched token.
process_token()  : int|void
Processes this test, when one of its tokens is encountered.
register()  : array<string|int, mixed>
Returns an array of tokens this test wants to listen for.
is_local_data_stream()  : bool
Determine based on the "clean" parameter value, whether a file parameter points to a local data stream.
prepare_name_for_regex()  : string
Prepare the function name for use in a regular expression.
set_minimum_wp_version()  : void
Overrule the minimum supported WordPress version with a command-line/config value.
setup_groups()  : bool
Set up the regular expressions for each group.
wp_version_compare()  : bool
Compares two version numbers.
normalize_version_number()  : string
Normalize a version number.

Properties

$exclude

Exclude groups.

public array<string|int, mixed> $exclude = array()

Example: 'switch_to_blog,user_meta'

Tags
since
0.3.0
since
1.0.0

This property now expects to be passed an array. Previously a comma-delimited string was expected.

$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

$unittest_groups

Groups of function data to check against.

public static array<string|int, mixed> $unittest_groups = array()

Don't use this in extended classes, override getGroups() instead. This is only used for Unit tests.

Tags
since
0.10.0

$phpcsFile

The current file being sniffed.

protected File $phpcsFile
Tags
since
0.4.0

$tokens

The list of tokens in the current file being sniffed.

protected array<string|int, mixed> $tokens
Tags
since
0.4.0

$allowed_local_stream_constants

Local input stream constants which should not be flagged for the file system function checks.

private array<string|int, mixed> $allowed_local_stream_constants = array('STDIN' => true, 'STDOUT' => true, 'STDERR' => true)
Tags
link
https://www.php.net/wrappers.php
since
2.1.0
since
3.0.0

The visibility was changed from protected to private.

$allowed_local_stream_partials

Local input streams which should not be flagged for the file system function checks if the $filename starts with them.

private array<string|int, mixed> $allowed_local_stream_partials = array('php://temp/', 'php://fd/')
Tags
link
https://www.php.net/wrappers.php
since
2.1.0
since
3.0.0

The visibility was changed from protected to private.

$allowed_local_streams

Local input streams which should not be flagged for the file system function checks.

private array<string|int, mixed> $allowed_local_streams = array('php://input' => true, 'php://output' => true, 'php://stdin' => true, 'php://stdout' => true, 'php://stderr' => true)
Tags
link
https://www.php.net/wrappers.php
since
2.1.0
since
3.0.0

The visibility was changed from protected to private.

$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

check_for_matches()

Verify if the current token is one of the targetted functions.

public check_for_matches(int $stackPtr) : int|void
Parameters
$stackPtr : int

The position of the current token in the stack.

Tags
since
0.11.0

Split out from the process() method.

Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

getGroups()

Groups of functions to restrict.

public getGroups() : array<string|int, mixed>

Example: groups => array( 'lambda' => array( 'type' => 'error' | 'warning', 'message' => 'Use anonymous functions instead please!', 'since' => '4.9.0', //=> the WP version in which the alternative became available. 'functions' => array( 'file_get_contents', 'create_function' ), ) )

Return values
array<string|int, mixed>

is_targetted_token()

Verify is the current token is a function call.

public is_targetted_token(int $stackPtr) : bool
Parameters
$stackPtr : int

The position of the current token in the stack.

Tags
since
0.11.0

Split out from the process() method.

Return values
bool

process()

Set sniff properties and hand off to child class for processing of the token.

public process(File $phpcsFile, int $stackPtr) : int|void
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the current token in the stack passed in $tokens.

Tags
since
0.11.0
Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

process_matched_token()

Process a matched token.

public process_matched_token(int $stackPtr, string $group_name, string $matched_content) : int|void
Parameters
$stackPtr : int

The position of the current token in the stack.

$group_name : string

The name of the group which was matched.

$matched_content : string

The token content (function name) which was matched in lowercase.

Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

process_token()

Processes this test, when one of its tokens is encountered.

public process_token(int $stackPtr) : int|void
Parameters
$stackPtr : int

The position of the current token in the stack.

Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

register()

Returns an array of tokens this test wants to listen for.

public register() : array<string|int, mixed>
Return values
array<string|int, mixed>

is_local_data_stream()

Determine based on the "clean" parameter value, whether a file parameter points to a local data stream.

protected is_local_data_stream(string $clean_param_value) : bool
Parameters
$clean_param_value : string

Parameter value without comments.

Return values
bool

True if this is a local data stream. False otherwise.

prepare_name_for_regex()

Prepare the function name for use in a regular expression.

protected prepare_name_for_regex(string $function_name) : string

The getGroups() method allows for providing function names with a wildcard * to target a group of functions. This prepare routine takes that into account while still safely escaping the function name for use in a regular expression.

Parameters
$function_name : string

Function name.

Tags
since
0.10.0
Return values
string

Regex escaped function name.

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().

setup_groups()

Set up the regular expressions for each group.

protected setup_groups(string $key) : bool
Parameters
$key : string

The group array index key where the input for the regular expression can be found.

Tags
since
0.10.0
Return values
bool

True if the groups were setup. False if not.

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