NewUniformVariableSyntaxSniff
extends Sniff
in package
The interpretation of variable variables has changed in PHP 7.0.
PHP version 7.0
Tags
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).
- $superglobals : array<string|int, mixed>
- List of superglobals as an array of strings.
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() : void
- Processes this test, when one of its tokens is encountered.
- 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.
- 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
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
$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
$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
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
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
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
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
Return values
boolgetCompleteTextString()
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
Return values
stringgetDeclaredNamespaceName()
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
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
Return values
stringgetFQClassNameFromNewToken()
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
Return values
stringgetFQExtendedClassName()
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
Return values
stringgetFQName()
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
Return values
stringgetFunctionCallParameter()
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
Return values
array<string|int, mixed>|falsegetFunctionCallParameterCount()
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
Return values
intgetFunctionCallParameters()
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
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
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
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
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
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
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
Return values
boolisClassProperty()
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
Return values
boolisNamespaced()
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
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
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
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
Return values
boolisShortTernary()
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
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
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
Return values
boolisVariable()
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
Return values
boolprocess()
Processes this test, when one of its tokens is encountered.
public
process(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : 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
register()
Returns an array of tokens this test wants to listen for.
public
register() : array<string|int, mixed>
Tags
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
Return values
stringstripQuotes()
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
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
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
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
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
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.
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
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
Return values
boolvalidDirectScope()
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
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:
- Omitted/empty, in which case no version is specified. This effectively disables all the checks for new PHP features provided by this standard.
- 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).
- 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
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.