Converts native PHP types exposed by Reflection objects into TypeLang
TypeLang\Type\* AST nodes.
Given a ReflectionClassConstant, ReflectionProperty, ReflectionParameter
or ReflectionFunctionAbstract, it returns the matching type node (or null
when no type is declared).
Full documentation is available at typelang.dev.
Install the package via Composer:
composer require type-lang/readerRequirements:
- PHP 8.4+
TypeLang\Reader\ReflectionReader exposes a find*Type() method for every kind
of Reflection object:
$reader = new TypeLang\Reader\ReflectionReader();
$node = $reader->findFunctionType(
new ReflectionFunction(function (): void {}),
);
var_dump($node);
// object(TypeLang\Type\NamedTypeNode) {
// ["name"] => "void"
// ...
// }Combine it with type-lang/printer
to dump the resolved types of a whole class:
$class = new ReflectionClass(Path\To\Example::class);
$reader = new TypeLang\Reader\ReflectionReader();
$printer = new TypeLang\Printer\PrettyTypePrinter();
foreach ($class->getProperties() as $property) {
if ($type = $reader->findPropertyType($property)) {
echo "property {$property->name}: {$printer->print($type)}\n";
}
}
foreach ($class->getMethods() as $method) {
if ($type = $reader->findFunctionType($method)) {
echo "method {$method->name}(): {$printer->print($type)}\n";
}
}Constants (findConstantType()) and parameters (findParameterType()) are read
the same way.