Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNumber] Add rawValue attribute to hold the original value #833

Merged
merged 2 commits into from May 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
| 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 { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
| dereferencable_scalar { $$ = $1; }
| constant { $$ = $1; }
| class_constant { $$ = $1; }
Expand Down
13 changes: 12 additions & 1 deletion lib/PhpParser/Node/Scalar/DNumber.php
Expand Up @@ -24,6 +24,17 @@ 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);
}

Comment on lines +27 to +37
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should mirror the LNumber::fromString() https://github.com/nikic/PHP-Parser/pull/832/files#diff-dcef01e803010221926e1c1f1d388bd86cd219dc843253abf177cca48fb57ba1

We could add this to grammar itself, but it would require either custom method in AbstractParser or some grammar coding. That would be both harder to maintain and inconsistent with LNumber.

/**
* @internal
*
Expand Down Expand Up @@ -63,7 +74,7 @@ public static function parse(string $str) : float {
// dec
return (float) $str;
}

public function getType() : string {
return 'Scalar_DNumber';
}
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->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
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->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
516 => function ($stackPos) {
$this->semValue = $this->semStack[$stackPos-(1-1)];
Expand Down
27 changes: 27 additions & 0 deletions test/PhpParser/Node/Scalar/DNumberTest.php
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace PhpParser\Node\Scalar;

use PhpParser\Node\Stmt\Echo_;
use PhpParser\ParserFactory;

class DNumberTest extends \PHPUnit\Framework\TestCase
{
public function testRawValue()
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$nodes = $parser->parse('<?php echo 1_234.56;');

$echo = $nodes[0];
$this->assertInstanceOf(Echo_::class, $echo);

/** @var Echo_ $echo */
$lLumber = $echo->exprs[0];
$this->assertInstanceOf(DNumber::class, $lLumber);

/** @var DNumber $dnumber */
$this->assertSame(1234.56, $lLumber->value);
$this->assertSame('1_234.56', $lLumber->getAttribute('rawValue'));
}
}
6 changes: 4 additions & 2 deletions test/PhpParser/NodeAbstractTest.php
Expand Up @@ -273,7 +273,8 @@ function functionName(&$a = 0, $b = 1.0) {
"value": 1,
"attributes": {
"startLine": 4,
"endLine": 4
"endLine": 4,
"rawValue": "1.0"
}
},
"flags": 0,
Expand Down Expand Up @@ -426,7 +427,8 @@ function functionName(&$a = 0, $b = 1.0) {
"nodeType": "Scalar_DNumber",
"attributes": {
"startLine": 4,
"endLine": 4
"endLine": 4,
"rawValue": "1.0"
},
"value": 1
},
Expand Down