Skip to content
php-type-languagePublic

About

[READ ONLY] 📦 An AST nodes of the TypeLang

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

PHP 8.1+ Latest Stable Version Latest Unstable Version License MIT


The AST node classes (TypeLang\Type\*) for TypeLang — a declarative type language inspired by static analyzers like PHPStan and Psalm.

These plain, dependency-free DTOs are the shared vocabulary of the TypeLang ecosystem: the parser produces them, the printer renders them, and the reader builds them from Reflection.

Full documentation is available at typelang.dev.

Installation

Install the package via Composer:

composer require type-lang/types

Requirements:

  • PHP 8.1+

Usage

A type is a tree of typed nodes instead of a string, so tools can inspect and change it without parsing text. You usually get the tree from the parser, but it can be built by hand as well. Every node also keeps the $offset of its token in the source.

// Example<string, int, *>|null
$type = new UnionTypeNode([
    new NamedTypeNode(
        name: new Name('Example'),
        arguments: new TemplateListNode([
            new TypedTemplateArgumentNode(
                new NamedTypeNode(new Name('string')),
            ),
            new TypedTemplateArgumentNode(
                new NamedTypeNode(new Name('int')),
            ),
            new WildcardTemplateArgumentNode()
        ]),
    ),
    new NullLiteralNode(),
]);

// Read the tree
$type[0]->name->value;                    // 'array'
$type[0]->arguments[1]->value->name->value; // 'int'

// Change it: the tree is mutable, names and literals are immutable values
$type[0]->name = new Name('iterable');

The package covers the full type grammar: unions and intersections, generics, shapes, callables, conditional types, class constants and their masks, and literals. See the documentation for the node reference.

About

[READ ONLY] 📦 An AST nodes of the TypeLang

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages