Skip to content

Commit 62fec10

Browse files
Support raw value for all integer formats
1 parent 4670e8f commit 62fec10

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ protected function pScalar_InterpolatedString(Scalar\InterpolatedString $node):
197197
}
198198

199199
protected function pScalar_Int(Scalar\Int_ $node): string {
200+
if ($node->getAttribute('shouldPrintRawValue') === true) {
201+
return (string) $node->getAttribute('rawValue');
202+
}
203+
200204
if ($node->value === -\PHP_INT_MAX - 1) {
201205
// PHP_INT_MIN cannot be represented as a literal,
202206
// because the sign is not part of the literal
@@ -206,9 +210,7 @@ protected function pScalar_Int(Scalar\Int_ $node): string {
206210
$kind = $node->getAttribute('kind', Scalar\Int_::KIND_DEC);
207211

208212
if (Scalar\Int_::KIND_DEC === $kind) {
209-
return $node->getAttribute('shouldPrintRawValue')
210-
? (string) $node->getAttribute('rawValue')
211-
: (string) $node->value;
213+
return (string) $node->value;
212214
}
213215

214216
if ($node->value < 0) {

test/PhpParser/PrettyPrinterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,28 @@ public static function provideTestUnnaturalLiterals() {
165165
];
166166
}
167167

168+
/** @dataProvider provideTestCustomRawValue */
169+
public function printCustomRawValue($node, $expected): void {
170+
$prettyPrinter = new PrettyPrinter\Standard();
171+
$result = $prettyPrinter->prettyPrintExpr($node);
172+
$this->assertSame($expected, $result);
173+
}
174+
175+
public static function provideTestCustomRawValue() {
176+
return [
177+
// Decimal with separator
178+
[new Int_(1000, ['rawValue' => '10_00', 'shouldPrintRawValue' => true]), '10_00'],
179+
// Hexadecimal with separator
180+
[new Int_(0xDEADBEEF, ['kind' => Int_::KIND_HEX, 'rawValue' => '0xDEAD_BEEF', 'shouldPrintRawValue' => true]), '0xDEAD_BEEF'],
181+
// Binary with separator
182+
[new Int_(0b11110000, ['kind' => Int_::KIND_BIN, 'rawValue' => '0b1111_0000', 'shouldPrintRawValue' => true]), '0b1111_0000'],
183+
// Octal with separator
184+
[new Int_(0755, ['kind' => Int_::KIND_OCT, 'rawValue' => '0755_000', 'shouldPrintRawValue' => true]), '0755_000'],
185+
// Without flag set, should use default formatting
186+
[new Int_(1000, ['rawValue' => '10_00', 'shouldPrintRawValue' => false]), '1000'],
187+
];
188+
}
189+
168190
public function testPrettyPrintWithError(): void {
169191
$this->expectException(\LogicException::class);
170192
$this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');

0 commit comments

Comments
 (0)