PHPCSUtils

Variables
in package

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.

since
1.0.0-alpha4

Dropped support for PHPCS < 3.7.1.

Table of Contents

$phpReservedVars  : array<string|int, mixed>
List of PHP Reserved variables.
getMemberProperties()  : array<string|int, 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|int, mixed> $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

getMemberProperties()

Retrieve the visibility and implementation properties of a class member variable.

public static getMemberProperties(File $phpcsFile, int $stackPtr) : array<string|int, 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.
  • Support for the PHP 8.2 true type.
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
since
1.0.0-alpha4

Added support for PHP 8.0 union types.

since
1.0.0-alpha4

No longer gets confused by PHP 8.0 property attributes.

since
1.0.0-alpha4

Added support for PHP 8.1 readonly properties.

since
1.0.0-alpha4

Added support for PHP 8.1 intersection types.

since
1.0.0-alpha4

Added support for PHP 8.2 true type.

throws
RuntimeException

If the specified position is not a T_VARIABLE token.

throws
RuntimeException

If the specified position is not a class member variable.

Return values
array<string|int, 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, // The stack pointer to the start of the type
                                // or FALSE if there is no type.
  'type_end_token'  => integer, // 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

Search results