Skip to content

Printer

Pretty printer

TypeLang\Printer\PrettyTypePrinter prints a type as it is, nothing is dropped or approximated. Use it where a human reads the output: an error message, a generated docblock, a diff of two types.

use TypeLang\Parser\TypeParser;
use TypeLang\Printer\PrettyTypePrinter;
$parser = new TypeParser();
$printer = new PrettyTypePrinter();
$printer->print($parser->parse('array<non-empty-string,list<Foo>>|null'));
// array<non-empty-string, list<Foo>>|null

The only argument of the printer is a TypeLang\Printer\PrinterOptions. The values below are the defaults:

use TypeLang\Printer\PrettyTypePrinter;
use TypeLang\Printer\PrinterOptions;
use TypeLang\Printer\PrinterOptions\CallableReturnSpacing;
use TypeLang\Printer\PrinterOptions\CommaSpacing;
use TypeLang\Printer\PrinterOptions\IntersectionSpacing;
use TypeLang\Printer\PrinterOptions\ListStyle;
use TypeLang\Printer\PrinterOptions\NullableStyle;
use TypeLang\Printer\PrinterOptions\UnionSpacing;
$printer = new PrettyTypePrinter(new PrinterOptions(
newLine: "\n",
indentation: ' ',
unionSpacing: UnionSpacing::Compact,
intersectionSpacing: IntersectionSpacing::Spaced,
callableReturnSpacing: CallableReturnSpacing::Spaced,
commaSpacing: CommaSpacing::Spaced,
nullableStyle: NullableStyle::Question,
listStyle: ListStyle::Brackets,
maxInlineShapeFields: 1,
maxInlineTemplateItems: \PHP_INT_MAX,
maxInlineCallableParameters: \PHP_INT_MAX,
));

The options are immutable and shared by both printers.

Option Value Output
unionSpacing Compact (default) int|string
Spaced int | string
intersectionSpacing Spaced (default) A & B
Compact A&B
callableReturnSpacing Spaced (default) callable(int): void
Compact callable(int):void
commaSpacing Spaced (default) array<int, string>
Compact array<int,string>
nullableStyle Question (default) ?int
Union int|null
listStyle Brackets (default) int[]
Generic array<int>
$printer = new PrettyTypePrinter(new PrinterOptions(
nullableStyle: NullableStyle::Union,
));
$printer->print($parser->parse('?int')); // int|null

A list with more items than the limit is printed one item per line.

Option Default List
maxInlineShapeFields 1 the fields of a shape
maxInlineTemplateItems PHP_INT_MAX the arguments of Foo<...>
maxInlineCallableParameters PHP_INT_MAX the parameters of a callable
$printer->print($parser->parse('array{id: int}'));
// array{id: int}
$printer->print($parser->parse('array{id: int, name: string}'));
// array{
// id: int,
// name: string
// }
$printer = new PrettyTypePrinter(new PrinterOptions(maxInlineShapeFields: 2));
$printer->print($parser->parse('array{id: int, name: string}'));
// array{id: int, name: string}

The lines are broken by newLine and indented by indentation:

$printer = new PrettyTypePrinter(new PrinterOptions(
indentation: ' ',
maxInlineTemplateItems: 1,
));
$printer->print($parser->parse('array<K, V>'));
// array<
// K,
// V
// >

Parentheses are added only where the meaning needs them:

Type Output
A&(B|C) A & (B|C)
callable(): (A|B) callable(): (A|B)
(int|string)[] (int|string)[]
$arg is null ? int : string ($arg is null ? int : string)

A conditional type is always wrapped. Everything else (generics, variance, offsets, constants, callable templates and defaults) is printed as written.

PrettyTypePrinter::createRegistry() returns a NodePrinterRegistry with a printer of every node class. A registry extended by with() is passed to the generic TypeLang\Printer\TypePrinter together with the options: this is how the native printer is built.