Documentation

ValidationHelper

FinalYes

Helper function for checking whether a token is validated.

Tags
since
3.0.0

The method in this class was previously contained in the WordPressCS\WordPress\Sniff class and has been moved here.

Table of Contents

Properties

$key_exists_functions  : array<string, bool>
List of PHP native functions to check if an array index exists.
$targets  : array<int|string, string>
The tokens the function looks for to determine whether a token is validated.

Methods

is_validated()  : bool
Check if the existence of a variable is validated with isset(), empty(), array_key_exists() or key_exists().
strip_quotes_from_array_values()  : array<string|int, string>
Strip quotes of all the values in an array containing only text strings.

Properties

$key_exists_functions

List of PHP native functions to check if an array index exists.

private static array<string, bool> $key_exists_functions = array('array_key_exists' => true, 'key_exists' => true)
Tags
since
3.0.0

$targets

The tokens the function looks for to determine whether a token is validated.

private static array<int|string, string> $targets = array(\T_ISSET => 'construct', \T_EMPTY => 'construct', \T_STRING => 'function_call', \T_COALESCE => 'coalesce', \T_COALESCE_EQUAL => 'coalesce')
Tags
since
3.0.0

Methods

is_validated()

Check if the existence of a variable is validated with isset(), empty(), array_key_exists() or key_exists().

public static is_validated(File $phpcsFile, int $stackPtr[, array<string|int, mixed>|string $array_keys = array() ][, bool $in_condition_only = false ]) : bool

When $in_condition_only is false, (which is the default), this is considered valid:

if ( isset( $var ) ) {
    // Do stuff, like maybe return or exit (but could be anything)
}

foo( $var );

When it is true, that would be invalid; the use of the variable must be within the scope of the validating condition, like this:

if ( isset( $var ) ) {
    foo( $var );
}
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The index of this token in the stack.

$array_keys : array<string|int, mixed>|string = array()

An array key to check for ("bar" in $foo['bar']) or an array of keys for multi-level array access.

$in_condition_only : bool = false

Whether to require that this use of the variable occurs within the scope of the validating condition, or just in the same scope (default).

Tags
since
0.5.0
since
2.1.0

Now recognizes array_key_exists() and key_exists() as validation functions.

since
2.1.0

Stricter check on whether the correct variable and the correct array keys are being validated.

since
3.0.0
  • Moved from the Sniff class to this class.
  • The method visibility was changed from protected to public static.
  • The $phpcsFile parameter was added.
Return values
bool

Whether the var is validated.

strip_quotes_from_array_values()

Strip quotes of all the values in an array containing only text strings.

private static strip_quotes_from_array_values(array<string|int, string> $text_strings) : array<string|int, string>
Parameters
$text_strings : array<string|int, string>

The input array.

Tags
since
3.0.0
Return values
array<string|int, string>

        
On this page

Search results