HumHub Documentation (unofficial)

ForbiddenGetClassNullSniff extends AbstractFunctionCallParameterSniff
in package

Detect: Passing `null` to `get_class()` is no longer allowed as of PHP 7.2.

This will now result in an E_WARNING being thrown.

PHP version 7.2

Tags
link
https://wiki.php.net/rfc/get_class_disallow_null_parameter
link
https://www.php.net/manual/en/function.get-class.php#refsect1-function.get-class-changelog
since
9.0.0

Table of Contents

Constants

REGEX_COMPLEX_VARS  = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'
Regex to match variables in a double quoted string.

Properties

$hashAlgoFunctions  : array<string|int, mixed>
List of functions using hash algorithm as parameter (always the first parameter).
$iniFunctions  : array<string|int, mixed>
List of functions which take an ini directive as parameter (always the first parameter).
$isMethod  : bool
Is the sniff looking for a function call or a method call ?
$superglobals  : array<string|int, mixed>
List of superglobals as an array of strings.
$targetFunctions  : array<string|int, mixed>
Functions to check for.
$ignoreTokens  : array<string|int, mixed>
List of tokens which when they preceed the $stackPtr indicate that this is not a function call.

Methods

addMessage()  : void
Add a PHPCS message to the output stack as either a warning or an error.
arrayKeysToLowercase()  : array<string|int, mixed>
Make all top level array keys in an array lowercase.
determineNamespace()  : string
Determine the namespace name an arbitrary token lives in.
doesFunctionCallHaveParameters()  : bool
Checks if a function call has parameters.
getCompleteTextString()  : string
Get the complete contents of a multi-line text string.
getDeclaredNamespaceName()  : string|false
Get the complete namespace name for a namespace declaration.
getFQClassNameFromDoubleColonToken()  : string
Returns the class name for the static usage of a class.
getFQClassNameFromNewToken()  : string
Returns the fully qualified class name for a new class instantiation.
getFQExtendedClassName()  : string
Returns the fully qualified name of the class that the specified class extends.
getFQName()  : string
Get the Fully Qualified name for a class/function/constant etc.
getFunctionCallParameter()  : array<string|int, mixed>|false
Get information on a specific parameter passed to a function call.
getFunctionCallParameterCount()  : int
Count the number of parameters a function call has been passed.
getFunctionCallParameters()  : array<string|int, mixed>
Get information on all parameters passed to a function call.
getHashAlgorithmParameter()  : string|false
Get the hash algorithm name from the parameter in a hash function call.
getReturnTypeHintName()  : string
Get the complete return type declaration for a given function.
getReturnTypeHintToken()  : int|false
Get the stack pointer for a return type token for a given function.
getTypeHintsFromFunctionDeclaration()  : array<string|int, mixed>
Get an array of just the type hints from a function declaration.
inClassScope()  : bool
Verify whether a token is within a class scope.
isClassConstant()  : bool
Check whether a T_CONST token is a class constant declaration.
isClassProperty()  : bool
Check whether a T_VARIABLE token is a class property declaration.
isNamespaced()  : bool
Is the class/function/constant name namespaced or global ?
isNegativeNumber()  : bool
Determine whether the tokens between $start and $end together form a negative number as recognized by PHP.
isPositiveNumber()  : bool
Determine whether the tokens between $start and $end together form a positive number as recognized by PHP.
isShortList()  : bool
Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a list() construct.
isShortTernary()  : bool
Determine whether a ternary is a short ternary, i.e. without "middle".
isUnaryPlusMinus()  : bool
Determine whether a T_MINUS/T_PLUS token is a unary operator.
isUseOfGlobalConstant()  : bool
Determine whether an arbitrary T_STRING token is the use of a global constant.
isVariable()  : bool
Determine whether the tokens between $start and $end could together represent a variable.
process()  : int|void
Processes this test, when one of its tokens is encountered.
processNoParameters()  : int|void
Process the function if no parameters were found.
processParameters()  : int|void
Process the parameters of a matched function.
register()  : array<string|int, mixed>
Returns an array of tokens this test wants to listen for.
stringToErrorCode()  : string
Convert an arbitrary string to an alphanumeric string with underscores.
stripQuotes()  : string
Strip quotes surrounding an arbitrary string.
stripVariables()  : string
Strip variables from an arbitrary double quoted string.
supportsAbove()  : bool
Check whether a specific PHP version is equal to or higher than the maximum supported PHP version as provided by the user in `testVersion`.
supportsBelow()  : bool
Check whether a specific PHP version is equal to or lower than the minimum supported PHP version as provided by the user in `testVersion`.
tokenHasScope()  : bool
Verify whether a token is within a scoped condition.
bowOutEarly()  : bool
Do a version check to determine if this sniff needs to run at all.
isNumber()  : int|float|bool
Determine whether the tokens between $start and $end together form a number as recognized by PHP.
isNumericCalculation()  : bool
Determine whether the tokens between $start and $end together form a numberic calculation as recognized by PHP.
validDirectScope()  : int|bool
Check whether the direct wrapping scope of a token is within a limited set of acceptable tokens.
getTestVersion()  : array<string|int, mixed>
Get the testVersion configuration variable.

