ilya/block

简化解析 docblocks

1.5.0 2014-06-14 16:38 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:58:05 UTC


README

Build Status

Block 让解析 PHP DocBlocks 变得简单。

简单、文档齐全且完全测试。需要 PHP 5.4。

安装

使用 Composer: composer require "ilya/block:~1"

示例

假设你有以下代码

class Foo {

    /**
     * The Bar.
     *
     * @var integer
     */
    protected $bar;

    /**
     * The Baz.
     *
     * @var string
     */
    public $baz;

    /**
     * Do Amaze.
     *
     * @param string $amaze
     * @return void
     */
    private function wow($amaze)
    {

    }

}

首先,像这样实例化 Block: $block = new Block\Block(new Foo);

现在你可以开始使用以下 4 个方法来检查 docblocks。

  • Block\Comment 属性(string $name)
  • 数组属性(integer $filter)
  • Block\Comment 方法(string $name)
  • 数组方法(integer $filter)
  • Block\Comment 反射器(Reflector $reflector)

方法 propertymethod 将接收一个名称(作为字符串)并返回 Block\Comment 实例。方法 propertiesmethods 将接收一个可选参数 $filter 并返回 Block\Comment 实例数组。

如果你想知道那个 $filter 是什么,请看以下示例

$block->methods(ReflectionMethod::IS_PUBLIC); // only fetch public methods

// only fetch private AND protected properties
$block->properties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED);

好的,现在你有了(或许多)\Block\Comment 实例,你可以执行什么操作呢?

  • 将实例转换为字符串:$comment->getComment()。请注意,(string) $comment 也可以工作!
  • 将其拆分为行:$comment->getLines()

让我们回顾第二种选项。

调用 getLines() 方法将返回一个数组,其中的每个元素都是 \Block\Line 实例。

这是它提供的内容

  • boolean isTag(void) - 确定行是否包含标签:对于 @param int $speed 返回 true,对于 My desc 返回 false。
  • string getLine(void) - 获取行本身,作为字符串,请注意 (string) $line 也可以工作。
  • array tokenize(void) - 将行拆分为 "tokens" - 例如,@param int $speed 将表示为 ['@param', 'int', '$speed']。所有空白都将忽略。
  • string stripTag(void)

就是这样,希望你现在可以开始在项目中使用 Block。

许可证

Block 在 MIT 许可证下授权。