Commit 695d330
committed
✨ New Utils\Numbers class
PHP 7.4 introduced numeric literal separators which break number tokenization in older PHP versions.
PHPCS backfills this since PHPCS 3.5.3, but that backfill implementation is broken in such a bad way that PHPCS 3.5.3 can not be supported.
A fix for the backfill has been merged and will be included in PHPCS 3.5.4.
Having said that, if an external standard intends to support PHPCS < 3.5.3 and/or PHP < 7.4, working with number tokens has become a challenge, as without the backfill any number token is potentially broken
The functions in this class have been put in place to ease that pain and it is *strongly* recommended to always use these functions when sniffing for and/or examining the contents of `T_LNUMBER` or `T_DNUMBER` tokens.
Note: sniffing for `T_STRING` tokens is also affected by this PHP 7.4 change, but that is not addressed here.
The `Numbers` class introduces two new methods:
* `getCompleteNumber()` - Helper function to deal with numeric literals, potentially with underscore separators. Returns an array with information about the number.
* `getDecimalValue()` - Get the decimal number value of a numeric string. Returns the value as a string or false if the passed parameter was not a (numeric) string.
* `isDecimalInt()` - Check if an arbitrary string represents a decimal integer. Returns boolean.
* `isHexidecimalInt()` - Check if an arbitrary string represents a hexidecimal integer. Returns boolean.
* `isBinaryInt()` - Check if an arbitrary string represents a binary integer. Returns boolean.
* `isOctalInt()` - Check if an arbitrary string represents a octal integer. Returns boolean.
* `isFloat()` - Check if an arbitrary string represents a floating point number. Returns boolean.
Includes dedicated unit tests for these methods.1 parent e72a83a commit 695d330
File tree
6 files changed
+2107
-0
lines changed- PHPCSUtils/Utils
- Tests/Utils/Numbers
6 files changed
+2107
-0
lines changed
0 commit comments