ReflectionDocBlock Documentation

repository·6.x·Indexed 27 days ago

https://github.com/phpdocumentor/reflectiondocblock

A phpDocumentor component providing a PHPDoc-compatible DocBlock parser for PHP. It enables libraries to extract metadata, type information, and annotations, as well as programmatically reconstitute or modify DocBlocks. Key features include the DocBlockFactory for parsing, DescriptionFactory for handling text content, and a Serializer for converting DocBlock objects back into DocComment strings.

Tokens
2.8K
Snippets
5
Records
22
Agent score
93%

What's inside ReflectionDocBlock

  1. Overview of ReflectionDocBlock

    6.x

    ReflectionDocBlock is a PHP library designed to parse and interpret DocBlocks that are fully compatible with the PHPDoc standard. It is used for extracting metadata, type information, and annotations from PHP code.

    Key capabilities include:

    • Metadata Extraction: Gathering type information for static analysis or code introspection.
    • DocBlock Reconstitution: Programmatically adding, removing, or modifying DocBlock tags.
    • Serialization Support: Helping tools interpret type information in complex structures.
    • Documentation Generation: Serving as a core component for generating API documentation.
  2. Quick Start with ReflectionDocBlock

    6.x
    To use ReflectionDocBlock in your project, you can parse a DocBlock string to interpret its contents. While the specific implementation details depend on the classes used, the library is designed to provide a simple and intuitive API for interacting with PHPDoc metadata.
  3. Parse DocBlocks using DocBlockFactory

    6.x

    To parse a DocBlock, instantiate a DocBlockFactory using the createInstance() method. You can then use the create() method to interpret a raw DocBlock string or an object that implements getDocComment() (such as a ReflectionClass). The create() method returns an instance of \phpDocumentor\Reflection\DocBlock.

    $factory  = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
    
    $docComment = <<<DOCCOMMENT
    /**
     * This is an example of a summary.
     *
     * This is a Description. A Summary and Description are separated by either
     * two subsequent newlines (thus a whiteline in between as can be seen in this
     * example), or when the Summary ends with a dot (`.`) and some form of
     * whitespace.
     */
    DOCCOMMENT;
    
    $docblock = $factory->create($docComment);
  4. Migrate from `::create` static methods to Tag Factories

    6.x

    In v6, the ::create static method has been removed from tag classes that represent type definitions (e.g., @param and @return tags).

    If you were instantiating these tag objects directly, you must now use a tag factory or the recommended construction pattern.

    // Before v6
    $tag = Param::create($body);
    
    // After v6
    $factory = \phpDocumentor\Reflection\DocBlock\Tags\Factory\StandardTagFactory::createInstance();
    $tag = $factory->create('@param int $foo');