Rename Scalar\DNumber to Scalar\Float_

This commit is contained in:
Nikita Popov 2022-09-03 11:56:06 +02:00
parent 2b562b72a8
commit 66b20bd6bc
27 changed files with 141 additions and 129 deletions

View File

@ -75,6 +75,7 @@ used.
A number of AST nodes have been renamed or moved in the AST hierarchy:
* `Node\Scalar\DNumber` is now `Node\Scalar\Float_`.
* `Node\Expr\ClosureUse` is now `Node\ClosureUse` and no longer extends `Node\Expr`.
* `Node\Expr\ArrayItem` is now `Node\ArrayItem` and no longer extends `Node\Expr`.
* `Node\Stmt\StaticVar` is now `Node\StaticVar` and no longer extends `Node\Stmt`.

View File

@ -1178,7 +1178,7 @@ dereferencable_scalar:
scalar:
T_LNUMBER
{ $$ = $this->parseLNumber($1, attributes(), $this->phpVersion->allowsInvalidOctals()); }
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
| T_DNUMBER { $$ = Scalar\Float_::fromString($1, attributes()); }
| dereferencable_scalar { $$ = $1; }
| constant { $$ = $1; }
| class_constant { $$ = $1; }

View File

@ -240,7 +240,7 @@ final class BuilderHelpers {
}
if (is_float($value)) {
return new Scalar\DNumber($value);
return new Scalar\Float_($value);
}
if (is_string($value)) {

View File

@ -103,7 +103,7 @@ class ConstExprEvaluator {
private function evaluate(Expr $expr) {
if ($expr instanceof Scalar\LNumber
|| $expr instanceof Scalar\DNumber
|| $expr instanceof Scalar\Float_
|| $expr instanceof Scalar\String_
) {
return $expr->value;

View File

@ -1,79 +1,3 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Scalar;
use PhpParser\Node\Scalar;
class DNumber extends Scalar {
/** @var float Number value */
public $value;
/**
* Constructs a float number scalar node.
*
* @param float $value Value of the number
* @param array $attributes Additional attributes
*/
public function __construct(float $value, array $attributes = []) {
$this->attributes = $attributes;
$this->value = $value;
}
public function getSubNodeNames(): array {
return ['value'];
}
/**
* @param mixed[] $attributes
*/
public static function fromString(string $str, array $attributes = []): DNumber {
$attributes['rawValue'] = $str;
$float = self::parse($str);
return new DNumber($float, $attributes);
}
/**
* @internal
*
* Parses a DNUMBER token like PHP would.
*
* @param string $str A string number
*
* @return float The parsed number
*/
public static function parse(string $str): float {
$str = str_replace('_', '', $str);
// if string contains any of .eE just cast it to float
if (false !== strpbrk($str, '.eE')) {
return (float) $str;
}
// otherwise it's an integer notation that overflowed into a float
// if it starts with 0 it's one of the special integer notations
if ('0' === $str[0]) {
// hex
if ('x' === $str[1] || 'X' === $str[1]) {
return hexdec($str);
}
// bin
if ('b' === $str[1] || 'B' === $str[1]) {
return bindec($str);
}
// oct
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
// so that only the digits before that are used
return octdec(substr($str, 0, strcspn($str, '89')));
}
// dec
return (float) $str;
}
public function getType(): string {
return 'Scalar_DNumber';
}
}
require __DIR__ . '/Float_.php';

View File

@ -0,0 +1,82 @@
<?php declare(strict_types=1);
namespace PhpParser\Node\Scalar;
use PhpParser\Node\Scalar;
class Float_ extends Scalar {
/** @var float Number value */
public $value;
/**
* Constructs a float number scalar node.
*
* @param float $value Value of the number
* @param array $attributes Additional attributes
*/
public function __construct(float $value, array $attributes = []) {
$this->attributes = $attributes;
$this->value = $value;
}
public function getSubNodeNames(): array {
return ['value'];
}
/**
* @param mixed[] $attributes
*/
public static function fromString(string $str, array $attributes = []): Float_ {
$attributes['rawValue'] = $str;
$float = self::parse($str);
return new Float_($float, $attributes);
}
/**
* @internal
*
* Parses a DNUMBER token like PHP would.
*
* @param string $str A string number
*
* @return float The parsed number
*/
public static function parse(string $str): float {
$str = str_replace('_', '', $str);
// if string contains any of .eE just cast it to float
if (false !== strpbrk($str, '.eE')) {
return (float) $str;
}
// otherwise it's an integer notation that overflowed into a float
// if it starts with 0 it's one of the special integer notations
if ('0' === $str[0]) {
// hex
if ('x' === $str[1] || 'X' === $str[1]) {
return hexdec($str);
}
// bin
if ('b' === $str[1] || 'B' === $str[1]) {
return bindec($str);
}
// oct
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
// so that only the digits before that are used
return octdec(substr($str, 0, strcspn($str, '89')));
}
// dec
return (float) $str;
}
public function getType(): string {
return 'Scalar_Float';
}
}
// @deprecated compatibility alias
class_alias(Float_::class, DNumber::class);

View File

@ -2774,7 +2774,7 @@ class Php7 extends \PhpParser\ParserAbstract
$this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, $this->phpVersion->allowsInvalidOctals());
},
526 => function ($stackPos) {
$this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
$this->semValue = Scalar\Float_::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
527 => function ($stackPos) {
$this->semValue = $this->semStack[$stackPos-(1-1)];

View File

@ -2792,7 +2792,7 @@ class Php8 extends \PhpParser\ParserAbstract
$this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, $this->phpVersion->allowsInvalidOctals());
},
526 => function ($stackPos) {
$this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
$this->semValue = Scalar\Float_::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
527 => function ($stackPos) {
$this->semValue = $this->semStack[$stackPos-(1-1)];

View File

@ -211,7 +211,7 @@ class Standard extends PrettyPrinterAbstract {
throw new \Exception('Invalid number kind');
}
protected function pScalar_DNumber(Scalar\DNumber $node) {
protected function pScalar_Float(Scalar\Float_ $node) {
if (!is_finite($node->value)) {
if ($node->value === \INF) {
return '\INF';

View File

@ -173,7 +173,7 @@ class ClassConstTest extends \PHPUnit\Framework\TestCase {
],
[
3.1415,
new Scalar\DNumber(3.1415)
new Scalar\Float_(3.1415)
],
[
'Hallo World',

View File

@ -9,7 +9,7 @@ use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
@ -50,7 +50,7 @@ class InterfaceTest extends \PHPUnit\Framework\TestCase {
public function testAddConst() {
$const = new Stmt\ClassConst([
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
new Node\Const_('SPEED_OF_LIGHT', new Float_(299792458.0))
]);
$contract = $this->createInterfaceBuilder()->addStmt($const)->getNode();
$this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
@ -58,7 +58,7 @@ class InterfaceTest extends \PHPUnit\Framework\TestCase {
public function testOrder() {
$const = new Stmt\ClassConst([
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
new Node\Const_('SPEED_OF_LIGHT', new Float_(299792458))
]);
$method = new Stmt\ClassMethod('doSomething');
$contract = $this->createInterfaceBuilder()
@ -105,7 +105,7 @@ class InterfaceTest extends \PHPUnit\Framework\TestCase {
public function testFullFunctional() {
$const = new Stmt\ClassConst([
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
new Node\Const_('SPEED_OF_LIGHT', new Float_(299792458))
]);
$method = new Stmt\ClassMethod('doSomething');
$contract = $this->createInterfaceBuilder()

View File

@ -49,7 +49,7 @@ class ParamTest extends \PHPUnit\Framework\TestCase {
],
[
3.1415,
new Scalar\DNumber(3.1415)
new Scalar\Float_(3.1415)
],
[
'Hallo World',

View File

@ -156,7 +156,7 @@ class PropertyTest extends \PHPUnit\Framework\TestCase {
],
[
3.1415,
new Scalar\DNumber(3.1415)
new Scalar\Float_(3.1415)
],
[
'Hallo World',

View File

@ -181,7 +181,7 @@ class BuilderHelpersTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals(new Expr\ConstFetch(new Node\Name('true')), BuilderHelpers::normalizeValue(true));
$this->assertEquals(new Expr\ConstFetch(new Node\Name('false')), BuilderHelpers::normalizeValue(false));
$this->assertEquals(new Scalar\LNumber(2), BuilderHelpers::normalizeValue(2));
$this->assertEquals(new Scalar\DNumber(2.5), BuilderHelpers::normalizeValue(2.5));
$this->assertEquals(new Scalar\Float_(2.5), BuilderHelpers::normalizeValue(2.5));
$this->assertEquals(new Scalar\String_('text'), BuilderHelpers::normalizeValue('text'));
$this->assertEquals(
new Expr\Array_([

View File

@ -3,6 +3,7 @@
namespace PhpParser;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
class CompatibilityTest extends \PHPUnit\Framework\TestCase {
@ -15,6 +16,8 @@ class CompatibilityTest extends \PHPUnit\Framework\TestCase {
$this->assertTrue($node instanceof Expr\ArrayItem);
$node = new Node\StaticVar($var);
$this->assertTrue($node instanceof Stmt\StaticVar);
$node = new Scalar\Float_(1.0);
$this->assertTrue($node instanceof Scalar\DNumber);
}
/** @runInSeparateProcess */
@ -26,5 +29,7 @@ class CompatibilityTest extends \PHPUnit\Framework\TestCase {
$this->assertTrue($node instanceof Node\ArrayItem);
$node = new Node\Stmt\StaticVar($var);
$this->assertTrue($node instanceof Node\StaticVar);
$node = new Node\Scalar\DNumber(1.0);
$this->assertTrue($node instanceof Scalar\Float_);
}
}

View File

@ -16,9 +16,9 @@ class DNumberTest extends \PHPUnit\Framework\TestCase {
/** @var Echo_ $echo */
$lLumber = $echo->exprs[0];
$this->assertInstanceOf(DNumber::class, $lLumber);
$this->assertInstanceOf(Float_::class, $lLumber);
/** @var DNumber $dnumber */
/** @var Float_ $dnumber */
$this->assertSame(1234.56, $lLumber->value);
$this->assertSame('1_234.56', $lLumber->getAttribute('rawValue'));
}

View File

@ -268,7 +268,7 @@ PHP;
}
},
"default": {
"nodeType": "Scalar_DNumber",
"nodeType": "Scalar_Float",
"value": 1,
"attributes": {
"startLine": 4,
@ -425,7 +425,7 @@ JSON;
"name": "b"
},
"default": {
"nodeType": "Scalar_DNumber",
"nodeType": "Scalar_Float",
"attributes": {
"startLine": 4,
"endLine": 4,

View File

@ -332,7 +332,7 @@ class NodeTraverserTest extends \PHPUnit\Framework\TestCase {
['leaveNode', $expr, 'foobar'],
]);
$visitor5 = new NodeVisitorForTesting([
['leaveNode', $num, [new Node\Scalar\DNumber(42.0)]],
['leaveNode', $num, [new Node\Scalar\Float_(42.0)]],
]);
$visitor6 = new NodeVisitorForTesting([
['leaveNode', $expr, false],

View File

@ -4,7 +4,7 @@ namespace PhpParser;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\Node\Scalar\LNumber;
@ -143,9 +143,9 @@ class PrettyPrinterTest extends CodeTestAbstract {
[new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
[new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
[new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
[new DNumber(\INF), '\INF'],
[new DNumber(-\INF), '-\INF'],
[new DNumber(-\NAN), '\NAN'],
[new Float_(\INF), '\INF'],
[new Float_(-\INF), '-\INF'],
[new Float_(-\NAN), '\NAN'],
];
}

View File

@ -82,10 +82,10 @@ array(
name: T_3
)
value: Expr_BinaryOp_Plus(
left: Scalar_DNumber(
left: Scalar_Float(
value: 1.5
)
right: Scalar_DNumber(
right: Scalar_Float(
value: 1.5
)
)
@ -117,10 +117,10 @@ array(
)
value: Expr_BinaryOp_Mul(
left: Expr_BinaryOp_Plus(
left: Scalar_DNumber(
left: Scalar_Float(
value: 1.5
)
right: Scalar_DNumber(
right: Scalar_Float(
value: 1.5
)
)
@ -151,7 +151,7 @@ array(
value: 3
)
)
right: Scalar_DNumber(
right: Scalar_Float(
value: 4
)
)

View File

@ -23,7 +23,7 @@ array(
)
)
3: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 9.2233720368548E+18
)
)

View File

@ -23,57 +23,57 @@ Different float syntaxes
-----
array(
0: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
1: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
2: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
3: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
4: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
5: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
6: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
7: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 302000000000
)
)
8: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 3.002E+102
)
)
9: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: INF
)
)
10: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1.844674407371E+19
comments: array(
0: // various integer -> float overflows
@ -86,22 +86,22 @@ array(
)
)
11: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1.844674407371E+19
)
)
12: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1.844674407371E+19
)
)
13: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1.844674407371E+19
)
)
14: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1.844674407371E+19
)
)

View File

@ -29,7 +29,7 @@ array(
)
)
3: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: @@{ PHP_INT_MAX + 1 }@@
)
)
@ -58,4 +58,4 @@ array(
value: 3640
)
)
)
)

View File

@ -31,7 +31,7 @@ Syntax error, unexpected T_STRING from 19:2 to 19:4
Syntax error, unexpected T_STRING from 20:2 to 20:4
array(
0: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 6.674083E-11
)
)
@ -122,12 +122,12 @@ array(
)
)
12: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 0
)
)
13: Stmt_Expression(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1
)
)

View File

@ -44,7 +44,7 @@ array(
var: Expr_Variable(
name: x
)
default: Scalar_DNumber(
default: Scalar_Float(
value: 0
)
)

View File

@ -19,7 +19,7 @@ array(
name: Identifier(
name: B
)
value: Scalar_DNumber(
value: Scalar_Float(
value: 1
)
)

View File

@ -103,7 +103,7 @@ array(
name: g
)
default: Expr_UnaryMinus(
expr: Scalar_DNumber(
expr: Scalar_Float(
value: 1
)
)