Constants

REGEX_COMPLEX_VARS

Regex to match variables in a double quoted string.

public string REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'

This matches plain variables, but also more complex variables, such as $obj->prop, self::prop and $var[].

Tags
since
7.1.2

Properties

$hashAlgoFunctions

List of functions using hash algorithm as parameter (always the first parameter).

protected array<string|int, mixed> $hashAlgoFunctions = array('hash_file' => 1, 'hash_hmac_file' => 1, 'hash_hmac' => 1, 'hash_init' => 1, 'hash_pbkdf2' => 1, 'hash' => 1)

Used by the new/removed hash algorithm sniffs. Key is the function name, value is the 1-based parameter position in the function call.

Tags
since
5.5
since
7.0.7

Moved from the RemovedHashAlgorithms sniff to the base Sniff class.

$iniFunctions

List of functions which take an ini directive as parameter (always the first parameter).

protected array<string|int, mixed> $iniFunctions = array('ini_get' => 1, 'ini_set' => 1)

Used by the new/removed ini directives sniffs. Key is the function name, value is the 1-based parameter position in the function call.

Tags
since
7.1.0

$isMethod

Is the sniff looking for a function call or a method call ?

protected bool $isMethod = false

Note: the child class may need to do additional checks to make sure that the method called is of the right class/object. Checking that is outside of the scope of this abstract sniff.

False (default) if the sniff is looking for function calls. True if the sniff is looking for method calls.

Tags
since
8.2.0

$superglobals

List of superglobals as an array of strings.

protected array<string|int, mixed> $superglobals = array('$GLOBALS' => true, '$_SERVER' => true, '$_GET' => true, '$_POST' => true, '$_FILES' => true, '$_COOKIE' => true, '$_SESSION' => true, '$_REQUEST' => true, '$_ENV' => true)

Used by the ForbiddenParameterShadowSuperGlobals and ForbiddenClosureUseVariableNames sniffs.

Tags
since
7.0.0
since
7.1.4

Moved from the ForbiddenParameterShadowSuperGlobals sniff to the base Sniff class.

$targetFunctions

Functions to check for.

protected array<string|int, mixed> $targetFunctions = array('get_class' => true)
Tags
since
9.0.0

$ignoreTokens

List of tokens which when they preceed the $stackPtr indicate that this is not a function call.

private array<string|int, mixed> $ignoreTokens = array(\T_DOUBLE_COLON => true, \T_OBJECT_OPERATOR => true, \T_FUNCTION => true, \T_NEW => true, \T_CONST => true, \T_USE => true)
Tags
since
8.2.0

Methods

addMessage()

Add a PHPCS message to the output stack as either a warning or an error.

public addMessage(PHP_CodeSniffer_File $phpcsFile, string $message, int $stackPtr, bool $isError[, string $code = 'Found' ][, array<string|int, mixed> $data = array() ]) : void
Parameters
$phpcsFile : PHP_CodeSniffer_File

