
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.4+
Every node extends the abstract Node class and exposes an $offset (byte
offset of the token in the original source). You usually get nodes from the
parser, but they can also be constructed by hand.
useTypeLang\Type\Identifier;
useTypeLang\Type\Name;
$id = Identifier::createFromString('non-empty-string');
$id->value; // 'non-empty-string'$id->isVirtual; // true (contains "-", e.g. "array-key", "positive-int")$id->isBuiltin; // false (e.g. "int", "bool", "null")$id->isSpecial; // false (e.g. "self", "static", "parent")$name = Name::createFromString('\TypeLang\Type\Node');
$name->isFullyQualified; // true$name->first->value; // 'TypeLang'$name->last->value; // 'Node'useTypeLang\Type\Name;
useTypeLang\Type\NamedTypeNode;
useTypeLang\Type\NullableTypeNode;
useTypeLang\Type\UnionTypeNode;
useTypeLang\Type\TemplateArgumentListNode;
useTypeLang\Type\TemplateArgumentNode;
// intnewNamedTypeNode(Name::createFromString('int'));
// ?stringnewNullableTypeNode(newNamedTypeNode(Name::createFromString('string')));
// int|string|null (nested unions of the same kind are flattened)newUnionTypeNode(
newNamedTypeNode(Name::createFromString('int')),
newNamedTypeNode(Name::createFromString('string')),
newNamedTypeNode(Name::createFromString('null')),
);
// array<string, int>newNamedTypeNode(
name: Name::createFromString('array'),
arguments: newTemplateArgumentListNode([
newTemplateArgumentNode(newNamedTypeNode(Name::createFromString('string'))),
newTemplateArgumentNode(newNamedTypeNode(Name::createFromString('int'))),
]),
);All list containers (shape fields, template arguments, callable parameters, ...)
extend NodeList and implement Countable, ArrayAccess and IteratorAggregate:
count($list); // number of items$list[0]; // item by offset$list->first; // first item$list->last; // last itemforeach ($listas$item) { /* ... */ }The package covers the full type grammar — unions and intersections, callables, conditional (ternary) expressions, class-constant masks, literals, shape fields and attributes. See the documentation for the complete node reference.