### Quick Start Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/docs/index.rst A minimal example of how to use ReflectionDocBlock in a PHP project to interpret a simple DocBlock. ```php registerTag(new StandardTagFactory()); $docBlock = $factory->create( '/**\n * This is a sample DocBlock.\n *\n * @param string $variable This is a variable.\n * @return bool Whether something is true.\n */', new Context('MyNamespace') ); echo $docBlock->getDescription()->render() . "\n"; foreach ($docBlock->getTags() as $tag) { echo $tag->getName() . ": " . $tag->render() . "\n"; } ``` -------------------------------- ### Extract All Documentation Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Example of extracting summary, description, parameters, return, and throws information from a DocBlock. ```php $factory = DocBlockFactory::createInstance(); $docblock = $factory->create($comment); $doc = [ 'summary' => $docblock->getSummary(), 'description' => (string) $docblock->getDescription(), 'parameters' => [], 'return' => null, 'throws' => [], ]; foreach ($docblock->getTagsByName('param') as $param) { $doc['parameters'][] = [ 'name' => $param->getVariableName(), 'type' => (string) $param->getType(), 'description' => $param->getDescription() ? (string) $param->getDescription() : '', ]; } $returns = $docblock->getTagsByName('return'); if (count($returns) > 0) { $doc['return'] = [ 'type' => (string) $returns[0]->getType(), 'description' => $returns[0]->getDescription() ? (string) $returns[0]->getDescription() : '', ]; } foreach ($docblock->getTagsByName('throws') as $throws) { $doc['throws'][] = [ 'type' => (string) $throws->getType(), 'description' => $throws->getDescription() ? (string) $throws->getDescription() : '', ]; } return $doc; ``` -------------------------------- ### Namespace Aliases Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Example of using namespace aliases within a Context for parsing DocBlocks. ```php $context = new Context('MyNamespace', [ 'Collection' => 'Doctrine\Common\Collections\Collection', 'Repository' => 'Doctrine\ORM\EntityRepository', ]); $docblock = $factory->create($comment, $context); ``` -------------------------------- ### StandardTagFactory instantiation migration Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/docs/upgrade-to-v6.rst Example showing the change in how to instantiate `StandardTagFactory` from direct instantiation to using the `createInstance()` method. ```php $factory = new StandardTagFactory(); ``` ```php $factory = StandardTagFactory::createInstance(); ``` -------------------------------- ### Create a Factory Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md The DocBlockFactory is your entry point. It parses DocBlock strings into structured objects. ```php use phpDocumentor\Reflection\DocBlockFactory; $factory = DocBlockFactory::createInstance(); ``` -------------------------------- ### Check Method Compatibility Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Compares method parameters and return documentation against reflection data. ```php function checkMethodDocumentation($className, $methodName) { $reflection = new ReflectionMethod($className, $methodName); $factory = DocBlockFactory::createInstance(); $docblock = $factory->create($reflection); $expectedParams = $reflection->getParameters(); $documentedParams = $docblock->getTagsByName('param'); if (count($expectedParams) !== count($documentedParams)) { echo "Warning: Parameter count mismatch"; } if (!$docblock->hasTag('return')) { echo "Warning: Missing @return documentation"; } } ``` -------------------------------- ### Handling Parse Errors Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Example of catching parse errors when creating a DocBlock. ```php use phpDocumentor\Reflection\Exception\ParserException; try { $docblock = $factory->create($comment, $context); } catch (ParserException $e) { echo "Failed to parse DocBlock: " . $e->getMessage(); } ``` -------------------------------- ### Registering a Custom Tag Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Shows how to register a custom tag handler with the DocBlockFactory. ```php $factory = DocBlockFactory::createInstance([ 'custom' => MyCustomTag::class, ]); // Or register after creation $factory->registerTagHandler('custom', MyCustomTag::class); ``` -------------------------------- ### isTemplateStart() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of using the isTemplateStart() method. ```php if ($docblock->isTemplateStart()) { // Apply this docblock's tags to following elements } ``` -------------------------------- ### Extract Information Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract summary, description, and tags from a parsed DocBlock. ```php // Get summary echo $docblock->getSummary(); // Output: "Retrieves the user's name." // Get full description echo $docblock->getDescription(); // Output: "This method returns the full name of the user, combining first and last names." // Get all tags $tags = $docblock->getTags(); foreach ($tags as $tag) { echo $tag->getName() . ": " . $tag . "\n"; } // Output: // param: string $firstName // param: string $lastName // return: string // throws: InvalidArgumentException ``` -------------------------------- ### DocBlock Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of instantiating a DocBlock. ```php $description = new \phpDocumentor\Reflection\DocBlock\Description('This is the description'); $docblock = new DocBlock( 'This is the summary', $description, [] ); ``` -------------------------------- ### Migration for type-based tag creation Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/docs/upgrade-to-v6.rst Example demonstrating the migration from the removed `::create` static method to using the tag factory for instantiating tag objects. ```php $tag = Param::create($body); ``` ```php $factory = \phpDocumentor\Reflection\DocBlock\Tags\Factory\StandardTagFactory::createInstance(); $tag = $factory->create('@param int $foo'); ``` -------------------------------- ### @return Tag Details Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract detailed information from '@return' tags, including the return type and description. ```php $returnTags = $docblock->getTagsByName('return'); if (count($returnTags) > 0) { $return = $returnTags[0]; $type = $return->getType(); $description = $return->getDescription(); echo "Returns: " . (string) $type; } ``` -------------------------------- ### Example Tag Constructor Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Constructor for the Example tag, which provides example code or references an example file. ```php public function __construct( string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content ) ``` -------------------------------- ### Parsing DocBlocks from Reflection Objects Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Demonstrates parsing DocBlocks directly from PHP Reflection objects (Class, Method, Function). ```php use ReflectionClass; use ReflectionMethod; $factory = DocBlockFactory::createInstance(); // From ReflectionClass $class = new ReflectionClass('MyClass'); $docblock = $factory->create($class); // From ReflectionMethod $method = new ReflectionMethod('MyClass', 'myMethod'); $docblock = $factory->create($method); // From ReflectionFunction $function = new ReflectionFunction('myFunction'); $docblock = $factory->create($function); ``` -------------------------------- ### Get Tags by Name Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Retrieve specific tags by their name, such as '@param' or '@return'. ```php // Get all @param tags $paramTags = $docblock->getTagsByName('param'); foreach ($paramTags as $param) { echo $param->getVariableName() . ": " . $param->getType() . "\n"; } // Get @return tag $returnTags = $docblock->getTagsByName('return'); if (count($returnTags) > 0) { $return = $returnTags[0]; echo "Returns: " . $return->getType(); } ``` -------------------------------- ### getDescription() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of using the getDescription() method. ```php $description = $docblock->getDescription(); echo (string) $description; // Renders description as string ``` -------------------------------- ### Example for createInstance() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/standardtagfactory.md Example usage of the createInstance method. ```php $fqsenResolver = new FqsenResolver(); $tagFactory = StandardTagFactory::createInstance($fqsenResolver); // Now use it to parse tags $tag = $tagFactory->create('@param string $name', $context); ``` -------------------------------- ### getContext() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of using the getContext() method. ```php $context = $docblock->getContext(); if ($context) { echo $context->getNamespace(); } ``` -------------------------------- ### Implementing a Custom Tag Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md A basic implementation of a custom tag adhering to the Tag interface. ```php use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Description; class MyCustomTag implements Tag { private string $value = ''; public function __construct(string $value = '') { $this->value = $value; } public function getName(): string { return 'custom'; } public static function create(string $body) { return new static($body); } public function render(?Formatter $formatter = null): string { return $this->value; } public function __toString(): string { return $this->value; } } ``` -------------------------------- ### Context with Aliases Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/patterns.md Example of creating a context with aliases. ```php function createContextForFile(string $filePath): Context { $namespace = getNamespaceFromFile($filePath); $aliases = getUseStatementsFromFile($filePath); return new Context($namespace, $aliases); } // Usage $context = createContextForFile('src/MyClass.php'); $docblock = $factory->create($comment, $context); ``` -------------------------------- ### getLocation() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of using the getLocation() method. ```php $location = $docblock->getLocation(); if ($location) { echo "Line: " . $location->getLineNumber(); } ``` -------------------------------- ### Example for getName() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Example of how to use the getName() method. ```php echo $param->getName(); // Outputs: "name" ``` -------------------------------- ### Example for __toString() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Examples showing the string representation of different parameter types. ```php $param = new MethodParameter('name', new String_()); echo (string) $param; // Outputs: "string $name" $variadicParam = new MethodParameter('items', new Mixed(), false, true); echo (string) $variadicParam; // Outputs: "mixed ...$items" $refParam = new MethodParameter('output', new String_(), true); echo (string) $refParam; // Outputs: "string &$output" ``` -------------------------------- ### Example Usage of Constructor Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/description.md Demonstrates how to create and use a Description object. ```php $description = new Description( 'This is a description with %1$s', [ new See(new Fqsen('\MyClass')) ] ); echo (string) $description; // Output: This is a description with MyClass ``` -------------------------------- ### Example for render() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/basetag.md Example usage of the render() method. ```php $tag = new Param('name', $type); echo $tag->render(); // Uses default formatting echo $tag->render(new AlignFormatter()); // Uses custom formatter ``` -------------------------------- ### Example: getName() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tag.md Demonstrates how to get the canonical name of a tag, such as 'param' or 'return'. ```php $param = new Param('name', $type); echo $param->getName(); // Output: "param" $return = new Return_($type); echo $return->getName(); // Output: "return" ``` -------------------------------- ### getSummary() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of using the getSummary() method. ```php $summary = $docblock->getSummary(); // Output: "This is the summary." ``` -------------------------------- ### Example for create() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/standardtagfactory.md Example usage of the create method for parsing different types of tags. ```php $factory = StandardTagFactory::createInstance($fqsenResolver); // Parse a simple tag $author = $factory->create('@author John Doe '); // Parse a typed tag $param = $factory->create('@param string $name', $context); // Parse a complex tag $method = $factory->create('@method getName(): string', $context); // Handle invalid tags $invalid = $factory->create('@invalid malformed'); if ($invalid instanceof InvalidTag) { echo "Failed to parse"; } ``` -------------------------------- ### examples/03-reconstituting-a-docblock.php Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/docs/how-to/reconstituting-a-docblock.rst A practical example demonstrating how to reconstitute a DocBlock using the ReflectionDocBlock library. ```php addTag($newTag); // You can also modify existing tags or descriptions $docBlock->setDescription(new Description('This is an updated sample DocBlock.')); // To get the DocBlock string representation: $docBlockString = $docBlock->__toString(); echo $docBlockString; // Output: // /** // * This is an updated sample DocBlock. // * // * @var string // */ ``` -------------------------------- ### Parse a DocBlock Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Parse a DocBlock string into a structured DocBlock object. ```php $comment = <<create($comment); ``` -------------------------------- ### Installation Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/README.md Install the ReflectionDocBlock component using Composer. ```bash composer require phpdocumentor/reflection-docblock ``` -------------------------------- ### Example for isVariadic() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Example of how to use the isVariadic() method. ```php if ($param->isVariadic()) { echo "Variable number of arguments"; } ``` -------------------------------- ### Example for isReference() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Example of how to use the isReference() method. ```php if ($param->isReference()) { echo "Passed by reference"; } ``` -------------------------------- ### Example for getType() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Example of how to use the getType() method. ```php $type = $param->getType(); echo (string) $type; ``` -------------------------------- ### Add Your Own Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/docs/how-to/adding-your-own-tag.rst Example demonstrating how to add a custom tag to a DocBlock. ```php name = $name; $this->content = $content; $this->description = $description; } public function getName(): string { return 'my-custom-tag'; } public function getContent(): ?string { return $this->content; } public function getDescription(): ?Description { return $this->description; } public function validate(string $content): void { Assert::that($content)->minLength(5); } } $factory = DocBlockFactory::createInstance(); $factory->registerTag('my-custom-tag', MyCustomTag::class); $docblock = $factory->create('/** * @my-custom-tag This is my custom tag content */', new Context('MyNamespace')); echo $docblock->getTagsByName('my-custom-tag')[0]->getContent(); ``` -------------------------------- ### Example Usage of render() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/description.md Demonstrates rendering a description with default and custom formatters. ```php // With default formatter $text = $description->render(); // With custom formatter $customFormatter = new MyFormatter(); $text = $description->render($customFormatter); // Manually format with different formatter $alternateFormatter = new AlignFormatter(); $text = $description->render($alternateFormatter); ``` -------------------------------- ### Example for getDescription() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/basetag.md Example usage of the getDescription() method. ```php $param = new Param('name', $type, false, new Description('The name parameter')); $desc = $param->getDescription(); if ($desc) { echo (string) $desc; } ``` -------------------------------- ### Example for getDefaultValue() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Examples demonstrating the usage of getDefaultValue() with and without a default value. ```php $param = new MethodParameter('name', new String_(), false, false, "'John'"); $default = $param->getDefaultValue(); echo "Default: " . $default; // Outputs: 'John' $param2 = new MethodParameter('id', new Integer()); $default2 = $param2->getDefaultValue(); // Returns: null ``` -------------------------------- ### Method Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Example of how to retrieve and display method information using Method tags. ```php $methodTags = $docblock->getTagsByName('method'); foreach ($methodTags as $method) { $static = $method->isStatic() ? 'static ' : ''; echo $static . $method->getMethodName() . "()"; } ``` -------------------------------- ### Return Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Example of how to retrieve and use @return tags from a DocBlock. ```php $returnTags = $docblock->getTagsByName('return'); foreach ($returnTags as $return) { echo "Returns: " . $return->getType(); } ``` -------------------------------- ### isTemplateEnd() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Example of using the isTemplateEnd() method. ```php if ($docblock->isTemplateEnd()) { // Stop applying template tags to subsequent elements } ``` -------------------------------- ### Type Resolution with Namespace Context Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Provide a Context object to the factory to correctly resolve type aliases within namespaces. ```php use phpDocumentor\Reflection\Types\Context; $context = new Context('MyNamespace'); $docblock = $factory->create($comment, $context); // Now type aliases are resolved correctly ``` -------------------------------- ### @method Tag Details Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract detailed information from '@method' tags, including the method name, return type, static status, and parameters. ```php $methodTags = $docblock->getTagsByName('method'); foreach ($methodTags as $method) { $name = $method->getMethodName(); $returnType = $method->getReturnType(); $isStatic = $method->isStatic(); $parameters = $method->getParameters(); echo ($isStatic ? 'static ' : '') . $returnType . ' ' . $name . '(...)'; } ``` -------------------------------- ### isTemplateStart() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Checks if this DocBlock marks the start of a template section. ```php public function isTemplateStart(): bool ``` -------------------------------- ### Context Reuse Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/configuration.md Demonstrates how to reuse a Context object when parsing multiple DocBlocks within the same namespace to improve performance. ```php $context = new Context('MyNamespace'); $factory = DocBlockFactory::createInstance(); foreach ($docblocks as $comment) { $docblock = $factory->create($comment, $context); // Parse... } ``` -------------------------------- ### Property Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Example of how to retrieve and display property information using Property tags. ```php $propertyTags = $docblock->getTagsByName('property'); foreach ($propertyTags as $prop) { echo "Property $" . $prop->getVariableName() . ": " . $prop->getType(); } ``` -------------------------------- ### Var Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Example of how to retrieve and use @var tags from a DocBlock. ```php $varTags = $docblock->getTagsByName('var'); foreach ($varTags as $var) { echo $var->getVariableName() . ": " . $var->getType(); } ``` -------------------------------- ### Example for getName() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/basetag.md Example usage of the getName() method. ```php $param = new Param('name', $type); echo $param->getName(); // Outputs: "param" ``` -------------------------------- ### Throws Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Example of how to retrieve and use @throws tags from a DocBlock. ```php $throwsTags = $docblock->getTagsByName('throws'); foreach ($throwsTags as $throws) { echo "May throw: " . $throws->getType(); } ``` -------------------------------- ### Example Usage of MethodParameter Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/methodparameter.md Demonstrates how to create MethodParameter objects for different scenarios. ```php $param1 = new MethodParameter('name', new String_()); $param2 = new MethodParameter('count', new Integer(), false, true); // variadic $param3 = new MethodParameter('optional', new String_(), false, false, "'default'"); $method = new Method('getName', [$param1, $param2, $param3]); ``` -------------------------------- ### @method Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/README.md DocBlock example demonstrating the @method tag for documenting magic methods. ```php /** * @method string getName() * @method void setName(string $name) * @method static create(): self */ ``` -------------------------------- ### __toString() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tagwithtype.md Shows how the tag's type and description are combined into a string representation. ```php $param = new Param('name', new String_(), false, new Description('The name')); echo (string) $param; // Outputs: "string The name" ``` -------------------------------- ### CannotCreateTag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/errors.md Demonstrates how to catch the CannotCreateTag exception when attempting to create specialized tag types directly. ```php use phpDocumentor\Reflection\Exception\CannotCreateTag; try { $param = Param::create('string $name'); } catch (CannotCreateTag $e) { // Use the appropriate factory instead echo "Cannot create typed tag directly"; } ``` -------------------------------- ### create() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tagwithtype.md Illustrates that typed tags cannot be created directly from a string and shows the expected exception handling. ```php // This will throw CannotCreateTag try { $param = Param::create('string $name'); } catch (CannotCreateTag $e) { echo "Use ParamFactory instead"; } ``` -------------------------------- ### @throws Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/README.md DocBlock example demonstrating the @throws tag for documenting exceptions. ```php /** * @throws InvalidArgumentException If name is empty * @throws RuntimeException On error */ ``` -------------------------------- ### Param Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tags.md Example of how to retrieve and use @param tags from a DocBlock. ```php // From DocBlock $paramTags = $docblock->getTagsByName('param'); foreach ($paramTags as $param) { echo $param->getVariableName() . ": " . $param->getType(); } ``` -------------------------------- ### @property Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/README.md DocBlock example demonstrating the @property tag for documenting magic properties. ```php /** * @property string $name User name * @property-read int $id User ID (read-only) * @property-write string $password (write-only) */ ``` -------------------------------- ### Example: Complete Configuration Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/configuration.md Demonstrates a comprehensive configuration of the DocBlockFactory, including setting up a type context, registering multiple custom tags (both classes and factories), and parsing a DocBlock with full configuration. ```php // Create a context for type resolution $context = new \phpDocumentor\Reflection\Types\Context('App\Services'); // Register custom tags $customTags = [ 'api' => ApiTag::class, 'feature' => FeatureTagFactory::class, ]; // Create factory with custom tags $factory = \phpDocumentoror\Reflection\DocBlockFactory::createInstance($customTags); // Parse DocBlock with full configuration $docblock = $factory->create( $phpComment, $context, new Location(__FILE__, 42) ); // Access parsed data echo $docblock->getSummary(); foreach ($docblock->getTags() as $tag) { echo $tag->getName() . ": " . $tag; } ``` -------------------------------- ### getTagsByName() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Retrieves and processes tags by name, demonstrating how to get 'param' and 'return' tags. ```php $paramTags = $docblock->getTagsByName('param'); foreach ($paramTags as $param) { echo $param->getVariableName(); } $returnTags = $docblock->getTagsByName('return'); foreach ($returnTags as $return) { echo $return->getType(); } ``` -------------------------------- ### Example Usage of __toString() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/description.md Shows how the __toString() method is implicitly called when echoing the description. ```php $docblock = $factory->create($comment); echo $docblock->getDescription(); // Automatically calls __toString() ``` -------------------------------- ### String Representation Examples Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tag.md Provides examples of the expected string output for various tag types when using the __toString() method. ```string Param: "string $name" Return_: "string" Author: "John Doe " Deprecated: "1.0" Version: "2.0" ``` -------------------------------- ### @return Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/README.md DocBlock example demonstrating the @return tag for documenting return types. ```php /** * @return string The user's full name */ ``` -------------------------------- ### getType() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tagwithtype.md Demonstrates how to retrieve the type component of a tag. ```php $param = new Param('name', new String_()); $type = $param->getType(); echo (string) $type; // Outputs the type representation ``` -------------------------------- ### Factory Method Parameters Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/configuration.md Example of creating a DocBlockFactory instance with custom tag configuration and then using it to create a DocBlock object. ```php $factory = DocBlockFactory::createInstance([ 'mytag' => MyTag::class, ]); $docblock = $factory->create( $comment, $context, // Type context $location // File location ); ``` -------------------------------- ### Check for Specific Tags Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Check if a DocBlock contains a specific tag. ```php if ($docblock->hasTag('deprecated')) { echo "This element is deprecated"; } if ($docblock->hasTag('throws')) { echo "This method may throw exceptions"; } ``` -------------------------------- ### Namespaced Tags Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/configuration.md Illustrates the correct and incorrect ways to register namespaced custom tags with the DocBlockFactory. ```php // Correct $factory->registerTagHandler('\MyNamespace\CustomTag', Handler::class); // Incorrect - will throw InvalidArgumentException $factory->registerTagHandler('MyNamespace\CustomTag', Handler::class); ``` -------------------------------- ### Interpreting a Simple DocBlock Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/docs/how-to/interpreting-a-simple-docblock.rst This example shows how to parse a simple DocBlock and extract its summary and description using ReflectionDocBlock. ```php getDocBlock(); if ($docblock instanceof DocBlock) { echo \"Summary: \" . $docblock->getSummary() . \"\n\n\n\"; echo \"Description: \" . $docblock->getDescription() . \"\n\"; } ``` -------------------------------- ### @param Tag Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/README.md DocBlock example demonstrating the @param tag for documenting function/method parameters. ```php /** * @param string $name User name * @param int $age User age * @param string ...$tags Variable tags * @param string &$output Output parameter */ ``` -------------------------------- ### Example Usage of getTags() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/description.md Iterates through and displays the tags within a Description object. ```php $tags = $description->getTags(); foreach ($tags as $tag) { echo $tag->getName() . ": " . $tag; } ``` -------------------------------- ### Implement Custom Formatter Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/modules.md Example of implementing the Formatter interface for custom output. ```php class MyFormatter implements Formatter { public function format(Tag $tag): string { ... } } ``` -------------------------------- ### Type Conversion to String Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/types.md Illustrates how to get the string representation of a Type object. ```php $type = $param->getType(); $typeString = (string) $type; // e.g., "string", "MyClass", "array" ``` -------------------------------- ### getTags() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Iterates through all tags in a DocBlock and prints their names and string representations. ```php foreach ($docblock->getTags() as $tag) { echo $tag->getName() . ": " . $tag; } ``` -------------------------------- ### Example: __toString() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tag.md Demonstrates the usage of the __toString() magic method for direct string conversion of a tag object. ```php $param = new Param('name', new String_()); echo "Tag: " . $param; // Calls __toString() ``` -------------------------------- ### ParserException Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/errors.md Shows how to catch a ParserException when parsing of a DocBlock fails due to invalid syntax or structure. ```php use phpDocumentor\Reflection\Exception\ParserException; try { $docblock = $factory->create($comment); } catch (ParserException $e) { echo "Parser error: " . $e->getMessage(); // Handle invalid DocBlock syntax } ``` -------------------------------- ### PcreException Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/errors.md Illustrates catching a PcreException when a PCRE operation fails within the library. ```php use phpDocumentor\Reflection\Exception\PcreException; try { // This would be called internally by the library Utils::pregSplit('/pattern/', 'subject'); } catch (PcreException $e) { echo "Regex error: " . $e->getMessage(); } ``` -------------------------------- ### Work with Typed Tags Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Retrieve tags that have associated types, like '@param' or '@return'. ```php $paramTags = $docblock->getTagsWithTypeByName('param'); foreach ($paramTags as $param) { $type = $param->getType(); if ($type) { echo "Parameter type: " . (string) $type; } } ``` -------------------------------- ### Custom Tags Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/README.md Example of creating a DocBlockFactory with support for custom tags. ```php $factory = DocBlockFactory::createInstance([ 'custom' => MyCustomTag::class, ]); ``` -------------------------------- ### Resilient Parsing Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/errors.md Demonstrates how the library continues parsing even when some tags fail, allowing work with valid tags only. ```php // The library continues parsing even when some tags fail $docblock = $factory->create($comment); $validTags = []; foreach ($docblock->getTags() as $tag) { if (!($tag instanceof InvalidTag)) { $validTags[] = $tag; } } // Work with valid tags only ``` -------------------------------- ### Handling Invalid Tags Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Code to check for and handle invalid tags within a parsed DocBlock. ```php use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag; $tags = $docblock->getTags(); foreach ($tags as $tag) { if ($tag instanceof InvalidTag) { echo "Invalid tag: " . $tag->getName(); if ($exception = $tag->getException()) { echo "Error: " . $exception->getMessage(); } } else { // Process valid tag echo $tag->getName() . ": " . $tag; } } ``` -------------------------------- ### @property Tag Details Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract detailed information from '@property' tags, including the property name and type. ```php $propertyTags = $docblock->getTagsByName('property'); foreach ($propertyTags as $prop) { $name = $prop->getVariableName(); $type = $prop->getType(); echo "Magic property \$$name: $type"; } ``` -------------------------------- ### @var Tag Details Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract detailed information from '@var' tags, including the variable name and type. ```php $varTags = $docblock->getTagsByName('var'); foreach ($varTags as $var) { $name = $var->getVariableName(); $type = $var->getType(); echo "Variable \$$name: $type"; } ``` -------------------------------- ### @throws Tag Details Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract detailed information from '@throws' tags, including the exception type and description. ```php $throwsTags = $docblock->getTagsByName('throws'); foreach ($throwsTags as $throws) { $exceptionType = $throws->getType(); $description = $throws->getDescription(); echo "May throw: " . (string) $exceptionType; } ``` -------------------------------- ### @param Tag Details Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/GUIDE.md Extract detailed information from '@param' tags, including variable name, type, description, and modifiers. ```php $paramTags = $docblock->getTagsByName('param'); foreach ($paramTags as $param) { $name = $param->getVariableName(); $type = $param->getType(); $description = $param->getDescription(); $isVariadic = $param->isVariadic(); $isReference = $param->isReference(); echo "$name: $type (variadic: $isVariadic, reference: $isReference)"; } ``` -------------------------------- ### registerTagHandler() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblockfactory.md Registers a custom tag handler that will be used for parsing tags with the specified name. This allows you to: - Override built-in tag handlers - Add support for custom tags - Use PHPStan-compatible tag syntax with factory handlers Namespaced tag names must start with a backslash and be fully qualified (e.g., `\MyNamespace\CustomTag`). ```php // Register a custom tag class $factory->registerTagHandler('mytag', MyCustomTag::class); // Register a factory for complex tags $factory->registerTagHandler('mycomplex', new MyComplexTagFactory()); // Override a built-in tag $factory->registerTagHandler('param', CustomParamTag::class); ``` -------------------------------- ### Example: render() with default and custom formatter Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tag.md Shows how to render a tag using the default formatter and a custom formatter. ```php $param = new Param('name', new String_(), false, new Description('The name')); // With default formatting echo $param->render(); // With custom formatter $formatted = $param->render(new AlignFormatter()); ``` -------------------------------- ### Example: create() for simple and complex tags Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/tag.md Illustrates using the static create() method for simple tags and handling exceptions for complex tags that require factories. ```php // For simple tags $author = Author::create('John Doe '); if ($author) { echo $author->getAuthorName(); } // For complex tags // These typically throw CannotCreateTag - use factories instead try { $param = Param::create('string $name'); } catch (CannotCreateTag $e) { echo "Use factory instead"; } ``` -------------------------------- ### createInstance() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblockfactory.md Creates a new DocBlockFactory with all standard tag handlers registered. The factory is pre-configured with handlers for all PHPDoc standard tags including @param, @return, @throws, @property, @method, and many others. Additional custom tag handlers can be registered by passing them in the $additionalTags parameter. ```php $factory = DocBlockFactory::createInstance(); // Or with custom tags $factory = DocBlockFactory::createInstance([ 'mycustom' => MyCustomTagClass::class, ]); ``` -------------------------------- ### Basic Instantiation Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/configuration.md Creates a factory with all standard PHPDoc tags pre-configured. ```php $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); ``` -------------------------------- ### InvalidArgumentException Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/errors.md Example of catching an InvalidArgumentException when creating a DocBlock. ```php try { $docblock = $factory->create($invalidObject); } catch (InvalidArgumentException $e) { echo "Invalid argument: " . $e->getMessage(); } ``` -------------------------------- ### Usage of Type Objects Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/types.md Demonstrates how to create a DocBlock, retrieve parameter types, and convert them to string representations. ```php $factory = DocBlockFactory::createInstance(); $docblock = $factory->create(<<<'DOC' /** * @param string $name * @param int $count * @return array */ DOC); $params = $docblock->getTagsByName('param'); foreach ($params as $param) { $type = $param->getType(); // Type object echo (string) $type; // "string", "int", etc. } ``` -------------------------------- ### Instantiate DocBlockFactory Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/README.md Create an instance of the DocBlockFactory. ```php $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); ``` -------------------------------- ### Example for registerTagHandler() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/standardtagfactory.md Example usage of the registerTagHandler method for custom tag handlers. ```php $factory = StandardTagFactory::createInstance($fqsenResolver); // Register a simple tag class $factory->registerTagHandler('custom', MyCustomTag::class); // Register a factory for complex parsing $factory->registerTagHandler('complex', new MyComplexTagFactory()); // Override built-in handler $factory->registerTagHandler('param', CustomParamHandler::class); // Namespaced custom tags $factory->registerTagHandler('\App\Tags\Special', SpecialTag::class); ``` -------------------------------- ### create() Example Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblockfactory.md Parses a DocBlock string or extracts and parses a DocBlock from a ReflectionClass/ReflectionMethod/ReflectionFunction. The factory: 1. Strips comment delimiters (/** and */) 2. Splits the content into summary, description, and tags 3. Creates a Description object with inline tags 4. Parses all individual tags 5. Detects template markers (#@+ and #@-) A Context object can be provided to help resolve relative type names. Without a context, names are assumed to be fully qualified. ```php // Parse from string $docComment = <<create($docComment); // Or parse from a Reflection object $reflection = new ReflectionClass('MyClass'); $docblock = $factory->create($reflection); // With context for type resolution $context = new \phpDocumentor\Reflection\Types\Context('MyNamespace'); $docblock = $factory->create($docComment, $context); ``` -------------------------------- ### Dependency Injection Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/patterns.md Inject the factory as a dependency. ```php class DocBlockParser { public function __construct(private DocBlockFactory $factory) {} public function parse(string $comment): DocBlock { return $this->factory->create($comment); } } // Configuration $container->set(DocBlockFactory::class, function() { return DocBlockFactory::createInstance(); }); ``` -------------------------------- ### Context Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/INDEX.md Namespace context for type resolution. ```php $context = new Context('MyNamespace'); $docblock = $factory->create($comment, $context); ``` -------------------------------- ### InvalidTag Example DocBlock Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/errors.md An example DocBlock containing an invalid tag, illustrating how InvalidTag is used. ```php /** * @invalid malformed tag body */ ``` -------------------------------- ### Singleton Pattern Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/patterns.md Create the factory once and reuse throughout your application. ```php class DocBlockService { private static ?DocBlockFactory $factory = null; public static function getFactory(): DocBlockFactory { if (self::$factory === null) { self::$factory = DocBlockFactory::createInstance(); } return self::$factory; } } // Usage $factory = DocBlockService::getFactory(); $docblock1 = $factory->create($comment1); $docblock2 = $factory->create($comment2); ``` -------------------------------- ### Method Documentation Extraction Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/patterns.md This class demonstrates how to extract detailed documentation information from a ReflectionMethod, including parameters, return types, and exceptions. ```php class MethodDocumentationExtractor { public function extractFromReflection(ReflectionMethod $method): array { $docblock = $this->factory->create($method); return [ 'method' => $method->getName(), 'static' => $method->isStatic(), 'visibility' => $this->getVisibility($method), 'documentation' => $this->extractDocumentation($docblock), 'parameters' => $this->matchParametersWithDocumentation( $method->getParameters(), $docblock->getTagsByName('param') ), 'return' => $this->extractReturn($docblock), 'throws' => $this->extractThrows($docblock), ]; } private function matchParametersWithDocumentation( array $reflectionParams, array $paramTags ): array { $documentedParams = []; foreach ($paramTags as $paramTag) { $documentedParams[$paramTag->getVariableName()] = $paramTag; } return array_map(function(ReflectionParameter $param) use ($documentedParams) { $name = $param->getName(); $documented = $documentedParams[$name] ?? null; return [ 'name' => $name, 'type' => $this->getParameterType($param, $documented), 'optional' => $param->isOptional(), 'default' => $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, 'description' => $documented ? (string) $documented->getDescription() : null, ]; }, $reflectionParams); } private function getParameterType( ReflectionParameter $param, ?Param $documented ): ?string { if ($documented && $documented->getType()) { return (string) $documented->getType(); } $type = $param->getType(); if ($type) { return (string) $type; } return null; } } ``` -------------------------------- ### Factory Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/INDEX.md Creates DocBlock objects from strings or Reflection objects. ```php $factory = DocBlockFactory::createInstance(); $docblock = $factory->create($comment); ``` -------------------------------- ### getSummary() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Method to get the summary of the DocBlock. ```php public function getSummary(): string ``` -------------------------------- ### Pattern: Process Specific Tags Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/patterns.md Demonstrates different ways to process specific tags. ```php // Method 1: Get by name $paramTags = $docblock->getTagsByName('param'); foreach ($paramTags as $param) { // Process @param tags } // Method 2: Check existence first if ($docblock->hasTag('deprecated')) { $deprecated = $docblock->getTagsByName('deprecated')[0]; // Process deprecated } // Method 3: Get typed tags only $typedParams = $docblock->getTagsWithTypeByName('param'); foreach ($typedParams as $param) { if ($param->getType()) { // Process typed parameter } } ``` -------------------------------- ### getContext() Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/api-reference/docblock.md Method to get the namespace context of the DocBlock. ```php public function getContext(): ?Types\Context ``` -------------------------------- ### Creating a Context Source: https://github.com/phpdocumentor/reflectiondocblock/blob/6.x/_autodocs/configuration.md Instantiate a Context object for type resolution. ```php $context = new \phpDocumentor\Reflection\Types\Context('MyNamespace'); ```