stein197/doxer

简单的phpdoc注释解析器

1.2.1 2021-08-01 15:39 UTC

This package is auto-updated.

Last update: 2024-09-18 12:25:55 UTC


README

简单的PHP文档注释解析器。首先,通过以下方式使用composer安装它

composer require stein197/doxer

然后可以这样使用它

$docblock = <<<DOC
/**
 * Description.
 * Another line
 * of description.
 * @param int \$age Age parameter.
 * @param string \$name Name
 *                      parameter.
 * @return Person The person.
 * @throws Exception In case of errors.
 * 
DOC;
$doc = new Stein197\Doxer\Doc($docblock);
if ($doc->exists()) { // Checks for existance
	$doc->getDescription(); // "Description. Another line of description."
	foreach ($doc->getTags() as $tag) { // Returns and array of Stein197\Doxer\Tag instances
		$tag->getDescription(); // Handles multiple lines of description
		$tag->getName(); // Returns the name of the tag without "@" char
		foreach ($tag->getProperties() as $name => $value) { // Returns an array of properties
			"$name=$value"; // Tag's properties such as "version", "type", etc.
		}
	}
}

每个docblock注释可以包含以@符号开始的标签,每个标签可以有一个属性数组和一个可选的描述。以下列表显示了目前支持的标签

  • @author <name> [<email>]
  • @deprecated [<version>] [<description>]
  • @license [<url>] [<description>]
  • @link <uri> [<description>]
  • @param <type> <name> [<description>]
  • @return <type> [<description>]
  • @see <uri> [<description>]
  • @since <version> [<description>]
  • @var <type> [<name>] [<description>]
  • @uses <uri> [<description>]
  • @throws <type> [<description>]

例如,如果标签"@return string A string"被解析,它的属性将只包含type条目

$returnTag = (new Stein197\Doxer\Doc('/** @return string A string */'))->getTags()[0];
$returnTag->getDescription(); // "A string"
$returnTag->getProperties(); // ['type' => 'string']

但是,如果标签与预定义的任何标签都不匹配,则它将被解析为@tag [<description>]。例如,如果标签"@nonexistent string A string"被解析,它将只包含一个描述

$tag = (new Stein197\Doxer\Doc('/** @nonexistent string A string */'))->getTags()[0];
$tag->getDescription(); // "string A string"
$tag->getProperties(); // null

如果要解析的标签可以有属性,但语法不正确,则属性和描述将为空

$tag = (new Stein197\Doxer\Doc('/** @param a b c d */'))->getTags()[0];
$tag->getDescription(); // null
$tag->getProperties(); // null

注意!如果标签接受以$符号开始的名称属性(@param@var标签),则在检索name属性后,名称将不带前导的"$"检索

$doc = new Stein197\Doxer\Doc('/** @param Type $arg1 */');
$doc->getTags()[0]->getProperties(); // ['name' => 'arg1']

API

应该使用的唯一类是Stein197\Doxer\Doc,它接受一个docblock字符串作为唯一的构造函数参数。然后可以使用以下方法

以下表格包含Stein197\Doxer\Tag公共方法的列表

测试

要运行单元测试,请调用test composer脚本

composer run test