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
- getAttributeOpeners() : array<string|int, int>
- Retrieve the stack pointers to the attribute openers for any attribute block which applies to an OO property or function declaration parameters.
- 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
getAttributeOpeners()
Retrieve the stack pointers to the attribute openers for any attribute block which applies to an OO property or function declaration parameters.
public
static getAttributeOpeners(File $phpcsFile, int $stackPtr) : array<string|int, int>
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position in the stack of the variable token to acquire the attributes for.
Tags
Return values
array<string|int, int> —Array with the stack pointers to the applicable attribute openers or an empty array if there are no attributes attached to the OO property or function declaration parameter.
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 enums (PHPCS 4.0 makes the same change). 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_VARIABLEtoken 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.
'set_scope' => string|false, // Scope for asymmetric visibility.
// Either public, private, or protected or
// FALSE if no set scope is specified.
'is_static' => boolean, // TRUE if the static keyword was found.
'is_readonly' => boolean, // TRUE if the readonly keyword was found.
'is_final' => boolean, // TRUE if the final keyword was found.
'is_abstract' => boolean, // TRUE if the abstract 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_VARIABLEtoken or of theT_CONSTANT_ENCAPSED_STRINGarray key to a variable in$GLOBALS.
Tags
Return values
bool —TRUE if this points to a superglobal; FALSE when not.
Note: This includes returning
FALSEwhen an unsupported token has been passed, when aT_CONSTANT_ENCAPSED_STRINGhas been passed which is not an array index key; or when it is, but is not an index to the$GLOBALSvariable.
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().