diff --git a/src/Parser/ConstExprParser.php b/src/Parser/ConstExprParser.php index dd08cfd4..45021b1c 100644 --- a/src/Parser/ConstExprParser.php +++ b/src/Parser/ConstExprParser.php @@ -55,7 +55,7 @@ public function parse(TokenIterator $tokens): Ast\ConstExpr\ConstExprNode return $this->enrichWithAttributes( $tokens, - new Ast\ConstExpr\ConstExprIntegerNode(str_replace('_', '', $value)), + new Ast\ConstExpr\ConstExprIntegerNode(IntegerLiteralNormalizer::normalizeToDecimal($value)), $startLine, $startIndex, ); diff --git a/src/Parser/IntegerLiteralNormalizer.php b/src/Parser/IntegerLiteralNormalizer.php new file mode 100644 index 00000000..aa783ba5 --- /dev/null +++ b/src/Parser/IntegerLiteralNormalizer.php @@ -0,0 +1,79 @@ + 0) { + $decimalDigits[] = $carry % 10; + $carry = intdiv($carry, 10); + } + } + $decimal = ltrim(implode('', array_reverse($decimalDigits)), '0'); + } + + if ($decimal === '') { + return '0'; + } + + return $sign . $decimal; + } + +} diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index 581a8b1e..e0c0fa16 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -15,7 +15,6 @@ use function array_key_exists; use function count; use function rtrim; -use function str_replace; use function trim; /** @@ -777,7 +776,7 @@ private function parseDoctrineArrayKey(TokenIterator $tokens) $startIndex = $tokens->currentTokenIndex(); if ($tokens->isCurrentTokenType(Lexer::TOKEN_INTEGER)) { - $key = new Ast\ConstExpr\ConstExprIntegerNode(str_replace('_', '', $tokens->currentTokenValue())); + $key = new Ast\ConstExpr\ConstExprIntegerNode(IntegerLiteralNormalizer::normalizeToDecimal($tokens->currentTokenValue())); $tokens->next(); } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_DOCTRINE_ANNOTATION_STRING)) { diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 348c56d1..e3990653 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -8,7 +8,6 @@ use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\PhpDocParser\ParserConfig; use function in_array; -use function str_replace; use function strlen; use function strpos; use function substr_compare; @@ -971,7 +970,7 @@ private function parseArrayShapeKey(TokenIterator $tokens) $startLine = $tokens->currentTokenLine(); if ($tokens->isCurrentTokenType(Lexer::TOKEN_INTEGER)) { - $key = new Ast\ConstExpr\ConstExprIntegerNode(str_replace('_', '', $tokens->currentTokenValue())); + $key = new Ast\ConstExpr\ConstExprIntegerNode(IntegerLiteralNormalizer::normalizeToDecimal($tokens->currentTokenValue())); $tokens->next(); } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_SINGLE_QUOTED_STRING)) { diff --git a/tests/PHPStan/Parser/ConstExprParserTest.php b/tests/PHPStan/Parser/ConstExprParserTest.php index 1fb0f30a..f569d9fe 100644 --- a/tests/PHPStan/Parser/ConstExprParserTest.php +++ b/tests/PHPStan/Parser/ConstExprParserTest.php @@ -147,7 +147,7 @@ public function provideIntegerNodeParseData(): Iterator yield [ '+123', - new ConstExprIntegerNode('+123'), + new ConstExprIntegerNode('123'), ]; yield [ @@ -157,27 +157,27 @@ public function provideIntegerNodeParseData(): Iterator yield [ '0b0110101', - new ConstExprIntegerNode('0b0110101'), + new ConstExprIntegerNode('53'), ]; yield [ '0o777', - new ConstExprIntegerNode('0o777'), + new ConstExprIntegerNode('511'), ]; yield [ '0x7Fb4', - new ConstExprIntegerNode('0x7Fb4'), + new ConstExprIntegerNode('32692'), ]; yield [ '-0O777', - new ConstExprIntegerNode('-0O777'), + new ConstExprIntegerNode('-511'), ]; yield [ '-0X7Fb4', - new ConstExprIntegerNode('-0X7Fb4'), + new ConstExprIntegerNode('-32692'), ]; yield [ @@ -187,18 +187,33 @@ public function provideIntegerNodeParseData(): Iterator yield [ '0b01_01_01', - new ConstExprIntegerNode('0b010101'), + new ConstExprIntegerNode('21'), ]; yield [ '-0X7_Fb_4', - new ConstExprIntegerNode('-0X7Fb4'), + new ConstExprIntegerNode('-32692'), ]; yield [ '18_446_744_073_709_551_616', // 64-bit unsigned long + 1, larger than PHP_INT_MAX new ConstExprIntegerNode('18446744073709551616'), ]; + + yield [ + '017', // legacy octal, as in PHP + new ConstExprIntegerNode('15'), + ]; + + yield [ + '0xFFFF_FFFF_FFFF_FFFF', // larger than PHP_INT_MAX + new ConstExprIntegerNode('18446744073709551615'), + ]; + + yield [ + '-0b0', + new ConstExprIntegerNode('0'), + ]; } public function provideFloatNodeParseData(): Iterator diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 1f97651d..47362f8a 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -1932,9 +1932,9 @@ public function provideParseData(): array [ '+0x10_20|+8e+2 | -0b11', new UnionTypeNode([ - new ConstTypeNode(new ConstExprIntegerNode('+0x1020')), + new ConstTypeNode(new ConstExprIntegerNode('4128')), new ConstTypeNode(new ConstExprFloatNode('+8e+2')), - new ConstTypeNode(new ConstExprIntegerNode('-0b11')), + new ConstTypeNode(new ConstExprIntegerNode('-3')), ]), ], [