The file the message applies to.

$message : string

The message.

$stackPtr : int

The position of the token the message relates to.

$isError : bool

Whether to report the message as an 'error' or 'warning'. Defaults to true (error).

$code : string = 'Found'

The error code for the message. Defaults to 'Found'.

$data : array<string|int, mixed> = array()

Optional input for the data replacements.

Tags
since
7.1.0

arrayKeysToLowercase()

Make all top level array keys in an array lowercase.

public arrayKeysToLowercase(array<string|int, mixed> $array) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

Initial array.

Tags
since
7.1.0
Return values
array<string|int, mixed>

Same array, but with all lowercase top level keys.

determineNamespace()

Determine the namespace name an arbitrary token lives in.

public determineNamespace(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : string
Parameters
$phpcsFile : PHP_CodeSniffer_File

Instance of phpcsFile.

$stackPtr : int

The token position for which to determine the namespace.

Tags
since
7.0.3
Return values
string

Namespace name or empty string if it couldn't be determined or no namespace applies.

doesFunctionCallHaveParameters()

Checks if a function call has parameters.

public doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool

Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. If passed a T_STRING which is not a function call, the behaviour is unreliable.

Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it will detect whether the array has values or is empty.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the function call token.

Tags
link
https://github.com/PHPCompatibility/PHPCompatibility/issues/120
link
https://github.com/PHPCompatibility/PHPCompatibility/issues/152
since
7.0.3
Return values
bool

getCompleteTextString()

Get the complete contents of a multi-line text string.

public getCompleteTextString(PHP_CodeSniffer_File $phpcsFile, int $stackPtr[, bool $stripQuotes = true ]) : string

N.B.: This is a back-fill for a new method which is expected to go into PHP_CodeSniffer 3.5.0. Once that method has been merged into PHPCS, this one should be moved to the PHPCSHelper.php file.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

Pointer to the first text string token of a multi-line text string or to a Nowdoc/Heredoc opener.

$stripQuotes : bool = true

Optional. Whether to strip text delimiter quotes off the resulting text string. Defaults to true.

Tags
since
9.3.0
codeCoverageIgnore

Method as pulled upstream is accompanied by unit tests.

throws
PHP_CodeSniffer_Exception

If the specified position is not a valid text string token or if the token is not the first text string token.

Return values
string

getDeclaredNamespaceName()

Get the complete namespace name for a namespace declaration.

public getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, int|bool $stackPtr) : string|false

For hierarchical namespaces, the name will be composed of several tokens, i.e. MyProject\Sub\Level which will be returned together as one string.

Parameters
$phpcsFile : PHP_CodeSniffer_File

Instance of phpcsFile.

$stackPtr : int|bool

The position of a T_NAMESPACE token.

Tags
since
7.0.3
Return values
string|false

Namespace name or false if not a namespace declaration. Namespace name can be an empty string for global namespace declaration.

getFQClassNameFromDoubleColonToken()

Returns the class name for the static usage of a class.

public getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : string

This can be a call to a method, the use of a property or constant.

Returns an empty string if the class name could not be reliably inferred.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of a T_NEW token.

Tags
since
7.0.3
Return values
string

getFQClassNameFromNewToken()

Returns the fully qualified class name for a new class instantiation.

public getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : string

Returns an empty string if the class name could not be reliably inferred.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of a T_NEW token.

Tags
since
7.0.3
Return values
string

getFQExtendedClassName()

Returns the fully qualified name of the class that the specified class extends.

public getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : string

Returns an empty string if the class does not extend another class or if the class name could not be reliably inferred.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of a T_CLASS token.

Tags
since
7.0.3
Return values
string

getFQName()

Get the Fully Qualified name for a class/function/constant etc.

public getFQName(PHP_CodeSniffer_File $phpcsFile, int $stackPtr, string $name) : string

Checks if a class/function/constant name is already fully qualified and if not, enrich it with the relevant namespace information.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the token.

