Skip to content

Commit 8211479

Browse files
Add shouldPrintRawValue attribute to print rawValue (#1127)
Add a shouldPrintRawValue attribute to Scalar\Int_. When enabled, the pretty printer will print the rawValue. This allows printing integers with specific separators, e.g. `10_50` instead of `1050`.
1 parent e481026 commit 8211479

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/component/Pretty_printing.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ The pretty printer respects a number of attributes used by some nodes:
3636
* `kind` on `Scalar\String_` to use single quotes (default), double quotes, heredoc or nowdoc.
3737
In the latter two cases, the heredoc/nowdoc label from the `docLabel` attribute is used.
3838
* `kind` on `Scalar\Int_` to use decimal (default), binary, octal or hexadecimal representation.
39+
* `shouldPrintRawValue` and `rawValue` on `Scalar\Int_` to preserve the original formatting of
40+
integer literals (e.g., numeric separators like `1_000`). When `shouldPrintRawValue` is set to
41+
`true`, the value from `rawValue` is used instead of the computed representation. This works for
42+
all integer formats (decimal, binary, octal, hexadecimal).
3943
* `kind` on `Cast\Double` to use `(double)` (default), `(float)` or `(real)`.
4044
* `kind` on `Expr\List_` to use `[]` or `list()` (default depends on `phpVersion` option).
4145
* `kind` on `Expr\Array_` to use `[]` or `array()` (default depends on `shortArraySyntax` option).

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,18 @@ 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 $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
203207
return '(-' . \PHP_INT_MAX . '-1)';
204208
}
205209

206210
$kind = $node->getAttribute('kind', Scalar\Int_::KIND_DEC);
211+
207212
if (Scalar\Int_::KIND_DEC === $kind) {
208213
return (string) $node->value;
209214
}

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)