kamermans/docblock-reflection

简单、快速的 PHP DocBlock 解析器/反射器

v1.0.1 2016-03-22 01:34 UTC

This package is auto-updated.

Last update: 2024-09-16 05:13:10 UTC


README

Build Status

简单、快速的 PHP DocBlock 解析器/反射器

这是一个非常简单的 DocBlock / doc comment / PHPDoc 解析器。它将一个块分解为标签和注释,仅此而已。这里没有复杂的功能。如果你想要复杂的功能,请使用 Doctrine Annotation 包。

安装

使用 Composer 安装,在您的 composer.json 文件中添加以下内容

	"require": {
		"kamermans/docblock-reflection": "~1.0"
	}

用法

这个库主要用于抓取注释和标签,以下是一些示例

考虑这个类

/**
 * A Foo class
 * 
 * @deprecated
 * @version      v1.1
 * @see          Foo::bar()
 * @see          google.com
 */
class Foo {
	/**
	 * Does something that is really
	 * cool and makes your life easy
	 * 
	 * @param string $name Your name
	 * @return string
	 */
	public function bar($name) {
		return "FooBar $name";
	}
}

我们可以使用库存的 Reflection API 来探索它,但它不会解析 DocBlocks

$reflect = new RelflectionClass("Foo");
echo $reflect->getDocComment(); // spits out the raw block

要挖掘注释,请使用 kamermans\Reflection\DocBlock。你可以传递任何实现 Reflector 接口并具有 getDocComment() 方法的对象。这意味着 ReflectionObjectReflectionClassReflectionMethodReflectionFunction 等。

use kamermans\Reflection\DocBlock;

$reflect = new ReflectionClass("Foo");
$doc = new DocBlock($reflect);

// Check if the @deprecated tag exists
$doc->tagExists("deprecated");

// Get the comment "A Foo class"
$doc->getComment();

echo $doc->version; // v1.1

// The same tag can be set multiple times
echo implode("|", $doc->see); // Foo::bar()|google.com

// It works on methods too
$doc = new DocBlock($reflect->getMethod("bar"));
echo "Foo returns a $doc->return\n"; // Foo returns a string

// Multiline comments work too
$doc->getComment();