Skip to content

Commit d3eb10a

Browse files
authored
[LNumber] Add rawValue attribute to LNumber to allow numeric separator etc. (#832)
1 parent a6e3466 commit d3eb10a

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/PhpParser/Node/Scalar/LNumber.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function getSubNodeNames() : array {
4141
* @return LNumber The constructed LNumber, including kind attribute
4242
*/
4343
public static function fromString(string $str, array $attributes = [], bool $allowInvalidOctal = false) : LNumber {
44+
$attributes['rawValue'] = $str;
45+
4446
$str = str_replace('_', '', $str);
4547

4648
if ('0' !== $str[0] || '0' === $str) {
@@ -71,7 +73,7 @@ public static function fromString(string $str, array $attributes = [], bool $all
7173
$attributes['kind'] = LNumber::KIND_OCT;
7274
return new LNumber(intval($str, 8), $attributes);
7375
}
74-
76+
7577
public function getType() : string {
7678
return 'Scalar_LNumber';
7779
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PhpParser\Node\Scalar;
4+
5+
use PhpParser\Node\Stmt\Echo_;
6+
use PhpParser\ParserFactory;
7+
8+
class NumberTest extends \PHPUnit\Framework\TestCase
9+
{
10+
public function testRawValue()
11+
{
12+
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
13+
$nodes = $parser->parse('<?php echo 1_234;');
14+
15+
$echo = $nodes[0];
16+
$this->assertInstanceOf(Echo_::class, $echo);
17+
18+
/** @var Echo_ $echo */
19+
$lLumber = $echo->exprs[0];
20+
$this->assertInstanceOf(LNumber::class, $lLumber);
21+
22+
/** @var LNumber $lnumber */
23+
$this->assertSame(1234, $lLumber->value);
24+
$this->assertSame('1_234', $lLumber->getAttribute('rawValue'));
25+
}
26+
}

test/PhpParser/NodeAbstractTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ function functionName(&$a = 0, $b = 1.0) {
245245
"attributes": {
246246
"startLine": 4,
247247
"endLine": 4,
248+
"rawValue": "0",
248249
"kind": 10
249250
}
250251
},
@@ -398,6 +399,7 @@ function functionName(&$a = 0, $b = 1.0) {
398399
"attributes": {
399400
"startLine": 4,
400401
"endLine": 4,
402+
"rawValue": "0",
401403
"kind": 10
402404
},
403405
"value": 0

0 commit comments

Comments
 (0)