|
14 | 14 |
|
15 | 15 | --- |
16 | 16 |
|
17 | | -Reference implementation for TypeLang Printer. |
| 17 | +## About |
18 | 18 |
|
19 | | -## Resources |
| 19 | +The reference printer for **TypeLang**. It renders `TypeLang\Type\*` AST nodes |
| 20 | +back into their string representation. |
20 | 21 |
|
21 | | -- [Documentation](https://typelang.dev) |
| 22 | +Two printers are provided: |
| 23 | + |
| 24 | +- `NativeTypePrinter` — outputs a valid, native PHP type declaration. |
| 25 | +- `PrettyTypePrinter` — outputs the full PHPStan/Psalm-style type, formatted |
| 26 | + across multiple lines. |
| 27 | + |
| 28 | +Full documentation is available at [typelang.dev](https://typelang.dev). |
22 | 29 |
|
23 | 30 | ## Installation |
24 | 31 |
|
25 | | -TypeLang Printer is available as Composer repository and can be installed |
26 | | -using the following command in a root of your project: |
| 32 | +Install the package via [Composer](https://getcomposer.org): |
27 | 33 |
|
28 | 34 | ```sh |
29 | 35 | composer require type-lang/printer |
30 | 36 | ``` |
31 | 37 |
|
32 | | -## Quick Start |
| 38 | +**Requirements:** |
| 39 | +- PHP 8.4+ |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +Parse a type into an AST (using [`type-lang/parser`](https://packagist.org/packages/type-lang/parser)), |
| 44 | +then print it back with either printer: |
33 | 45 |
|
34 | 46 | ```php |
35 | | -$parser = new \TypeLang\Parser\TypeParser(); |
36 | | -$type = $parser->parseType(<<<'PHP' |
| 47 | +$parser = new TypeLang\Parser\TypeParser(); |
| 48 | + |
| 49 | +$type = $parser->parse(<<<'PHP' |
37 | 50 | array{ |
38 | | - field1: (callable(Example,int):mixed), |
| 51 | + field1: (callable(Example, int): mixed), |
39 | 52 | field2: list<Some>, |
40 | | - field3: iterable<array-key, array{int, non-empty-string}>, |
41 | | - Some::CONST_*, |
42 | | - "\njson_flags": \JSON_*, |
43 | 53 | ... |
44 | 54 | } |
45 | 55 | PHP); |
46 | 56 |
|
47 | | -// Print Statement |
48 | | - |
49 | | -$native = new \TypeLang\Printer\NativeTypePrinter(); |
50 | | -echo $native->print($type); |
51 | | - |
52 | | -// Expected Output: |
| 57 | +echo new TypeLang\Printer\NativeTypePrinter()->print($type); |
53 | 58 | // array |
54 | 59 |
|
55 | | -$phpdoc = new \TypeLang\Printer\PrettyTypePrinter(); |
56 | | -echo $phpdoc->print($type); |
57 | | - |
58 | | -// Expected Output: |
| 60 | +echo new TypeLang\Printer\PrettyTypePrinter()->print($type); |
59 | 61 | // array{ |
60 | 62 | // field1: callable(Example, int): mixed, |
61 | 63 | // field2: list<Some>, |
62 | | -// field3: iterable<array-key, array{ |
63 | | -// int, |
64 | | -// non-empty-string |
65 | | -// }>, |
66 | | -// Some::CONST_*, |
67 | | -// "\njson_flags": \JSON_*, |
68 | 64 | // ... |
69 | 65 | // } |
70 | 66 | ``` |
0 commit comments