Pretty Printer
TypeLang\Printer\PrettyTypePrinter renders a type as faithfully as the AST allows: nothing is dropped or approximated, and the result parses back into an equivalent tree. It is the printer to reach for whenever a human reads the output — an exception message, a generated docblock, a diff between two types.
Out of the box it normalizes whitespace, groups nested logical types with parentheses only where precedence requires it, and breaks large shapes across several lines. Every part of that behavior is adjustable through the constructor.
Options
All options are constructor arguments; pass the ones you need by name.
Union Spacing
wrapUnionType (default false) controls the whitespace around the | of a union type.
Intersection Spacing
wrapIntersectionType (default true) does the same for the & of an intersection type.
Callable Return Type
wrapCallableReturnType (default true) controls whether a space follows the colon before a callable's return type.
Multi-line Shapes
multilineShape (default 1) is the threshold at which a shape — an array{...}, object{...}, or list{...} — spills onto multiple lines. A shape with more fields than this value is printed one field per line; anything at or below it stays on a single line.
The newLine and indention arguments (see Formatting) control how each of those lines is broken and indented:
Grouping and Precedence
The pretty printer adds parentheses only where they are needed to preserve meaning, so the output stays readable while parsing back into the same tree.
A nested logical type is wrapped when it sits inside another one:
echo $printer->print($parser->parse('A&(B|C)')); // A & (B | C)A callable return type that is itself a union or intersection is wrapped, so it is not mistaken for a continuation of the outer type:
echo $printer->print($parser->parse('callable(): A|B')); // callable(): (A | B)A conditional (ternary) type is always wrapped:
echo $printer->print($parser->parse('$arg is null ? int : string')); // ($arg is null ? int : string)
Everything else — generics, offset access, list suffixes, class constants — is rendered verbatim:
See Also
- Native Printer
Render a PHP-compatible type instead. See Native Printer.
- Type Printer
The component overview and the shared
print()contract. See Type Printer.