phpdocumentor/reflection-docblock

此组件允许库通过 DocBlocks 提供注释支持或检索嵌入在 DocBlock 中的信息。


README

License: MIT Qa workflow Scrutinizer Code Coverage Scrutinizer Code Quality Stable Version Unstable Version

ReflectionDocBlock

简介

phpDocumentor 的 ReflectionDocBlock 组件提供了一种与 PHPDoc 标准 100% 兼容的 DocBlock 解析器。

此组件允许库通过 DocBlocks 提供注释支持或检索嵌入在 DocBlock 中的信息。

安装

composer require phpdocumentor/reflection-docblock

用法

要解析 DocBlock,需要一个 DocBlockFactory,可以使用其 createInstance 工厂方法进行实例化,如下所示

$factory  = \phpDocumentor\Reflection\DocBlockFactory::createInstance();

然后我们可以使用工厂的 create 方法来解释 DocBlock。请注意,也可以提供一个具有 getDocComment() 方法的类,例如 ReflectionClass 类型的对象,如果存在,创建方法将读取该对象。

$docComment = <<<DOCCOMMENT
/**
 * This is an example of a summary.
 *
 * This is a Description. A Summary and Description are separated by either
 * two subsequent newlines (thus a whiteline in between as can be seen in this
 * example), or when the Summary ends with a dot (`.`) and some form of
 * whitespace.
 */
DOCCOMMENT;

$docblock = $factory->create($docComment);

create 方法将产生一个类型为 \phpDocumentor\Reflection\DocBlock 的对象,可以查询其方法

// Contains the summary for this DocBlock
$summary = $docblock->getSummary();

// Contains \phpDocumentor\Reflection\DocBlock\Description object
$description = $docblock->getDescription();

// You can either cast it to string
$description = (string) $docblock->getDescription();

// Or use the render method to get a string representation of the Description.
$description = $docblock->getDescription()->render();

有关更多示例,最好查看 /examples 文件夹中的脚本