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.
Install the package via Composer:
composer require type-lang/typesRequirements:
- PHP 8.1+
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.