type-lang / reader
用于从PHP反射对象公开的类型中读取TypeLang AST节点的库
1.0.2
2024-06-29 19:11 UTC
Requires
- php: ^8.1
- type-lang/parser: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.53
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.11
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/phpunit: ^10.5|^11.0
- rector/rector: ^1.0
- symfony/var-dumper: ^5.4|^6.0|^7.0
- type-lang/printer: ^1.0
This package is auto-updated.
Last update: 2024-09-02 07:52:06 UTC
README

提供了一组方法,用于将PHP反射对象转换为TypeLang AST节点。
阅读文档页面获取更多信息。
安装
TypeLang Reader可以作为Composer仓库使用,您可以在项目的根目录中使用以下命令进行安装
composer require type-lang/reader
快速入门
$reader = new \TypeLang\Reader\ReflectionReader(); $node = $reader->findFunctionType( function: new \ReflectionFunction(function(): void {}), ); var_dump($node);
预期输出
TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 0
+name: TypeLang\Parser\Node\Name {
+offset: 0
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 0
+value: "void"
}
]
}
+arguments: null
+fields: null
}
从反射创建
$class = new \ReflectionClass(Path\To\Example::class); // Printer component provided by "type-lang/printer" package. $printer = new \TypeLang\Printer\PrettyPrinter(); $converter = new \TypeLang\Reader\ReflectionReader(); // Dump all constants with its types. foreach ($class->getReflectionConstants() as $constant) { // Creates type node AST from a constant's type. if ($type = $converter->findConstantType($constant)) { echo 'const ' . $constant->name . ' has type ' . $printer->print($type) . "\n"; } } // Dump all properties with its types. foreach ($class->getProperties() as $property) { // Creates type node AST from a property's type. if ($type = $converter->findPropertyType($property)) { echo 'property ' . $property->name . ' has type ' . $printer->print($type) . "\n"; } } // Dump all methods with its types. foreach ($class->getMethods() as $method) { // Creates type node AST from any function's return type. if ($type = $converter->findFunctionType($method)) { echo 'function ' . $method->name . ' has type ' . $printer->print($type) . "\n"; } // Creates type node AST from a parameter's type. foreach ($method->getParameters() as $parameter) { if ($type = $converter->findParameterType($parameter)) { echo 'parameter ' . $parameter->name . ' has type ' . $printer->print($type) . "\n"; } } }