定制 type-lang/reader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

type-lang/reader

最新稳定版本:1.1.1

Composer 安装命令:

composer require type-lang/reader

包简介

Library for reading TypeLang AST nodes from types exposed by PHP Reflection objects

README 文档

README

PHP 8.1+ Latest Stable Version Latest Unstable Version License MIT

Provides a set of methods for converting PHP Reflection objects into the TypeLang AST Nodes.

Read documentation pages for more information.

Installation

TypeLang Reader is available as Composer repository and can be installed using the following command in a root of your project:

composer require type-lang/reader

Quick Start

$reader = new \TypeLang\Reader\ReflectionReader();

$node = $reader->findFunctionType(
    function: new \ReflectionFunction(function(): void {}),
);

var_dump($node);

Expected Output:

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
}

Creating From Reflection

$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";
        }
    }
}

统计信息

  • 总下载量: 5.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-01