定制 jasny/phpdoc-parser 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jasny/phpdoc-parser

最新稳定版本:v1.0.1

Composer 安装命令:

composer require jasny/phpdoc-parser

包简介

Jasny skeleton library

README 文档

README

jasny-banner

Jasny PHPDoc parser

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

Configurable DocBlock parser from PHP.

The PHPDoc parser allows you to configure tags including the method how to parse and extract information. This is inline with phpDocumentor style annotations and differs from for instance Doctrine type annotations.

Installation

composer require jasny/phpdoc-parser

Usage

/**
 * 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)
{
   // ...
}

Parse annotations

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);
$meta = $parser->parse($doc);

The result will be the following:

[
    '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'
]

Tags

The following tags are included in PhpDocumentor::tags():

  • @api
  • @author
  • @copyright
  • @deprecated
  • @example
  • @ignore
  • @internal
  • @link
  • @method (all methods will be grouped in methods array)
  • @package
  • @param (all params will be grouped in params array)
  • @property (all properties will be grouped in properties array)
  • @property-read (also in properties array)
  • @property-write (also in properties array)
  • @return
  • @see
  • @since
  • @throws (all exceptions will be grouped in throws array)
  • @todo
  • @uses
  • @used-by
  • @var

If you only need to parse those tags, you can simply do:

use Jasny\PhpdocParser\PhpdocParser;
use Jasny\PhpdocParser\Set\PhpDocumentor;

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

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

Tags classes

Here's a list of available tags classes:

FQSEN Resolver

FQSEN stands for Fully Qualified Structural Element Name. FQSEN convertor is used to expand class name or function name to fully unique name (so with full namespace). For example, Foo can be converted to Zoo\\Foo\\Bar.

Such convertors are used in this lib. Some tags, that deal with variable types, or classes names, support adding them as a constructor parameter.

For example, TypeTag, that can be used for parsing @return tag, has the following constructor: TypeTag($name, $fqsenConvertor = null). If provided, convertor expands the type, given as type of returned value in doc-comment. If ommited, the type will stay as it is in doc-comment.

Convertor can be provided in one of two ways:

  • $tags = PhpDocumentor::tags($fn) - for all the tags, predefined in PhpDocumentor::tags()
  • $tags = $tags->add(new TypeTag('footag', $fn)) - for all the tags, that are explicitly added to predefined, it should be passed as a constructor parameter (if it is supported by constructor).

After that create the parser from the tags as $parser = new PhpdocParser($tags).

The resolver function should accept a class name and return an expanded name.

Example

This example uses phpDocumentor/TypeResolver.

use Jasny\PhpdocParser\PhpdocParser;
use Jasny\PhpdocParser\Set\PhpDocumentor;
use phpDocumentor\Reflection\Types\ContextFactory;
use phpDocumentor\Reflection\FqsenResolver;

$reflection = new ReflectionClass('\My\Example\Classy');

$contextFactory = new ContextFactory();
$context = $contextFactory->createFromReflector($reflection);

$resolver = new FqsenResolver();
$fn = fn(string $class): string => $resolver->resolve($class, $context);

$tags = PhpDocumentor::tags($fn);
$parser = new PhpdocParser($tags);

$doc = $reflection->getDocComment();
$meta = $parser->parse($doc);

统计信息

  • 总下载量: 334.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 42
  • 点击次数: 1
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 41
  • Watchers: 1
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-15