
Converts native PHP types exposed by Reflection objects into TypeLangTypeLang\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 = newTypeLang\Reader\ReflectionReader();
$node = $reader->findFunctionType(
newReflectionFunction(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 = newReflectionClass(Path\To\Example::class);
$reader = newTypeLang\Reader\ReflectionReader();
$printer = newTypeLang\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.