Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Parser/ConstExprParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
79 changes: 79 additions & 0 deletions src/Parser/IntegerLiteralNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Parser;

use function array_reverse;
use function count;
use function implode;
use function intdiv;
use function intval;
use function ltrim;
use function str_replace;
use function str_split;
use function strlen;
use function strspn;
use function strtolower;
use function substr;

class IntegerLiteralNormalizer
{

/**
* Converts an integer literal in any base, such as 0x1F, 0b101, 0o17 or 017,
* to a decimal number of any size, such as 31, so that (int) casts read it correctly.
*/
public static function normalizeToDecimal(string $literal): string
{
$literal = str_replace('_', '', $literal);
$sign = '';
if ($literal !== '' && ($literal[0] === '-' || $literal[0] === '+')) {
$sign = $literal[0] === '-' ? '-' : '';
$literal = substr($literal, 1);
}

$prefix = strtolower(substr($literal, 0, 2));
if ($prefix === '0x') {
$base = 16;
$digits = substr($literal, 2);
} elseif ($prefix === '0b') {
$base = 2;
$digits = substr($literal, 2);
} elseif ($prefix === '0o') {
$base = 8;
$digits = substr($literal, 2);
} elseif ($literal !== '' && $literal[0] === '0' && strspn($literal, '01234567') === strlen($literal)) {
$base = 8;
$digits = $literal;
} else {
$base = 10;
$digits = $literal;
}

if ($base === 10) {
$decimal = ltrim($digits, '0');
} else {
// decimal digits, least significant first
$decimalDigits = [0];
foreach (str_split($digits) as $character) {
$carry = intval($character, $base);
for ($i = 0; $i < count($decimalDigits); $i++) {
$product = $decimalDigits[$i] * $base + $carry;
$decimalDigits[$i] = $product % 10;
$carry = intdiv($product, 10);
}
while ($carry > 0) {
$decimalDigits[] = $carry % 10;
$carry = intdiv($carry, 10);
}
}
$decimal = ltrim(implode('', array_reverse($decimalDigits)), '0');
}

if ($decimal === '') {
return '0';
}

return $sign . $decimal;
}

}
3 changes: 1 addition & 2 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use function array_key_exists;
use function count;
use function rtrim;
use function str_replace;
use function trim;

/**
Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 1 addition & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
31 changes: 23 additions & 8 deletions tests/PHPStan/Parser/ConstExprParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function provideIntegerNodeParseData(): Iterator

yield [
'+123',
new ConstExprIntegerNode('+123'),
new ConstExprIntegerNode('123'),
];

yield [
Expand All @@ -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 [
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
]),
],
[
Expand Down
Loading