phpdocumentor/reflection-docblock
Parse and reflect PHPDoc DocBlocks with a factory-based API. Fully compatible with the PHPDoc standard, it extracts summaries, descriptions, and tags, enabling libraries to read documentation metadata and build annotation-like features from doc comments.
composer require phpdocumentor/reflection-docblock
use phpdocumentor\Reflection\DocBlockFactory;
$factory = DocBlockFactory::createInstance();
$docblock = $factory->create('/** @var string $name */');
$reflectionClass = new ReflectionClass(MyClass::class);
$docblock = $factory->create($reflectionClass->getDocComment());
$summary = $docblock->getSummary(); // "Class description"
$description = $docblock->getDescription()->render(); // Full description
DocBlockFactory: Central factory for creating DocBlock objects.DocBlock: Parsed docblock with methods like getTags(), getSummary(), getDescription().Tag: Individual docblock tags (e.g., @param, @return, @var).foreach ($docblock->getTags() as $tag) {
if ($tag->getName() === 'param') {
$type = $tag->getType();
$var = $tag->getVariableName();
// Validate or process
}
}
$factory->addTagHandler('custom', function (string $content) {
return new CustomTag($content);
});
DocBlock back to string for logging or validation.
$rendered = $docblock->render();
// Use in Laravel's logging or validation rules
ReflectionClass/ReflectionMethod for metadata extraction.
$method = new ReflectionMethod(MyClass::class, 'myMethod');
$docblock = $factory->create($method->getDocComment());
$returnType = $docblock->getTagsByName('return')[0]->getType();
FormRequest or Validator for docblock compliance.
use Illuminate\Validation\Rule;
$rules = [
'docblock' => [
'required',
Rule::custom(function ($attribute, $value, $fail) {
$docblock = $factory->create($value);
if (!$docblock->hasTag('return')) {
$fail('Missing @return tag.');
}
}),
],
];
$cache = new Illuminate\Support\Facades\Cache;
$key = 'docblock:'.md5($className);
$docblock = $cache->remember($key, 3600, function () use ($factory, $reflectionClass) {
return $factory->create($reflectionClass->getDocComment());
});
@param tags without variable names (deprecated in v5.4+).DocBlockFactory directly.Array<mixed>) may return InvalidTag. Use phpdocumentor/type-resolver for advanced cases.Reflection*::setAccessible(). The library handles this automatically, but custom extensions may need adjustments.$rawDocblock = $reflectionClass->getDocComment();
Log::debug('Raw docblock:', [$rawDocblock]);
try {
$docblock = $factory->create($docblockText);
} catch (\phpDocumentor\Reflection\Exception\InvalidTagException $e) {
Log::error('Invalid tag:', [$e->getTag()->getContent()]);
}
if (!$docblock->hasTag('return')) {
throw new \RuntimeException('Missing @return tag.');
}
$factory->addTagHandler('deprecated', function (string $content) {
return new DeprecatedTag($content);
});
$factory->setTagFactory(new CustomTagFactory());
Event::listen('illuminate.starting', function () {
$docblock = $factory->create(MyService::class->getDocComment());
if (!$docblock->hasTag('author')) {
throw new \RuntimeException('Missing @author tag.');
}
});
DocBlock objects for frequently used classes/methods.boot() or register() methods).$app->bind(DocBlockFactory::class, function () {
return DocBlockFactory::createInstance();
});
use phpdocumentor\Reflection\DocBlockFactory;
protected $factory;
public function __construct(DocBlockFactory $factory) {
$this->factory = $factory;
}
$this->app->instance(DocBlockFactory::class, DocBlockFactory::createInstance());
How can I help you explore Laravel packages today?