Documentation

AbstractClassRestrictionsSniff extends AbstractFunctionRestrictionsSniff

AbstractYes

Restricts usage of some classes.

Tags
since
0.10.0

Table of Contents

Properties

$exclude  : array<string|int, mixed>
Exclude groups.
$unittest_groups  : array<string|int, mixed>
Groups of function data to check against.
$classname  : string
Temporary storage for retrieved class name.
$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 class names.
$tokens  : array<string|int, mixed>
The list of tokens in the current file being sniffed.

Methods

check_for_matches()  : int|void
Verify if the current token is one of the targetted classes.
getGroups()  : array<string|int, mixed>
Groups of functions to restrict.
is_targetted_token()  : bool
Determine if we have a valid classname for the target token.
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.
get_namespaced_classname()  : string
See if the classname was found in a namespaced file and if so, add the namespace to the classname.
prepare_name_for_regex()  : string
Prepare the class name for use in a regular expression.
setup_groups()  : bool
Set up the regular expressions for each group.

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.

$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

Methods

check_for_matches()

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

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 abstract getGroups() : array<string|int, mixed>

This method should be overridden in extending classes.

Example: groups => array( 'lambda' => array( 'type' => 'error' | 'warning', 'message' => 'Use anonymous functions instead please!', 'functions' => array( 'file_get_contents', 'create_function', 'mysql_*' ), // Only useful when using wildcards: 'allow' => array( 'mysql_to_rfc3339' => true, ), ) )

You can use * wildcards to target a group of functions. When you use * wildcards, you may inadvertently restrict too many functions. In that case you can add the allow key to safe list individual functions to prevent false positives.

Return values
array<string|int, mixed>

is_targetted_token()

Determine if we have a valid classname for the target token.

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

The position of the current token in the stack.

Tags
since
0.11.0

This logic was originally contained in 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 it original case.

Tags
since
0.11.0

Split out from the process() method.

phpcs:disable

Generic.CodeAnalysis.UselessOverridingMethod.Found

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>

get_namespaced_classname()

See if the classname was found in a namespaced file and if so, add the namespace to the classname.

protected get_namespaced_classname(string $classname, int $search_from) : string
Parameters
$classname : string

The full classname as found.

$search_from : int

The token position to search up from.

Return values
string

Classname, potentially prefixed with the namespace.

prepare_name_for_regex()

Prepare the class name for use in a regular expression.

protected prepare_name_for_regex(string $classname) : string

The getGroups() method allows for providing class names with a wildcard * to target a group of classes within a namespace. It also allows for providing class names as 'ordinary' names or prefixed with one or more namespaces. This prepare routine takes that into account while still safely escaping the class name for use in a regular expression.

Parameters
$classname : string

Class name, potentially prefixed with namespaces.

Return values
string

Regex escaped class name.

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.


        
On this page

Search results