nekoos/phpdoc-parser

Jasny 架构库

dev-master 2021-06-26 08:13 UTC

This package is auto-updated.

Last update: 2024-09-26 15:12:04 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

PHP 可配置 DocBlock 解析器。

PHP 本身没有实现注解,因此该组件提供了一个使用 PHP doc-blocks 作为已知标签语法的位置的途径,使用 @ 字符。

PHPDoc 解析器允许您配置标签,包括解析和提取信息的方法。这与 phpDocumentor 风格的注解一致,与 Doctrine 类型的注解不同。

安装

composer require jasny/phpdoc-parser

使用

/**
 * The description of foo. This function does a lot of thing
 *   which are described here.
 *
 * Some more text here.
 *
 * @important
 * @uses FooReader
 * @internal Why this isn't part of the API.
 *   Multi-line is supported.
 *
 * @param string|callable $first   This is the first param
 * @param int             $second  The second one
 * @return void
 * @throws InvalidArgumentException
 * @throws DoaminException if first argument is not found
 */
function foo($first, int $second)
{
   // ...
}

解析注解

use Jasny\PHPDocParser\PHPDocParser;
use Jasny\PHPDocParser\Set\PhpDocumentor;
use Jasny\PHPDocParser\Tag\FlagTag;

$doc = (new ReflectionFunction('foo'))->getDocComment();

$customTags = [
    new FlagTag('important')
];
$tags = PhpDocumentor::tags()->with($customTags);

$parser = new PHPDocParser($tags);
$annotations = $parser->parse($doc);

结果如下

[
    'summery' => "The description of foo",
    'description' => "The description of foo. This function does a lot of thing which are described here.\n\nSome more text.",
    'important' => true,
    'uses' => 'FooReader',
    'internal' => "Why this isn't part of the API. Mutlti-line is supported",
    'params' => [
        'first' => [
            'type' => "string|callable",
            'name' => "first",
            'description' => "This is the first parm"
        ],
        'second' => [
            'type' => "int",
            'name' => "second",
        ]
    ],
    'return' => 'void'
]

标签

以下标签已包含在 PhpDocumentor::tags()

  • @api
  • @author
  • @copyright
  • @deprecated
  • @example
  • @ignore
  • @internal
  • @link
  • @method(所有方法将分组到 methods 数组中)
  • @package
  • @param(所有参数将分组到 params 数组中)
  • @property(所有属性将分组到 properties 数组中)
  • @property-read(也在 properties 数组中)
  • @property-write(也在 properties 数组中)
  • @return
  • @see
  • @since
  • @throws(所有异常将分组到 throws 数组中)
  • @todo
  • @uses
  • @used-by
  • @var

所以如果您只需要解析这些标签,您可以直接这样做

//$doc = ...; Get doc-comment string from reflection

$tags = PhpDocumentor::tags();
$parser = new PhpdocParser($tags);
$annotations = $parser->parse($doc);

标签类

以下是一个可用的标签类列表,应该可以覆盖大多数用例

以下函数用于标签文档中,用于简短地引用解析

function getNotations(string $doc, array $tags = []) {
    $tags = PhpDocumentor::tags()->add($tags);

    $parser = new PhpdocParser($tags);
    $notations = $parser->parse($doc);

    return $notations;
}

FQSEN 转换器

FQSEN 表示 Fully Qualified Structural Element Name(完全合格的结构元素名称)。FQSEN 转换器用于将类名、函数名扩展为完全唯一的名称(因此包含完整命名空间)。例如,Foo 可以转换为 Zoo\\Foo\\Bar

此类库中使用了此类转换器。一些涉及变量类型或类名的标签支持将它们作为构造函数参数添加。

例如,TypeTag 可以用于解析 @return 标签,具有以下构造函数: TypeTag($name, $fqsenConvertor = null)。如果提供,转换器会扩展类型,该类型是文档注释中返回值的类型。如果省略,类型将保持与文档注释中相同。

转换器可以通过两种方式之一提供

  • $tags = PhpDocumentor::tags($fqsenConvertor) - 对于在 PhpDocumentor::tags() 中预定义的所有标签
  • $tags = $tags->add(new TypeTag('footag', $fqsenConvertor)) - 对于显式添加到预定义中的所有标签,它应作为构造函数参数传递(如果构造函数支持)

转换器应该是一个可调用的,它接受一个类名并返回扩展后的名称。