PHPCSUtils 1.2.1

Variables
in package

FinalYes

Utility functions for use when examining variables.

Tags
since
1.0.0

The Variables::getMemberProperties() method is based on and inspired by the method of the same name in the PHPCS native PHP_CodeSniffer\Files\File class. Also see BCFile.

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
link

PHP Manual on reserved variables

since
1.0.0

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
since
1.2.0
throws
TypeError

If the $stackPtr parameter is not an integer.

throws
OutOfBoundsStackPtr

If the token passed does not exist in the $phpcsFile.

throws
UnexpectedTokenType

If the token passed is not a T_VARIABLE token.

throws
ValueError

If the token passed does not point to an OO property token or a parameter in a function declaration.

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_VARIABLE token to acquire the properties for.

Tags
see
File::getMemberProperties()

Original source.

see
BCFile::getMemberProperties()

Cross-version compatible version of the original.

since
1.0.0
throws
TypeError

If the $stackPtr parameter is not an integer.

throws
OutOfBoundsStackPtr

If the token passed does not exist in the $phpcsFile.

throws
UnexpectedTokenType

If the token passed is not a T_VARIABLE token.

throws
ValueError

If the specified position is not a class member variable.

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
see
Variables::$phpReservedVars

List of variables names reserved by PHP.

since
1.0.0
Return values
bool

isSuperglobal()

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 the T_CONSTANT_ENCAPSED_STRING array key to a variable in $GLOBALS.

Tags
since
1.0.0
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 a T_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().

Tags
since
1.0.0
Return values
bool

        
On this page

Search results