Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Utils\Numbers class #62

Merged
merged 1 commit into from Jan 23, 2020
Merged

New Utils\Numbers class #62

merged 1 commit into from Jan 23, 2020

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 23, 2020

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.

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.
@jrfnl jrfnl added this to the 1.0.0 milestone Jan 23, 2020
@jrfnl jrfnl merged commit d5ae800 into develop Jan 23, 2020
@jrfnl jrfnl deleted the feature/new-numbers-class branch January 23, 2020 05:17
@jrfnl jrfnl modified the milestones: 1.0.0, 1.0.0-alpha1 May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant