type-lang / phpdoc
用于识别PHP DocBlock注释中PHPDoc注解的库
1.0.0
2024-06-29 18:10 UTC
Requires
- php: ^8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.53
- jetbrains/phpstorm-attributes: ^1.0
- 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
- type-lang/parser: ^1.0
This package is auto-updated.
Last update: 2024-09-09 20:36:43 UTC
README

TypeLang PHPDoc解析器的参考实现。
阅读文档页面获取更多信息。
安装
TypeLang PHPDoc解析器以Composer仓库的形式提供,可以在项目的根目录中使用以下命令进行安装
composer require type-lang/phpdoc
快速入门
$parser = new \TypeLang\PHPDoc\Parser(); $result = $parser->parse(<<<'PHPDOC' /** * Example description {@see some} and blah-blah-blah. * * @Example\Annotation("foo") * @return array<non-empty-string, TypeStatement> * @throws \Throwable */ PHPDOC); var_dump($result);
预期输出
TypeLang\PHPDoc\DocBlock { -description: TypeLang\PHPDoc\Tag\Description\Description { -template: "Example description %1$s and blah-blah-blah." -tags: array:1 [ 0 => TypeLang\PHPDoc\Tag\Tag { #description: TypeLang\PHPDoc\Tag\Description\Description { -template: "some" -tags: [] } #name: "see" } ] } -tags: array:3 [ 0 => TypeLang\PHPDoc\Tag\Tag { #description: TypeLang\PHPDoc\Tag\Description\Description { -template: "("foo")" -tags: [] } #name: "Example\Annotation" } 1 => TypeLang\PHPDoc\Tag\Tag { #description: TypeLang\PHPDoc\Tag\Description\Description { -template: "array<non-empty-string, TypeStatement>" -tags: [] } #name: "return" } 2 => TypeLang\PHPDoc\Tag\Tag { #description: TypeLang\PHPDoc\Tag\Description\Description { -template: "\Throwable" -tags: [] } #name: "throws" } ] }
结构元素
DocBlock
DocBlock是注释对象的表示。
/** | * Hello world | ← DocBlock's description. * | * @param int $example | ← DocBlock's tag #1. * @throws \Throwable Description | ← DocBlock's tag #2. */ |
getDescription()
—— 提供一个Description
对象。getTags()
—— 提供一个Tag
对象的列表。
/** @template-implements \Traversable<array-key, Tag> */ class DocBlock implements \Traversable { public function getDescription(): Description; /** @return list<Tag> */ public function getTags(): array; }
描述
描述是描述对象的表示,可能包含其他标签。
/** ↓↓↓↓↓↓↓↓↓↓↓ | ← This is a nested tag of the description. * Hello world {@see some} and blah-blah-blah. | ↑↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ | ← This is part of the template. */
getTemplate()
—— 提供一个格式化后的描述模板字符串。getTags()
—— 提供一个Tag
对象的列表。
/** @template-implements \Traversable<array-key, Tag> */ class Description implements \Traversable, \Stringable { public function getTemplate(): string; /** @return list<Tag> */ public function getTags(): array; }
标签
标签代表一个名称(ID)及其内容。
/** ↓↓↓↓↓↓ | ← This is a tag name. * @throws \Throwable An error occurred. | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ | ← This is tag description. */
getName()
—— 提供标签的名称(ID)。getDescription()
—— 提供标签的可选描述。
class Tag implements \Stringable { public function getName(): string; public function getDescription(): ?Description; }