Skip to content

Commit

Permalink
[LNumber] Add rawValue attribute to LNumber to allow numeric separato…
Browse files Browse the repository at this point in the history
…r etc. (nikic#832)
  • Loading branch information
TomasVotruba authored May 15, 2022
1 parent a6e3466 commit d3eb10a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/PhpParser/Node/Scalar/LNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function getSubNodeNames() : array {
* @return LNumber The constructed LNumber, including kind attribute
*/
public static function fromString(string $str, array $attributes = [], bool $allowInvalidOctal = false) : LNumber {
$attributes['rawValue'] = $str;

$str = str_replace('_', '', $str);

if ('0' !== $str[0] || '0' === $str) {
Expand Down Expand Up @@ -71,7 +73,7 @@ public static function fromString(string $str, array $attributes = [], bool $all
$attributes['kind'] = LNumber::KIND_OCT;
return new LNumber(intval($str, 8), $attributes);
}

public function getType() : string {
return 'Scalar_LNumber';
}
Expand Down
26 changes: 26 additions & 0 deletions test/PhpParser/Node/Scalar/NumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace PhpParser\Node\Scalar;

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

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

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

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

/** @var LNumber $lnumber */
$this->assertSame(1234, $lLumber->value);
$this->assertSame('1_234', $lLumber->getAttribute('rawValue'));
}
}
2 changes: 2 additions & 0 deletions test/PhpParser/NodeAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ function functionName(&$a = 0, $b = 1.0) {
"attributes": {
"startLine": 4,
"endLine": 4,
"rawValue": "0",
"kind": 10
}
},
Expand Down Expand Up @@ -398,6 +399,7 @@ function functionName(&$a = 0, $b = 1.0) {
"attributes": {
"startLine": 4,
"endLine": 4,
"rawValue": "0",
"kind": 10
},
"value": 0
Expand Down

0 comments on commit d3eb10a

Please sign in to comment.