Documentation

FunctionDeclarations

Utility functions for use when examining function declaration statements.

Table of Contents

$magicFunctions A list of all PHP magic functions. array
$magicMethods A list of all PHP magic methods. array
$methodsDoubleUnderscore A list of all PHP native non-magic methods starting with a double underscore. array
$arrowFunctionEndTokens Tokens which can be the end token of an arrow function. array
getName() Returns the declaration name for a function. string|null
getProperties() Retrieves the visibility and implementation properties of a method. array
getParameters() Retrieves the method parameters for the specified function token. array
isArrowFunction() Check if an arbitrary token is the "fn" keyword for a PHP 7.4 arrow function. bool
getArrowFunctionOpenClose() Retrieve the parenthesis opener, parenthesis closer, the scope opener and the scope closer for an arrow function. array|bool
isMagicFunction() Checks if a given function is a PHP magic function. bool
isMagicFunctionName() Verify if a given function name is the name of a PHP magic function. bool
isMagicMethod() Checks if a given function is a PHP magic method. bool
isMagicMethodName() Verify if a given function name is the name of a PHP magic method. bool
isPHPDoubleUnderscoreMethod() Checks if a given function is a PHP native double underscore method. bool
isPHPDoubleUnderscoreMethodName() Verify if a given function name is the name of a PHP native double underscore method. bool
isSpecialMethod() Checks if a given function is a magic method or a PHP native double underscore method. bool
isSpecialMethodName() Verify if a given function name is the name of a magic method or a PHP native double underscore method. bool

Properties

$magicFunctions

A list of all PHP magic functions.

public static array $magicFunctions = ['__autoload' => 'autoload']
The array keys are the function names, the values as well, but without the double underscore. The function names are listed in lowercase as function names in PHP are case-insensitive and comparisons against this list should therefore always be done in a case-insensitive manner.

$magicMethods

A list of all PHP magic methods.

