GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - php-type-language/printer: [READ ONLY] 🖌️ Library for rendering TypeLange AST nodes into it's string representation

BranchesTags
Open more actions menu

Latest commit

 

History

128 Commits
128 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
resources/aliases
resources/aliases
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpstan.neon
phpstan.neon
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

PHP 8.4+ Latest Stable Version Latest Unstable Version License MIT


About

The reference printer for TypeLang. It renders TypeLang\Type\* AST nodes back into their string representation.

Two printers are provided:

  • NativeTypePrinter — outputs a valid, native PHP type declaration.
  • PrettyTypePrinter — outputs the full PHPStan/Psalm-style type, formatted across multiple lines.

Full documentation is available at typelang.dev.

Installation

Install the package via Composer:

composer require type-lang/printer

Requirements:

  • PHP 8.1+

Usage

Parse a type into an AST (using type-lang/parser), then print it back with either printer:

$parser = new TypeLang\Parser\TypeParser();

$type = $parser->parse(<<<'PHP'
    array{
        field1: (callable(Example, int): mixed),
        field2: list<Some>,
        ...
    }
    PHP);

echo (new TypeLang\Printer\NativeTypePrinter())->print($type);
// array

echo (new TypeLang\Printer\PrettyTypePrinter())->print($type);
// array{
//     field1: callable(Example, int): mixed,
//     field2: list<Some>,
//     ...
// }

Options

The formatting is set with the PrinterOptions:

use TypeLang\Printer\PrettyTypePrinter;
use TypeLang\Printer\PrinterOptions;
use TypeLang\Printer\PrinterOptions\CommaSpacing;
use TypeLang\Printer\PrinterOptions\ListStyle;
use TypeLang\Printer\PrinterOptions\NullableStyle;
use TypeLang\Printer\PrinterOptions\UnionSpacing;

$printer = new PrettyTypePrinter(new PrinterOptions(
    unionSpacing: UnionSpacing::Spaced,        // int | string
    maxInlineTemplateItems: 2,             // array<K, V> is written one argument per line after 2 of them
    commaSpacing: CommaSpacing::Compact,       // array<K,V>
    nullableStyle: NullableStyle::Union,   // int|null instead of ?int
    listStyle: ListStyle::Generic,         // array<int> instead of int[]
));

Printing a node in your own way

Every kind of node is printed by its own NodePrinterInterface, and a printer looks them up in a registry. To change how a node is printed, replace its printer instead of extending the printer:

use TypeLang\Printer\NodePrinterInterface;
use TypeLang\Printer\PrettyTypePrinter;
use TypeLang\Printer\PrinterContext;
use TypeLang\Printer\TypePrinter;
use TypeLang\Type\NamedTypeNode;
use TypeLang\Type\Node;

/**
 * @template-implements NodePrinterInterface<NamedTypeNode>
 */
final class ShoutingNamePrinter implements NodePrinterInterface
{
    public function printNode(Node $node, PrinterContext $context): string
    {
        return \strtoupper($node->name->toString());
    }
}

$printer = new TypePrinter(
    PrettyTypePrinter::createRegistry()->with(NamedTypeNode::class, new ShoutingNamePrinter()),
);

A node without its own printer is printed by the printer of the closest parent class. The PrinterContext prints the children of a node ($context->print()) and carries the options.

About

[READ ONLY] 🖌️ Library for rendering TypeLange AST nodes into it's string representation

typelang.dev

Topics

docblockformatterphpdocprinterrendererrenderersrenderingstringifytypes

Resources

Readme
MIT license

Code of conduct

Code of conduct
Activity
Custom properties

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.