smuuf / php-docblock-parser
一个简单的PHP文档注释解析器。
0.1.1
2021-12-11 01:30 UTC
Requires
- php: ^7.4|^8.0
- ext-mbstring: *
- smuuf/strict-object: ^1.0
Requires (Dev)
- nette/tester: ^2.4
- phpstan/phpstan: ^0.12.67
This package is auto-updated.
Last update: 2024-09-11 08:10:13 UTC
README
PHP文档注释文本解析器
一个简单的工具,可以解析PHP文档注释文本,同时也能读取标签及其参数
/**
* Some text.
*
* @whatever-tags(with-arguments: yes, or-even=these)
*/
... 或者您可以直接解析文本,即使没有包含在注释块中也可以
Some text.
@whatever-tags(with-arguments: yes, or-even=these)
解析结果
文档注释
- 注释文本 (标题 + 正文)
- 注释标题
- 注释正文
标签
对象- 标签名称
TagArg
对象- 标签参数名称
- 标签参数值
示例用法
<?php $text =<<<TEXT Lorem ipsum dolor sit amet. Consectetuer adipiscing elit. Nulla quis diam. In rutrum. Maecenas libero. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. @tag1 @tag2.abc(arg1: yes, arg2 = no, arg3-without-value, arg4=maybe) @tag3 @tag3(but_with: argument) TEXT; $docBlock = \Smuuf\DocBlockParser\Parser::parse($text); // // Title, body, text. // $docBlock->hasTitle(); // true $docBlock->getTitle(); // "Lorem ipsum dolor sit amet." $docBlock->getBody(); // "Consectetuer adipiscing elit. Nulla quis diam ..." $docBlock->getText(); // "Lorem ipsum dolor sit amet. Consectetuer ..." // // Tags. // $docBlock->getTags(); // Tags object containing all tags. $docBlock->hasTag('tag1'); // true $docBlock->hasTag('tag2.abc'); // true $docBlock->hasTag('tag3'); // true $docBlock->hasTag('tag999lol'); // false // // Single tag. // $tag = $docBlock->getTags('tag2.abc')->getFirst(); // Instance of Tag object. $tag->getArgs(); // A dict array of [tag_arg_name => TagArg object] // // Multiple tags with the same name. // $docBlock->getTags('tag3')->getAll(); // List array [<Tag object for "@tag3">, <Tag object for "@tag3(but_with: argument)">] $tag = $docBlock->getTags('tag3')->getFirst(); // Instance of Tag object for the tag "@tag3". $tag->getArgs(); // An empty array. // // Tag args. // $tag = $docBlock->getTags('tag2.abc')->getFirst(); $tag->hasArg('arg1'); // true $tag->hasArg('arg2'); // true $tag->hasArg('arg999'); // false $tagArg = $tag->getArg('arg2'); // Instance of TagArg object. $tagArg->getName(); // "arg2" $tagArg->getValue(); // "no" $tagArg = $tag->getArg('arg3-without-value'); // Instance of TagArg object. $tagArg->getName(); // "arg3-without-value" $tagArg->getValue(); // null