diff --git a/grammar/php5.y b/grammar/php5.y index 20a7cd5a66..7b6a9ee808 100644 --- a/grammar/php5.y +++ b/grammar/php5.y @@ -793,7 +793,7 @@ ctor_arguments: common_scalar: T_LNUMBER { $$ = $this->parseLNumber($1, attributes(), true); } - | T_DNUMBER { $$ = new Scalar\DNumber(Scalar\DNumber::parse($1), attributes(), $1); } + | T_DNUMBER { $$ = Scalar\DNumber::fromString($1); } | T_CONSTANT_ENCAPSED_STRING { $attrs = attributes(); $attrs['kind'] = strKind($1); $$ = new Scalar\String_(Scalar\String_::parse($1, false), $attrs); } diff --git a/grammar/php7.y b/grammar/php7.y index c3112aa8fc..afcf811162 100644 --- a/grammar/php7.y +++ b/grammar/php7.y @@ -1024,7 +1024,7 @@ dereferencable_scalar: scalar: T_LNUMBER { $$ = $this->parseLNumber($1, attributes()); } - | T_DNUMBER { $$ = new Scalar\DNumber(Scalar\DNumber::parse($1), attributes(), $1); } + | T_DNUMBER { $$ = Scalar\DNumber::fromString($1); } | dereferencable_scalar { $$ = $1; } | constant { $$ = $1; } | class_constant { $$ = $1; } diff --git a/lib/PhpParser/Node/Scalar/DNumber.php b/lib/PhpParser/Node/Scalar/DNumber.php index 19b2d17279..d4796d65bb 100644 --- a/lib/PhpParser/Node/Scalar/DNumber.php +++ b/lib/PhpParser/Node/Scalar/DNumber.php @@ -9,26 +9,32 @@ class DNumber extends Scalar /** @var float Number value */ public $value; - /** @var float|string */ - public $rawValue; - /** * Constructs a float number scalar node. * - * @param float $value Value of the number - * @param array $attributes Additional attributes - * @param string|float|null $rawValue + * @param float $value Value of the number + * @param array $attributes Additional attributes */ - public function __construct(float $value, array $attributes = [], $rawValue = null) { + public function __construct(float $value, array $attributes = []) { $this->attributes = $attributes; $this->value = $value; - $this->rawValue = $rawValue ?? $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 * diff --git a/lib/PhpParser/Parser/Php5.php b/lib/PhpParser/Parser/Php5.php index 4dd145fb37..f9cbf04d33 100644 --- a/lib/PhpParser/Parser/Php5.php +++ b/lib/PhpParser/Parser/Php5.php @@ -2275,7 +2275,7 @@ protected function initReduceCallbacks() { $this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, true); }, 434 => function ($stackPos) { - $this->semValue = new Scalar\DNumber(Scalar\DNumber::parse($this->semStack[$stackPos-(1-1)]), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, $this->semStack[$stackPos-(1-1)]); + $this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)]); }, 435 => function ($stackPos) { $attrs = $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes; $attrs['kind'] = ($this->semStack[$stackPos-(1-1)][0] === "'" || ($this->semStack[$stackPos-(1-1)][1] === "'" && ($this->semStack[$stackPos-(1-1)][0] === 'b' || $this->semStack[$stackPos-(1-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED); diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index 739b08d014..45b6ce692f 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -2554,7 +2554,7 @@ protected function initReduceCallbacks() { $this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes); }, 515 => function ($stackPos) { - $this->semValue = new Scalar\DNumber(Scalar\DNumber::parse($this->semStack[$stackPos-(1-1)]), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, $this->semStack[$stackPos-(1-1)]); + $this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)]); }, 516 => function ($stackPos) { $this->semValue = $this->semStack[$stackPos-(1-1)]; diff --git a/test/PhpParser/Node/Scalar/DNumberTest.php b/test/PhpParser/Node/Scalar/DNumberTest.php index ffb1800993..b3e7c76a3c 100644 --- a/test/PhpParser/Node/Scalar/DNumberTest.php +++ b/test/PhpParser/Node/Scalar/DNumberTest.php @@ -22,6 +22,6 @@ public function testRawValue() /** @var DNumber $dnumber */ $this->assertSame(1234.56, $lLumber->value); - $this->assertSame('1_234.56', $lLumber->rawValue); + $this->assertSame('1_234.56', $lLumber->getAttribute('rawValue')); } }