Variables
in package
Utility functions for use when examining variables.
Tags
Table of Contents
Properties
- $phpReservedVars : array<string, bool>
- List of PHP Reserved variables.
Methods
- getMemberProperties() : array<string, mixed>
- Retrieve the visibility and implementation properties of a class member variable.
- isPHPReservedVarName() : bool
- Verify if a given variable name is the name of a PHP reserved variable.
- isSuperglobal() : bool
- Verify if a given variable or array key token points to a PHP superglobal.
- isSuperglobalName() : bool
- Verify if a given variable name is the name of a PHP superglobal.
Properties
$phpReservedVars
List of PHP Reserved variables.
public
static array<string, bool>
$phpReservedVars
= [
'_SERVER' => true,
'_GET' => true,
'_POST' => true,
'_REQUEST' => true,
'_SESSION' => true,
'_ENV' => true,
'_COOKIE' => true,
'_FILES' => true,
'GLOBALS' => true,
'http_response_header' => false,
'argc' => false,
'argv' => false,
// Deprecated.
'php_errormsg' => false,
// Removed PHP 5.4.0.
'HTTP_SERVER_VARS' => false,
'HTTP_GET_VARS' => false,
'HTTP_POST_VARS' => false,
'HTTP_SESSION_VARS' => false,
'HTTP_ENV_VARS' => false,
'HTTP_COOKIE_VARS' => false,
'HTTP_POST_FILES' => false,
// Removed PHP 5.6.0.
'HTTP_RAW_POST_DATA' => false,
]
The array keys are the variable names without the leading dollar sign, the values indicate whether the variable is a superglobal or not.
The variables names are set without the leading dollar sign to allow this array
to be used with array index keys as well. Think: '_GET'
in $GLOBALS['_GET']
.}
Tags
Methods
getMemberProperties()
Retrieve the visibility and implementation properties of a class member variable.
public
static getMemberProperties(File $phpcsFile, int $stackPtr) : array<string, mixed>
Main differences with the PHPCS version:
- Removed the parse error warning for properties in interfaces. This will now throw the same "$stackPtr is not a class member var" runtime exception as other non-property variables passed to the method.
- Defensive coding against incorrect calls to this method.
- Support PHP 8.0 identifier name tokens in property types, cross-version PHP & PHPCS.
- The results of this function call are cached during a PHPCS run for faster response times.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position in the stack of the
T_VARIABLE
token to acquire the properties for.
Tags
Return values
array<string, mixed> —Array with information about the class member variable. The format of the return value is:
array(
'scope' => string, // Public, private, or protected.
'scope_specified' => boolean, // TRUE if the scope was explicitly specified.
'is_static' => boolean, // TRUE if the static keyword was found.
'is_readonly' => boolean, // TRUE if the readonly keyword was found.
'type' => string, // The type of the var (empty if no type specified).
'type_token' => integer|false, // The stack pointer to the start of the type
// or FALSE if there is no type.
'type_end_token' => integer|false, // The stack pointer to the end of the type
// or FALSE if there is no type.
'nullable_type' => boolean, // TRUE if the type is preceded by the
// nullability operator.
);
isPHPReservedVarName()
Verify if a given variable name is the name of a PHP reserved variable.
public
static isPHPReservedVarName(string $name) : bool
Parameters
- $name : string
-
The full variable name with or without leading dollar sign. This allows for passing an array key variable name, such as
'_GET'
retrieved from$GLOBALS['_GET']
.Note: when passing an array key, string quotes are expected to have been stripped already. Also see: TextStrings::stripQuotes().
Tags
Return values
boolisSuperglobal()
Verify if a given variable or array key token points to a PHP superglobal.
public
static isSuperglobal(File $phpcsFile, int $stackPtr) : bool
Parameters
- $phpcsFile : File
-
The file where this token was found.
- $stackPtr : int
-
The position in the stack of a
T_VARIABLE
token or of theT_CONSTANT_ENCAPSED_STRING
array key to a variable in$GLOBALS
.
Tags
Return values
bool —TRUE
if this points to a superglobal; FALSE
when not.
Note: This includes returning
FALSE
when an unsupported token has been passed, when aT_CONSTANT_ENCAPSED_STRING
has been passed which is not an array index key; or when it is, but is not an index to the$GLOBALS
variable.
isSuperglobalName()
Verify if a given variable name is the name of a PHP superglobal.
public
static isSuperglobalName(string $name) : bool
Parameters
- $name : string
-
The full variable name with or without leading dollar sign. This allows for passing an array key variable name, such as
'_GET'
retrieved from$GLOBALS['_GET']
.Note: when passing an array key, string quotes are expected to have been stripped already. Also see: TextStrings::stripQuotes().