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

[LNumber] Add rawValue attribute to LNumber to allow numeric separator etc. #832

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
4 changes: 3 additions & 1 deletion lib/PhpParser/Node/Scalar/LNumber.php
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
@@ -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
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