type-lang/printer

将TypeLange AST节点渲染为其字符串表示的库

1.0.1 2024-06-29 18:02 UTC

This package is auto-updated.

Last update: 2024-09-02 07:49:20 UTC


README

PHP 8.1+ Latest Stable Version Latest Unstable Version License MIT

TypeLang Printer的参考实现。

资源

安装

TypeLang Printer作为Composer仓库提供,您可以使用以下命令在项目根目录中安装:

composer require type-lang/printer

快速开始

$parser = new \TypeLang\Parser\Parser();
$type = $parser->parseType(<<<'PHP'
    array{
        field1: (callable(Example,int):mixed),
        field2: list<Some>,
        field3: iterable<array-key, array{int, non-empty-string}>,
        Some::CONST_*,
        "\njson_flags": \JSON_*,
        ...
    }
    PHP);

// Print Statement

$native = new \TypeLang\Printer\NativeTypePrinter();
echo $native->print($type);

// Expected Output:
// array

$phpdoc = new \TypeLang\Printer\PrettyPrinter();
echo $phpdoc->print($type);

// Expected Output:
// array{
//     field1: callable(Example, int): mixed,
//     field2: list<Some>,
//     field3: iterable<array-key, array{
//         int,
//         non-empty-string
//     }>,
//     Some::CONST_*,
//     "\njson_flags": \JSON_*,
//     ...
// }