Skip to content

Commit

Permalink
add fromString() method to create DNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 12, 2022
1 parent d50a25b commit b4c647a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion grammar/php5.y
Expand Up @@ -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); }
Expand Down
2 changes: 1 addition & 1 deletion grammar/php7.y
Expand Up @@ -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; }
Expand Down
22 changes: 14 additions & 8 deletions lib/PhpParser/Node/Scalar/DNumber.php
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php5.php
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php7.php
Expand Up @@ -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)];
Expand Down
2 changes: 1 addition & 1 deletion test/PhpParser/Node/Scalar/DNumberTest.php
Expand Up @@ -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'));
}
}

0 comments on commit b4c647a

Please sign in to comment.