public static array $magicMethods = [ '__construct' => 'construct', '__destruct' => 'destruct', '__call' => 'call', '__callstatic' => 'callstatic', '__get' => 'get', '__set' => 'set', '__isset' => 'isset', '__unset' => 'unset', '__sleep' => 'sleep', '__wakeup' => 'wakeup', '__tostring' => 'tostring', '__set_state' => 'set_state', '__clone' => 'clone', '__invoke' => 'invoke', '__debuginfo' => 'debuginfo', // PHP 5.6. '__serialize' => 'serialize', // PHP 7.4. '__unserialize' => 'unserialize', ]
The array keys are the method names, the values as well, but without the double underscore. The method names are listed in lowercase as function names in PHP are case-insensitive and comparisons against this list should therefore always be done in a case-insensitive manner.

$methodsDoubleUnderscore

A list of all PHP native non-magic methods starting with a double underscore.

public static array $methodsDoubleUnderscore = ['__dorequest' => 'SOAPClient', '__getcookies' => 'SOAPClient', '__getfunctions' => 'SOAPClient', '__getlastrequest' => 'SOAPClient', '__getlastrequestheaders' => 'SOAPClient', '__getlastresponse' => 'SOAPClient', '__getlastresponseheaders' => 'SOAPClient', '__gettypes' => 'SOAPClient', '__setcookie' => 'SOAPClient', '__setlocation' => 'SOAPClient', '__setsoapheaders' => 'SOAPClient', '__soapcall' => 'SOAPClient']
These come from PHP modules such as SOAPClient. The array keys are the method names, the values the name of the PHP extension containing the function. The method names are listed in lowercase as function names in PHP are case-insensitive and comparisons against this list should therefore always be done in a case-insensitive manner.

$arrowFunctionEndTokens

Tokens which can be the end token of an arrow function.

private static array $arrowFunctionEndTokens = [\T_COLON => true, \T_COMMA => true, \T_SEMICOLON => true, \T_CLOSE_PARENTHESIS => true, \T_CLOSE_SQUARE_BRACKET => true, \T_CLOSE_CURLY_BRACKET => true, \T_CLOSE_SHORT_ARRAY => true, \T_OPEN_TAG => true, \T_CLOSE_TAG => true]

Methods

getName()

Returns the declaration name for a function.

public static getName( $phpcsFile : File , $stackPtr : int ) : string|null

Alias for the \PHPCSUtils\Utils\ObjectDeclarations::getName() method.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the declaration token which declared the function.

Tags
codeCoverageIgnore
see

Original function.

see

PHPCSUtils native improved version.

since
throws

If the specified token is not of type T_FUNCTION, T_CLASS, T_TRAIT, or T_INTERFACE.

Return values
string|null

The name of the function; or NULL if the passed token doesn't exist, the function is anonymous or in case of a parse error/live coding.

getProperties()

Retrieves the visibility and implementation properties of a method.

public static getProperties( $phpcsFile : File , $stackPtr : int ) : array

The format of the return value is: array( 'scope' => 'public', // Public, private, or protected 'scope_specified' => true, // TRUE if the scope keyword was found. 'return_type' => '', // The return type of the method. 'return_type_token' => integer, // The stack pointer to the start of the return type // or FALSE if there is no return type. 'return_type_end_token' => integer, // The stack pointer to the end of the return type // or FALSE if there is no return type. 'nullable_return_type' => false, // TRUE if the return type is nullable. 'is_abstract' => false, // TRUE if the abstract keyword was found. 'is_final' => false, // TRUE if the final keyword was found. 'is_static' => false, // TRUE if the static keyword was found. 'has_body' => false, // TRUE if the method has a body );

Main differences with the PHPCS version:

  • Bugs fixed:
    • Handling of PHPCS annotations.
    • has_body index could be set to true for functions without body in the case of parse errors or live coding.
  • Defensive coding against incorrect calls to this method.
  • More efficient checking whether a function has a body.
  • New return_type_end_token (int|false) array index.
  • To allow for backward compatible handling of arrow functions, this method will also accept T_STRING tokens and examine them to check if these are arrow functions.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position in the stack of the function token to acquire the properties for.

Tags
see

Original source.

see

Cross-version compatible version of the original.

since
since

Added BC support for PHP 7.4 arrow functions.

throws

If the specified position is not a T_FUNCTION or T_CLOSURE token nor an arrow function.

Return values
array

getParameters()

Retrieves the method parameters for the specified function token.

public static getParameters( $phpcsFile : File , $stackPtr : int ) : array

Also supports passing in a USE token for a closure use group.

The returned array will contain the following information for each parameter:

0 => array( 'name' => '$var', // The variable name. 'token' => integer, // The stack pointer to the variable name. 'content' => string, // The full content of the variable definition. 'pass_by_reference' => boolean, // Is the variable passed by reference? 'reference_token' => integer, // The stack pointer to the reference operator // or FALSE if the param is not passed by reference. 'variable_length' => boolean, // Is the param of variable length through use of ... ? 'variadic_token' => integer, // The stack pointer to the ... operator // or FALSE if the param is not variable length. 'type_hint' => string, // The type hint for the variable. 'type_hint_token' => integer, // The stack pointer to the start of the type hint // or FALSE if there is no type hint. 'type_hint_end_token' => integer, // The stack pointer to the end of the type hint // or FALSE if there is no type hint. 'nullable_type' => boolean, // TRUE if the var type is nullable. 'comma_token' => integer, // The stack pointer to the comma after the param // or FALSE if this is the last param. )

Parameters with default values have the following additional array indexes: 'default' => string, // The full content of the default value. 'default_token' => integer, // The stack pointer to the start of the default value. 'default_equal_token' => integer, // The stack pointer to the equals sign.

Main differences with the PHPCS version:

  • Defensive coding against incorrect calls to this method.
  • More efficient and more stable checking whether a T_USE token is a closure use.
  • More efficient and more stable looping of the default value.
  • Clearer exception message when a non-closure use token was passed to the function.
  • To allow for backward compatible handling of arrow functions, this method will also accept T_STRING tokens and examine them to check if these are arrow functions.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position in the stack of the function token to acquire the parameters for.

Tags
see

Original source.

see

Cross-version compatible version of the original.

since
since

Added BC support for PHP 7.4 arrow functions.

throws

If the specified $stackPtr is not of type T_FUNCTION, T_CLOSURE or T_USE, nor an arrow function.

Return values
array

isArrowFunction()

Check if an arbitrary token is the "fn" keyword for a PHP 7.4 arrow function.

public static isArrowFunction( $phpcsFile : File , $stackPtr : int ) : bool

Helper function for cross-version compatibility with both PHP as well as PHPCS.

  • PHP 7.4+ will tokenize most tokens with the content "fn" as T_FN, even when it isn't an arrow function.
  • PHPCS < 3.5.3 will tokenize arrow functions keywords as T_STRING.
  • PHPCS 3.5.3/3.5.4 will tokenize the keyword differently depending on which PHP version is used and similar to PHP will tokenize most tokens with the content "fn" as T_FN, even when it's not an arrow function. Note: the tokens tokenized by PHPCS 3.5.3 - 3.5.4 as T_FN are not 100% the same as those tokenized by PHP 7.4+ as T_FN.

Either way, the T_FN token is not a reliable indicator that something is in actual fact an arrow function. This function solves that and will give reliable results in the same way as this is now solved in PHPCS 3.5.5.

Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The token to check. Typically a T_FN or T_STRING token as those are the only two tokens which can be the arrow function keyword.

Tags
see

Related function.

since
Return values
bool

TRUE is the token is the "fn" keyword for an arrow function. FALSE when not or in case of live coding/parse error.

getArrowFunctionOpenClose()

Retrieve the parenthesis opener, parenthesis closer, the scope opener and the scope closer for an arrow function.

public static getArrowFunctionOpenClose( $phpcsFile : File , $stackPtr : int ) : array|bool

Helper function for cross-version compatibility with both PHP as well as PHPCS. In PHPCS versions prior to PHPCS 3.5.3/3.5.4, the T_FN token is not yet backfilled and does not have parenthesis opener/closer nor scope opener/closer indexes assigned in the $tokens array.

Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The token to retrieve the openers/closers for. Typically a T_FN or T_STRING token as those are the only two tokens which can be the arrow function keyword.

Tags
see

Related function.

since
Return values
array|bool

An array with the token pointers or FALSE if this is not an arrow function. The format of the return value is: array( 'parenthesis_opener' => integer, // Stack pointer to the parenthesis opener. 'parenthesis_closer' => integer, // Stack pointer to the parenthesis closer. 'scope_opener' => integer, // Stack pointer to the scope opener (arrow). 'scope_closer' => integer, // Stack pointer to the scope closer. )

isMagicFunction()

Checks if a given function is a PHP magic function.

public static isMagicFunction( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The T_FUNCTION token to check.

Tags
todo

Add check for the function declaration being namespaced!

see

For when you already know the name of the function and scope checking is done in the sniff.

since
Return values
bool

isMagicFunctionName()

Verify if a given function name is the name of a PHP magic function.

public static isMagicFunctionName( $name : string ) : bool
Parameters
$name : string

The full function name.

Tags
since
Return values
bool

isMagicMethod()

Checks if a given function is a PHP magic method.

public static isMagicMethod( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The T_FUNCTION token to check.

Tags
see

For when you already know the name of the method and scope checking is done in the sniff.

since
Return values
bool

isMagicMethodName()

Verify if a given function name is the name of a PHP magic method.

public static isMagicMethodName( $name : string ) : bool
Parameters
$name : string

The full function name.

Tags
since
Return values
bool

isPHPDoubleUnderscoreMethod()

Checks if a given function is a PHP native double underscore method.

public static isPHPDoubleUnderscoreMethod( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The T_FUNCTION token to check.

Tags
see

For when you already know the name of the method and scope checking is done in the sniff.

since
Return values
bool

isPHPDoubleUnderscoreMethodName()

Verify if a given function name is the name of a PHP native double underscore method.

public static isPHPDoubleUnderscoreMethodName( $name : string ) : bool
Parameters
$name : string

The full function name.

Tags
since
Return values
bool

isSpecialMethod()

Checks if a given function is a magic method or a PHP native double underscore method.

public static isSpecialMethod( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The T_FUNCTION token to check.

Tags
see

For when you already know the name of the method and scope checking is done in the sniff.

since

{@internal Not the most efficient way of checking this, but less efficient ways will get less reliable results or introduce a lot of code duplication.}

Return values
bool

isSpecialMethodName()

Verify if a given function name is the name of a magic method or a PHP native double underscore method.

public static isSpecialMethodName( $name : string ) : bool
Parameters
$name : string

The full function name.

Tags
since
Return values
bool

Search results