$name : string

The class / function / constant name.

Tags
since
7.0.3
Return values
string

getFunctionCallParameter()

Get information on a specific parameter passed to a function call.

public getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, int $stackPtr, int $paramOffset) : array<string|int, mixed>|false

Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. If passed a T_STRING which is not a function call, the behaviour is unreliable.

Will return a array with the start token pointer, end token pointer and the raw value of the parameter at a specific offset. If the specified parameter is not found, will return false.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the function call token.

$paramOffset : int

The 1-based index position of the parameter to retrieve.

Tags
since
7.0.5
Return values
array<string|int, mixed>|false

getFunctionCallParameterCount()

Count the number of parameters a function call has been passed.

public getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : int

Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. If passed a T_STRING which is not a function call, the behaviour is unreliable.

Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it will return the number of values in the array.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the function call token.

Tags
link
https://github.com/PHPCompatibility/PHPCompatibility/issues/111
link
https://github.com/PHPCompatibility/PHPCompatibility/issues/114
link
https://github.com/PHPCompatibility/PHPCompatibility/issues/151
since
7.0.3
Return values
int

getFunctionCallParameters()

Get information on all parameters passed to a function call.

public getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : array<string|int, mixed>

Expects to be passed the T_STRING or T_VARIABLE stack pointer for the function call. If passed a T_STRING which is not a function call, the behaviour is unreliable.

Will return an multi-dimentional array with the start token pointer, end token pointer and raw parameter value for all parameters. Index will be 1-based. If no parameters are found, will return an empty array.

Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it will tokenize the values / key/value pairs contained in the array call.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the function call token.

Tags
since
7.0.5

Split off from the getFunctionCallParameterCount() method.

Return values
array<string|int, mixed>

getHashAlgorithmParameter()

Get the hash algorithm name from the parameter in a hash function call.

public getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : string|false
Parameters
$phpcsFile : PHP_CodeSniffer_File

Instance of phpcsFile.

$stackPtr : int

The position of the T_STRING function token.

Tags
since
7.0.7

Logic was originally contained in the RemovedHashAlgorithms sniff.

Return values
string|false

The algorithm name without quotes if this was a relevant hash function call or false if it was not.

getReturnTypeHintName()

Get the complete return type declaration for a given function.

