mpociot/reflection-docblock
Parse and work with PHP DocBlocks via reflection. mpociot/reflection-docblock extracts summaries, descriptions, tags, params, and return types from classes, methods, and properties, making it easy to generate documentation or build tooling that relies on DocBlock metadata.
Install via Composer: composer require mpociot/reflection-docblock. Start by wrapping a standard ReflectionClass, ReflectionMethod, or ReflectionProperty with the package’s DocBlock factory. The most common first use case is extracting annotations like @Route, @ParamConverter, or custom meta tags for dependency injection or routing frameworks.
use Mpociot\Reflection\DocBlock;
$reflection = new ReflectionClass(MyController::class);
$docBlock = DocBlock::create($reflection);
// Get the full description
$description = $docBlock->getDescription();
// Get specific tags
$routes = $docBlock->getTagsByName('Route');
$params = $docBlock->getTagsByName('Param');
// Get all tags as objects
foreach ($docBlock->getTags() as $tag) {
echo $tag->getName() . ': ' . $tag->getContent();
}
Begin with the DocBlock::create() factory—works for any Reflection* object. Check for tags and descriptions; ignore @internal unless explicitly needed.
@Route("/path", methods={"GET"}) annotations on method docblocks.@Assert\Type("string") or custom validation annotations to drive runtime validation or serialization logic.@Inject, @Autowired from docblocks.Integration tip: Keep
DocBlockinstantiation lightweight—always pass in the reflected object, never raw strings (for correct@param/@vartype inference and context). Cache results per class/method in your tooling to avoid repeated parsing.
#[Attribute]). Only parses /** */ DocBlocks—use phpdocumentor/reflection-docblock (its maintained successor) for modern PHP projects unless locked to older codebases.@Route("/path") returns raw content; rely on getTagsByName() and manually parse content or extend with custom Tag handlers. Use getContent() then regex only as fallback.@var and @param types are returned as raw strings—handle union types and nullable indicators (e.g., ?string) yourself unless you normalize strings.*/@ characters reliably, but nested quotes or multiline @Annotation blocks may need manual cleanup.DocBlock and override extractTags() if deeper parsing (e.g., custom AST handling) is required.getTagsByName() returns empty, inspect $docBlock->getTags()—sometimes misformatted annotations (e.g., @Route(… missing closing parenthesis) cause silent failures. Enable strict linting on your PHPDocs first.How can I help you explore Laravel packages today?