Numbers
in package
Utility functions for working with integer/float tokens.
PHP 7.4 introduced numeric literal separators. PHPCS backfills this since PHPCS 3.5.3/4. PHP 8.1 introduced an explicit octal notation. This is backfilled in PHPCS since PHPCS 3.7.0.
While there are currently no unsupported numeric syntaxes, the methods in this class
can still be useful for external standards which need to examine the
contents of T_LNUMBER
or T_DNUMBER
tokens.
Tags
Table of Contents
Constants
- REGEX_BINARY_INT = '`^0b[0-1]+$`iD'
- Regex to determine whether the contents of an arbitrary string represents a binary integer.
- REGEX_DECIMAL_INT = '`^(?:0|[1-9][0-9]*)$`D'
- Regex to determine whether the contents of an arbitrary string represents a decimal integer.
- REGEX_FLOAT = '` ^(?: (?: (?: (?P<LNUM>[0-9]+) | (?P<DNUM>([0-9]*\\.(?P>LNUM)|(?P>LNUM)\\.[0-9]*)) ) [e][+-]?(?P>LNUM) ) | (?P>DNUM) | (?:0|[1-9][0-9]*) )$ `ixD'
- Regex to determine whether the contents of an arbitrary string represents a float.
- REGEX_HEX_INT = '`^0x[0-9A-F]+$`iD'
- Regex to determine whether the contents of an arbitrary string represents a hexidecimal integer.
- REGEX_OCTAL_INT = '`^0[o]?[0-7]+$`iD'
- Regex to determine whether the contents of an arbitrary string represents an octal integer.
Methods
- getCompleteNumber() : array<string, string|int>
- Retrieve information about a number token.
- getDecimalValue() : string|false
- Get the decimal number value of a numeric string.
- isBinaryInt() : bool
- Verify whether the contents of an arbitrary string represents a binary integer.
- isDecimalInt() : bool
- Verify whether the contents of an arbitrary string represents a decimal integer.
- isFloat() : bool
- Verify whether the contents of an arbitrary string represents a floating point number.
- isHexidecimalInt() : bool
- Verify whether the contents of an arbitrary string represents a hexidecimal integer.
- isOctalInt() : bool
- Verify whether the contents of an arbitrary string represents an octal integer.
Constants
REGEX_BINARY_INT
Regex to determine whether the contents of an arbitrary string represents a binary integer.
public
string
REGEX_BINARY_INT
= '`^0b[0-1]+$`iD'
Tags
REGEX_DECIMAL_INT
Regex to determine whether the contents of an arbitrary string represents a decimal integer.
public
string
REGEX_DECIMAL_INT
= '`^(?:0|[1-9][0-9]*)$`D'
Tags
REGEX_FLOAT
Regex to determine whether the contents of an arbitrary string represents a float.
public
string
REGEX_FLOAT
= '`
^(?:
(?:
(?:
(?P<LNUM>[0-9]+)
|
(?P<DNUM>([0-9]*\\.(?P>LNUM)|(?P>LNUM)\\.[0-9]*))
)
[e][+-]?(?P>LNUM)
)
|
(?P>DNUM)
|
(?:0|[1-9][0-9]*)
)$
`ixD'
Tags
REGEX_HEX_INT
Regex to determine whether the contents of an arbitrary string represents a hexidecimal integer.
public
string
REGEX_HEX_INT
= '`^0x[0-9A-F]+$`iD'
Tags
REGEX_OCTAL_INT
Regex to determine whether the contents of an arbitrary string represents an octal integer.
public
string
REGEX_OCTAL_INT
= '`^0[o]?[0-7]+$`iD'
Tags
Methods
getCompleteNumber()
Retrieve information about a number token.
public
static getCompleteNumber(File $phpcsFile, int $stackPtr) : array<string, string|int>
Helper function to deal with numeric literals, potentially with underscore separators and/or explicit octal notation.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of a T_LNUMBER or T_DNUMBER token.
Tags
Return values
array<string, string|int> —An array with information about the number. The format of the array return value is:
array(
'orig_content' => string, // The original content of the token(s);
'content' => string, // The content, underscore(s) removed;
'code' => int, // The token code of the number, either
// T_LNUMBER or T_DNUMBER.
'type' => string, // The token type, either 'T_LNUMBER'
// or 'T_DNUMBER'.
'decimal' => string, // The decimal value of the number;
'last_token' => int, // The stackPtr to the last token which was
// part of the number.
// At this time, this will be always be the original
// stackPtr. This may change in the future if
// new numeric syntaxes would be added to PHP.
)
getDecimalValue()
Get the decimal number value of a numeric string.
public
static getDecimalValue(string $textString) : string|false
Takes PHP 7.4 numeric literal separators and explicit octal literals in numbers into account.
Parameters
- $textString : string
-
Arbitrary text string. This text string should be the (combined) token content of one or more tokens which together represent a number in PHP.
Tags
Return values
string|false —Decimal number as a string or FALSE
if the passed parameter
was not a numeric string.
Note: floating point numbers with exponent will not be expanded, but returned as-is.
isBinaryInt()
Verify whether the contents of an arbitrary string represents a binary integer.
public
static isBinaryInt(string $textString) : bool
Takes PHP 7.4 numeric literal separators in numbers into account.
Parameters
- $textString : string
-
Arbitrary string.
Tags
Return values
boolisDecimalInt()
Verify whether the contents of an arbitrary string represents a decimal integer.
public
static isDecimalInt(string $textString) : bool
Takes PHP 7.4 numeric literal separators in numbers into account.
Parameters
- $textString : string
-
Arbitrary string.
Tags
Return values
boolisFloat()
Verify whether the contents of an arbitrary string represents a floating point number.
public
static isFloat(string $textString) : bool
Takes PHP 7.4 numeric literal separators in numbers into account.
Parameters
- $textString : string
-
Arbitrary string.
Tags
Return values
boolisHexidecimalInt()
Verify whether the contents of an arbitrary string represents a hexidecimal integer.
public
static isHexidecimalInt(string $textString) : bool
Takes PHP 7.4 numeric literal separators in numbers into account.
Parameters
- $textString : string
-
Arbitrary string.
Tags
Return values
boolisOctalInt()
Verify whether the contents of an arbitrary string represents an octal integer.
public
static isOctalInt(string $textString) : bool
Takes PHP 7.4 numeric literal separators and explicit octal literals in numbers into account.
Parameters
- $textString : string
-
Arbitrary string.