public getReturnTypeHintName(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : string

Cross-version compatible way to retrieve the complete return type declaration.

For a classname-based return type, PHPCS, as well as the Sniff::getReturnTypeHintToken() method will mark the classname as the return type token. This method will find preceeding namespaces and namespace separators and will return a string containing the qualified return type declaration.

Expects to be passed a T_RETURN_TYPE token or the return value from a call to the Sniff::getReturnTypeHintToken() method.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the return type token.

Tags
since
8.2.0
Return values
string

The name of the return type token.

getReturnTypeHintToken()

Get the stack pointer for a return type token for a given function.

public getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : int|false

Compatible layer for older PHPCS versions which don't recognize return type hints correctly.

Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the token.

Tags
since
7.1.2
Return values
int|false

Stack pointer to the return type token or false if no return type was found or the passed token was not of the correct type.

getTypeHintsFromFunctionDeclaration()

Get an array of just the type hints from a function declaration.

public getTypeHintsFromFunctionDeclaration(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : array<string|int, mixed>

Expects to be passed T_FUNCTION or T_CLOSURE token.

Strips potential nullable indicator and potential global namespace indicator from the type hints before returning them.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the token.

Tags
since
7.1.4
Return values
array<string|int, mixed>

Array with type hints or an empty array if

  • the function does not have any parameters
  • no type hints were found
  • or the passed token was not of the correct type.

inClassScope()

Verify whether a token is within a class scope.

public inClassScope(PHP_CodeSniffer_File $phpcsFile, int $stackPtr[, bool $strict = true ]) : bool
Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the token.

$strict : bool = true

Whether to strictly check for the T_CLASS scope or also accept interfaces and traits as scope.

Tags
since
7.0.3
Return values
bool

True if within class scope, false otherwise.

isClassConstant()

Check whether a T_CONST token is a class constant declaration.

public isClassConstant(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool
Parameters
$phpcsFile : PHP_CodeSniffer_File

Instance of phpcsFile.

$stackPtr : int

The position in the stack of the T_CONST token to verify.

Tags
since
7.1.4
Return values
bool

isClassProperty()

Check whether a T_VARIABLE token is a class property declaration.

public isClassProperty(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool

Compatibility layer for PHPCS cross-version compatibility as PHPCS 2.4.0 - 2.7.1 does not have good enough support for anonymous classes. Along the same lines, thegetMemberProperties() method does not support the var prefix.

Parameters
$phpcsFile : PHP_CodeSniffer_File

Instance of phpcsFile.

$stackPtr : int

The position in the stack of the T_VARIABLE token to verify.

Tags
since
7.1.4
Return values
bool

isNamespaced()

Is the class/function/constant name namespaced or global ?

public isNamespaced(string $FQName) : bool
Parameters
$FQName : string

Fully Qualified name of a class, function etc. I.e. should always start with a \.

Tags
since
7.0.3
throws
PHP_CodeSniffer_Exception

If the name in the passed parameter is not fully qualified.

Return values
bool

True if namespaced, false if global.

isNegativeNumber()

Determine whether the tokens between $start and $end together form a negative number as recognized by PHP.

public isNegativeNumber(PHP_CodeSniffer_File $phpcsFile, int $start, int $end[, bool $allowFloats = false ]) : bool

The outcome of this function is reliable for true, false should be regarded as "undetermined".

Note: Zero is not regarded as a negative number.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$start : int

Start of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$end : int

End of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$allowFloats : bool = false

Whether to only consider integers, or also floats.

Tags
since
8.2.0
Return values
bool

True if PHP would evaluate the snippet as a negative number. False if not or if it could not be reliably determined (variable or calculations and such).

isPositiveNumber()

Determine whether the tokens between $start and $end together form a positive number as recognized by PHP.

public isPositiveNumber(PHP_CodeSniffer_File $phpcsFile, int $start, int $end[, bool $allowFloats = false ]) : bool

The outcome of this function is reliable for true, false should be regarded as "undetermined".

Note: Zero is not regarded as a positive number.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$start : int

Start of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$end : int

End of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$allowFloats : bool = false

Whether to only consider integers, or also floats.

Tags
since
8.2.0
Return values
bool

True if PHP would evaluate the snippet as a positive number. False if not or if it could not be reliably determined (variable or calculations and such).

isShortList()

Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a list() construct.

public isShortList(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool

Note: A variety of PHPCS versions have bugs in the tokenizing of short arrays. In that case, the tokens are identified as T_OPEN/CLOSE_SQUARE_BRACKET.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the function call token.

Tags
since
8.2.0
Return values
bool

isShortTernary()

Determine whether a ternary is a short ternary, i.e. without "middle".

public isShortTernary(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool

N.B.: This is a back-fill for a new method which is expected to go into PHP_CodeSniffer 3.5.0. Once that method has been merged into PHPCS, this one should be moved to the PHPCSHelper.php file.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the ternary operator in the stack.

Tags
since
9.2.0
codeCoverageIgnore

Method as pulled upstream is accompanied by unit tests.

Return values
bool

True if short ternary, or false otherwise.

isUnaryPlusMinus()

Determine whether a T_MINUS/T_PLUS token is a unary operator.

public static isUnaryPlusMinus(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool

N.B.: This is a back-fill for a new method which is expected to go into PHP_CodeSniffer 3.5.0. Once that method has been merged into PHPCS, this one should be moved to the PHPCSHelper.php file.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the plus/minus token.

Tags
since
9.2.0
codeCoverageIgnore

Method as pulled upstream is accompanied by unit tests.

Return values
bool

True if the token passed is a unary operator. False otherwise or if the token is not a T_PLUS/T_MINUS token.

isUseOfGlobalConstant()

Determine whether an arbitrary T_STRING token is the use of a global constant.

public isUseOfGlobalConstant(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : bool
Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the T_STRING token.

Tags
since
8.1.0
Return values
bool

isVariable()

Determine whether the tokens between $start and $end could together represent a variable.

public isVariable(PHP_CodeSniffer_File $phpcsFile, int $start, int $end, int $targetNestingLevel) : bool
Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$start : int

Starting point stack pointer. Inclusive. I.e. this token should be taken into account.

$end : int

End point stack pointer. Exclusive. I.e. this token should not be taken into account.

$targetNestingLevel : int

The nesting level the variable should be at.

Tags
since
9.0.0
Return values
bool

process()

Processes this test, when one of its tokens is encountered.

public process(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : int|void
Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the current token in the stack passed in $tokens.

Tags
since
8.2.0
Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

processNoParameters()

Process the function if no parameters were found.

public processNoParameters(PHP_CodeSniffer_File $phpcsFile, int $stackPtr, string $functionName) : int|void

Defaults to doing nothing. Can be overloaded in child classes to handle functions were parameters are expected, but none found.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the current token in the stack.

$functionName : string

The token content (function name) which was matched.

Tags
since
8.2.0
Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

processParameters()

Process the parameters of a matched function.

public processParameters(PHP_CodeSniffer_File $phpcsFile, int $stackPtr, string $functionName, array<string|int, mixed> $parameters) : int|void
Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the current token in the stack.

$functionName : string

The token content (function name) which was matched.

$parameters : array<string|int, mixed>

Array with information about the parameters.

Tags
since
9.0.0
Return values
int|void

Integer stack pointer to skip forward or void to continue normal file processing.

register()

Returns an array of tokens this test wants to listen for.

public register() : array<string|int, mixed>
Tags
since
8.2.0
Return values
array<string|int, mixed>

stringToErrorCode()

Convert an arbitrary string to an alphanumeric string with underscores.

public stringToErrorCode(string $baseString) : string

Pre-empt issues with arbitrary strings being used as error codes in XML and PHP.

Parameters
$baseString : string

Arbitrary string.

Tags
since
7.1.0
Return values
string

stripQuotes()

Strip quotes surrounding an arbitrary string.

public stripQuotes(string $string) : string

Intended for use with the contents of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING.

Parameters
$string : string

The raw string.

Tags
since
7.0.6
Return values
string

String without quotes around it.

stripVariables()

Strip variables from an arbitrary double quoted string.

public stripVariables(string $string) : string

Intended for use with the contents of a T_DOUBLE_QUOTED_STRING.

Parameters
$string : string

The raw string.

Tags
since
7.1.2
Return values
string

String without variables in it.

supportsAbove()

Check whether a specific PHP version is equal to or higher than the maximum supported PHP version as provided by the user in `testVersion`.

public supportsAbove(string $phpVersion) : bool

Should be used when sniffing for old PHP features (deprecated/removed).

Parameters
$phpVersion : string

A PHP version number in 'major.minor' format.

Tags
since
5.6
Return values
bool

True if testVersion has not been provided or if the PHP version is equal to or higher than the highest supported PHP version in testVersion. False otherwise.

supportsBelow()

Check whether a specific PHP version is equal to or lower than the minimum supported PHP version as provided by the user in `testVersion`.

public supportsBelow(string $phpVersion) : bool

Should be used when sniffing for new PHP features.

Parameters
$phpVersion : string

A PHP version number in 'major.minor' format.

Tags
since
5.6
Return values
bool

True if the PHP version is equal to or lower than the lowest supported PHP version in testVersion. False otherwise or if no testVersion is provided.

tokenHasScope()

Verify whether a token is within a scoped condition.

public tokenHasScope(PHP_CodeSniffer_File $phpcsFile, int $stackPtr[, array<string|int, mixed>|int $validScopes = null ]) : bool

If the optional $validScopes parameter has been passed, the function will check that the token has at least one condition which is of a type defined in $validScopes.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$stackPtr : int

The position of the token.

$validScopes : array<string|int, mixed>|int = null

Optional. Array of valid scopes or int value of a valid scope. Pass the T_.. constant(s) for the desired scope to this parameter.

Tags
since
7.0.5

Largely split off from the inClassScope() method.

Return values
bool

Without the optional $scopeTypes: True if within a scope, false otherwise. If the $scopeTypes are set: True if one of the conditions is a valid scope, false otherwise.

bowOutEarly()

Do a version check to determine if this sniff needs to run at all.

protected bowOutEarly() : bool
Tags
since
9.0.0
Return values
bool

isNumber()

Determine whether the tokens between $start and $end together form a number as recognized by PHP.

protected isNumber(PHP_CodeSniffer_File $phpcsFile, int $start, int $end[, bool $allowFloats = false ]) : int|float|bool

The outcome of this function is reliable for "true-ish" values, false should be regarded as "undetermined".

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$start : int

Start of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$end : int

End of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$allowFloats : bool = false

Whether to only consider integers, or also floats.

Tags
link

Mainly intended for examining variable assignments, function call parameters, array values where the start and end of the snippet to examine is very clear.

since
8.2.0
Return values
int|float|bool

The number found if PHP would evaluate the snippet as a number. The return type will be int if $allowFloats is false, if $allowFloats is true, the return type will be float. False will be returned when the snippet does not evaluate to a number or if it could not be reliably determined (variable or calculations and such).

isNumericCalculation()

Determine whether the tokens between $start and $end together form a numberic calculation as recognized by PHP.

protected isNumericCalculation(PHP_CodeSniffer_File $phpcsFile, int $start, int $end) : bool

The outcome of this function is reliable for true, false should be regarded as "undetermined".

Mainly intended for examining variable assignments, function call parameters, array values where the start and end of the snippet to examine is very clear.

Parameters
$phpcsFile : PHP_CodeSniffer_File

The file being scanned.

$start : int

Start of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

$end : int

End of the snippet (inclusive), i.e. this token will be examined as part of the snippet.

Tags
since
9.0.0
Return values
bool

validDirectScope()

Check whether the direct wrapping scope of a token is within a limited set of acceptable tokens.

protected validDirectScope(PHP_CodeSniffer_File $phpcsFile, int $stackPtr, array<string|int, mixed> $validScopes) : int|bool

Used to check, for instance, if a T_CONST is a class constant.

Parameters
$phpcsFile : PHP_CodeSniffer_File

Instance of phpcsFile.

$stackPtr : int

The position in the stack of the token to verify.

$validScopes : array<string|int, mixed>

Array of token types. Keys should be the token types in string format to allow for newer token types. Value is irrelevant.

Tags
since
7.1.4
Return values
int|bool

StackPtr to the scope if valid, false otherwise.

getTestVersion()

Get the testVersion configuration variable.

private getTestVersion() : array<string|int, mixed>

The testVersion configuration variable may be in any of the following formats:

  1. Omitted/empty, in which case no version is specified. This effectively disables all the checks for new PHP features provided by this standard.
  2. A single PHP version number, e.g. "5.4" in which case the standard checks that the code will run on that version of PHP (no deprecated features or newer features being used).
  3. A range, e.g. "5.0-5.5", in which case the standard checks the code will run on all PHP versions in that range, and that it doesn't use any features that were deprecated by the final version in the list, or which were not available for the first version in the list. We accept ranges where one of the components is missing, e.g. "-5.6" means all versions up to PHP 5.6, and "7.0-" means all versions above PHP 7.0. PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" would be treated as invalid, and ignored.
Tags
since
7.0.0
since
7.1.3

Now allows for partial ranges such as 5.2-.

throws
PHP_CodeSniffer_Exception

If testVersion is invalid.

Return values
array<string|int, mixed>

$arrTestVersions will hold an array containing min/max version of PHP that we are checking against (see above). If only a single version number is specified, then this is used as both the min and max.


        
On this page

Search results