barryvdh/reflection-docblock
Fork of phpDocumentor ReflectionDocBlock for laravel-ide-helper. Parses PHPDoc DocBlocks to extract descriptions, tags, and types, working like PHP’s Reflection. Use to read getDocComment() from classes/methods or raw DocBlock strings.
This package provides a robust DocBlock parser compatible with the PHPDoc standard. It's primarily used behind the scenes by tools like laravel-ide-helper, but can be leveraged directly for custom annotation processing or reflection-based tooling. Start by installing it via Composer:
composer require barryvdh/reflection-docblock
Then parse a DocBlock like this:
use phpDocumentor\Reflection\DocBlock;
$doc = <<<DOCBLOCK
/**
* @param int $id
* @return string
*/
DOCBLOCK;
$reflection = new DocBlock($doc);
echo $reflection->getTags()[0]->getName(); // 'param'
First use case: extract type hints and annotations from controller or service methods to generate documentation, configuration, or validation rules.
ReflectionMethod, ReflectionClass, or ReflectionProperty objects directly to the parser:
$method = new ReflectionMethod(MyClass::class, 'myMethod');
$docBlock = new DocBlock($method);
$docBlock->getTags()) and match tag names (e.g., @route, @rule) to implement custom behavior—ideal for framework-agnostic annotation systems.ContextFactory (v2.3.0+): Use ContextFactory::create() for more accurate type resolution, especially with namespaced types or generics.phpdocumentor/reflection-docblock (v4+).true/false literals.Collection<int, User>), but context matters—use ContextFactory::create() with namespace info for reliable resolution.var_dump($docBlock->getTags()) or json_encode($docBlock->getDecoratedObject()) (if available) to inspect hidden properties or malformed tags.How can I help you explore Laravel packages today?