singlequote/docblock-parser

This package is abandoned and no longer maintained. The author suggests using the phpdocumentor/reflection-docblock package instead.

A simple and fast docblock parser for PHP

v1.0.0 2018-12-17 12:44 UTC

This package is auto-updated.

Last update: 2020-12-01 13:16:43 UTC


README

Latest Version on Packagist Build Status Total Downloads

This package allows you to easily parse docblocks from a class or method.

Installation

You can install the package via composer:

composer require singlequote/docblock-parser

Laravel

In Laravel 5.5 or higher the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:

'providers' => [
    // ...
    SingleQuote\DocblockParser\DocblockParserServiceProvider::class,
];

The parser will now be available trough the DocblockParser facade.

Usage

Initializing

$parser = DocblockParser::forClass("ClassName");
$parser = DocblockParser::forMethod("ClassName", "Method");
$parser = DocblockParser::forProperty("ClassName", "Property"); // works with static properties
$parser = new DocblockParser(new ReflectionObject(new stdClass()));

Getting the comment

$parser->getComment();

Getting a tag

$parser->tagName;

// or

$parser->getTag('tagName', 'default value');

Getting all tags

$parser->getTags();

Checking if a tag exists

$parser->hasTag('tagName');

Using true/ false values

to check on true/false values you need to put "is" before the tag name. This will check if the tag exists or the tag value is true

/**
 * @deprecated
 */
function foo(){}
$parser->isDeprecated;

Getting the raw docblock

$parser->getRaw();

Example

The example is based on this class

/**
 * Class FooBar
 * @version 1.0.0
 */
class FooBar
{
    /**
     * @var string
     */
    public static $version = "1.0.0";

    /**
     * @var string
     */
    public static $baz = "baz";

    /**
     * @var string
     */
    public $bar = "bar";


    /**
     * Simple multiply
     *
     * @param int $number
     * @param int $multiply
     *
     * @return float|int
     */
    public function foo(int $number, int $multiply)
    {
        return $number * $multiply;
    }


    /**
     * Get the version
     *
     * @return string
     */
    public function version()
    {
        return self::$version;
    }
}

Parsing a class docblock

$parser = DocblockParser::forClass("FooBar");

// Get the comment
$parser->getComment(); // Class FooBar

// Get the version tag
$parser->version; // 1.0.0

Parsing a method docblock

$parser = DocblockParser::forMethod("FooBar", "foo");

// Get the comment
$parser->getComment(); // Simple multiply

// Get the return tag
$parser->return; // float|int

// Check if a tag exists or is true
$parser->isDeprecated; // false

// Check if a tag exists
$parser->hasTag('author'); // false

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.