Skip to content

Commit 1757e77

Browse files
committed
Merge branch 'feature/fix-php-7.4-numeric-separators-backfill' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents be86839 + bb9216e commit 1757e77

File tree

3 files changed

+366
-21
lines changed

3 files changed

+366
-21
lines changed

src/Tokenizers/PHP.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -992,20 +992,24 @@ protected function tokenize($string)
992992

993993
if ($tokens[$i][0] === T_LNUMBER
994994
|| $tokens[$i][0] === T_DNUMBER
995-
|| ($tokens[$i][0] === T_STRING
996-
&& $tokens[$i][1][0] === '_')
997995
) {
998996
$newContent .= $tokens[$i][1];
997+
continue;
998+
}
999999

1000-
// Any T_DNUMBER token needs to make the
1001-
// new number a T_DNUMBER as well.
1002-
if ($tokens[$i][0] === T_DNUMBER) {
1003-
$newType = T_DNUMBER;
1004-
}
1000+
if ($tokens[$i][0] === T_STRING
1001+
&& $tokens[$i][1][0] === '_'
1002+
&& ((strpos($newContent, '0x') === 0
1003+
&& preg_match('`^((?<!\.)_[0-9A-F][0-9A-F\.]*)+$`iD', $tokens[$i][1]) === 1)
1004+
|| (strpos($newContent, '0x') !== 0
1005+
&& substr($newContent, -1) !== '.'
1006+
&& substr(strtolower($newContent), -1) !== 'e'
1007+
&& preg_match('`^(?:(?<![\.e])_[0-9][0-9e\.]*)+$`iD', $tokens[$i][1]) === 1))
1008+
) {
1009+
$newContent .= $tokens[$i][1];
10051010

10061011
// Support floats.
1007-
if ($tokens[$i][0] === T_STRING
1008-
&& substr(strtolower($tokens[$i][1]), -1) === 'e'
1012+
if (substr(strtolower($tokens[$i][1]), -1) === 'e'
10091013
&& ($tokens[($i + 1)] === '-'
10101014
|| $tokens[($i + 1)] === '+')
10111015
) {
@@ -1019,9 +1023,21 @@ protected function tokenize($string)
10191023
break;
10201024
}//end for
10211025

1026+
if ($newType === T_LNUMBER
1027+
&& ((stripos($newContent, '0x') === 0 && hexdec(str_replace('_', '', $newContent)) > PHP_INT_MAX)
1028+
|| (stripos($newContent, '0b') === 0 && bindec(str_replace('_', '', $newContent)) > PHP_INT_MAX)
1029+
|| (stripos($newContent, '0x') !== 0
1030+
&& stripos($newContent, 'e') !== false || strpos($newContent, '.') !== false)
1031+
|| (strpos($newContent, '0') === 0 && stripos($newContent, '0x') !== 0
1032+
&& stripos($newContent, '0b') !== 0 && octdec(str_replace('_', '', $newContent)) > PHP_INT_MAX)
1033+
|| (strpos($newContent, '0') !== 0 && str_replace('_', '', $newContent) > PHP_INT_MAX))
1034+
) {
1035+
$newType = T_DNUMBER;
1036+
}
1037+
10221038
$newToken = [];
10231039
$newToken['code'] = $newType;
1024-
$newToken['type'] = Util\Tokens::tokenName($token[0]);
1040+
$newToken['type'] = Util\Tokens::tokenName($newType);
10251041
$newToken['content'] = $newContent;
10261042
$finalTokens[$newStackPtr] = $newToken;
10271043

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,82 @@
11
<?php
22

3+
/*
4+
* Numbers with PHP 7.4 underscore separators.
5+
*/
6+
37
/* testSimpleLNumber */
48
$foo = 1_000_000_000;
59

610
/* testSimpleDNumber */
7-
$foo = 107_925_284.88;
11+
$foo = 107_925_284.88;
812

913
/* testFloat */
1014
$foo = 6.674_083e-11;
1115

1216
/* testFloat2 */
1317
$foo = 6.674_083e+11;
1418

19+
/* testFloat3 */
20+
$foo = 1_2.3_4e1_23;
21+
1522
/* testHex */
1623
$foo = 0xCAFE_F00D;
1724

1825
/* testHexMultiple */
1926
$foo = 0x42_72_6F_77_6E;
2027

28+
/* testHexInt */
29+
$foo = 0x42_72_6F;
30+
2131
/* testBinary */
2232
$foo = 0b0101_1111;
2333

2434
/* testOctal */
2535
$foo = 0137_041;
36+
37+
/* testIntMoreThanMax */
38+
$foo = 10_223_372_036_854_775_807;
39+
40+
/*
41+
* Invalid use of numeric separators. These should not be touched by the backfill.
42+
*/
43+
44+
/* testInvalid1 */
45+
$a = 100_; // trailing
46+
47+
/* testInvalid2 */
48+
$a = 1__1; // next to underscore
49+
50+
/* testInvalid3 */
51+
$a = 1_.0; // next to decimal point
52+
53+
/* testInvalid4 */
54+
$a = 1._0; // next to decimal point
55+
56+
/* testInvalid5 */
57+
$a = 0x_123; // next to x
58+
59+
/* testInvalid6 */
60+
$a = 0b_101; // next to b
61+
62+
/* testInvalid7 */
63+
$a = 1_e2; // next to e
64+
65+
/* testInvalid8 */
66+
$a = 1e_2; // next to e
67+
68+
/* testInvalid9 */
69+
$testValue = 107_925_284 .88;
70+
71+
/* testInvalid10 */
72+
$testValue = 107_925_284/*comment*/.88;
73+
74+
/*
75+
* Ensure that legitimate calculations are not touched by the backfill.
76+
*/
77+
78+
/* testCalc1 */
79+
$a = 667_083 - 11; // Calculation.
80+
81+
/* test Calc2 */
82+
$a = 6.674_08e3 + 11; // Calculation.

0 commit comments

Comments
 (0)