Convert Figma logo to code with AI

phpDocumentor logoReflectionDocBlock

No description available

9,335
117
9,335
11

Top Related Projects

A PHP parser written in PHP

:crystal_ball: Better Reflection is a reflection API that aims to improve and provide more features than PHP's built-in reflection API.

Annotations Docblock Parser

Metadata is a library for metadata management in PHP

Quick Overview

phpDocumentor/ReflectionDocBlock is a PHP library that provides a way to parse and inspect DocBlocks in PHP code. It offers a robust API for extracting information from DocBlocks, including tags, descriptions, and other metadata. This library is particularly useful for developers working on documentation tools, static analysis, or code generation projects.

Pros

  • Comprehensive parsing of DocBlock elements, including complex tags and annotations
  • Well-maintained and actively developed, with regular updates and improvements
  • Integrates seamlessly with other phpDocumentor projects and tools
  • Provides a flexible and extensible API for custom DocBlock parsing needs

Cons

  • Learning curve can be steep for developers unfamiliar with reflection concepts
  • Performance may be impacted when parsing large codebases with many DocBlocks
  • Requires PHP 7.2 or higher, which may limit compatibility with older projects

Code Examples

  1. Parsing a simple DocBlock:
use phpDocumentor\Reflection\DocBlockFactory;

$docblock = <<<'DOCBLOCK'
/**
 * This is a short description.
 *
 * This is a long description.
 *
 * @param string $param1 Description of param1
 * @return int
 */
DOCBLOCK;

$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($docblock);

echo $docBlock->getSummary(); // Outputs: This is a short description.
echo $docBlock->getDescription(); // Outputs: This is a long description.
  1. Extracting tags from a DocBlock:
use phpDocumentor\Reflection\DocBlockFactory;

$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create('/** @param string $name The person\'s name */');

$params = $docBlock->getTagsByName('param');
foreach ($params as $param) {
    echo $param->getVariableName(); // Outputs: name
    echo $param->getType(); // Outputs: string
    echo $param->getDescription(); // Outputs: The person's name
}
  1. Creating a custom tag:
use phpDocumentor\Reflection\DocBlock\Tags\BaseTag;
use phpDocumentor\Reflection\DocBlock\Description;

class CustomTag extends BaseTag
{
    public function __construct($name, Description $description = null)
    {
        $this->name = $name;
        $this->description = $description;
    }

    public static function create($body)
    {
        return new static('custom', new Description($body));
    }

    public function __toString()
    {
        return $this->name . ' ' . $this->description;
    }
}

Getting Started

To use phpDocumentor/ReflectionDocBlock in your project:

  1. Install via Composer:

    composer require phpdocumentor/reflection-docblock
    
  2. Basic usage in your PHP code:

    use phpDocumentor\Reflection\DocBlockFactory;
    
    $factory = DocBlockFactory::createInstance();
    $docBlock = $factory->create('/** @var string $example This is an example */');
    
    echo $docBlock->getSummary();
    echo $docBlock->getDescription();
    

For more advanced usage and configuration options, refer to the official documentation on the project's GitHub repository.

Competitor Comparisons

A PHP parser written in PHP

Pros of PHP-Parser

  • More comprehensive parsing capabilities, handling full PHP syntax
  • Generates an Abstract Syntax Tree (AST) for deeper code analysis
  • Supports parsing and modifying PHP code programmatically

Cons of PHP-Parser

  • Steeper learning curve due to its complexity
  • Overkill for simple docblock parsing tasks
  • Requires more setup and configuration for basic use cases

Code Comparison

ReflectionDocBlock:

$docblock = new DocBlock('/** @param string $foo */');
$tags = $docblock->getTags();
echo $tags[0]->getVariableName(); // Outputs: foo

PHP-Parser:

$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse('<?php function foo($bar) {}');
echo $ast[0]->params[0]->var->name; // Outputs: bar

Summary

ReflectionDocBlock is focused on parsing and manipulating docblocks, making it simpler for specific documentation-related tasks. PHP-Parser offers a more powerful and flexible solution for parsing entire PHP files and manipulating code structure, but comes with increased complexity. Choose ReflectionDocBlock for straightforward docblock handling, and PHP-Parser for comprehensive PHP code analysis and manipulation.

:crystal_ball: Better Reflection is a reflection API that aims to improve and provide more features than PHP's built-in reflection API.

Pros of BetterReflection

  • More comprehensive reflection capabilities, including parsing code without loading it
  • Ability to analyze code statically, useful for static analysis tools
  • Support for reflecting on code that might not be directly loadable

Cons of BetterReflection

  • Potentially slower performance due to its more extensive analysis
  • Larger library size and more dependencies
  • Steeper learning curve due to its more complex API

Code Comparison

ReflectionDocBlock:

$docBlock = new DocBlock('/** @var string $foo */');
$tags = $docBlock->getTags();

BetterReflection:

$reflector = new DefaultReflector(new AstLocator());
$class = $reflector->reflectClass('MyClass');
$method = $class->getMethod('myMethod');
$docBlock = $method->getDocComment();

BetterReflection offers more powerful reflection capabilities but requires more setup and has a more complex API. ReflectionDocBlock is simpler and focuses specifically on DocBlock parsing, making it easier to use for basic docblock-related tasks. The choice between them depends on the specific requirements of your project and the depth of reflection needed.

Annotations Docblock Parser

Pros of Annotations

  • More feature-rich and versatile for handling various types of annotations
  • Better integration with Doctrine ORM and other Doctrine projects
  • Actively maintained with regular updates and improvements

Cons of Annotations

  • Heavier and more complex, which may be overkill for simpler projects
  • Steeper learning curve due to its extensive feature set
  • Requires additional configuration and setup compared to ReflectionDocBlock

Code Comparison

ReflectionDocBlock:

use phpDocumentor\Reflection\DocBlockFactory;

$docblock = '/**
 * @var string
 */';
$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($docblock);

Annotations:

use Doctrine\Common\Annotations\AnnotationReader;

/**
 * @Entity
 */
class MyEntity {
    /** @Column(type="string") */
    private $name;
}

$reader = new AnnotationReader();
$reflClass = new ReflectionClass(MyEntity::class);
$annotations = $reader->getClassAnnotations($reflClass);

Summary

ReflectionDocBlock is simpler and more lightweight, focusing primarily on parsing DocBlocks. Annotations offers a more comprehensive solution for working with annotations, particularly in Doctrine-based projects. The choice between the two depends on the specific requirements of your project and the level of annotation functionality needed.

Metadata is a library for metadata management in PHP

Pros of Metadata

  • More flexible and extensible metadata handling system
  • Supports various metadata drivers (annotations, YAML, XML, PHP)
  • Better performance due to caching mechanisms

Cons of Metadata

  • Steeper learning curve for beginners
  • Less focused on DocBlock parsing specifically
  • Requires additional configuration for different metadata drivers

Code Comparison

ReflectionDocBlock:

$docBlock = new DocBlock('/** @var string $foo */');
$tags = $docBlock->getTags();
$varTag = $tags[0];
echo $varTag->getVariableName(); // Output: foo

Metadata:

$driver = new AnnotationDriver([new AnnotationReader()]);
$metadata = new MetadataFactory($driver);
$classMetadata = $metadata->getMetadataForClass(MyClass::class);
$propertyMetadata = $classMetadata->propertyMetadata['foo'];

Summary

ReflectionDocBlock is more focused on parsing DocBlocks and extracting information from them, while Metadata provides a more comprehensive solution for handling various types of metadata across different sources. ReflectionDocBlock is simpler to use for basic DocBlock parsing, but Metadata offers more flexibility and features for complex metadata management in larger projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

License: MIT Qa workflow Scrutinizer Code Coverage Scrutinizer Code Quality Stable Version Unstable Version

ReflectionDocBlock

Introduction

The ReflectionDocBlock component of phpDocumentor provides a DocBlock parser that is 100% compatible with the PHPDoc standard.

With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.

Installation

composer require phpdocumentor/reflection-docblock

Usage

In order to parse the DocBlock one needs a DocBlockFactory that can be instantiated using its createInstance factory method like this:

$factory  = \phpDocumentor\Reflection\DocBlockFactory::createInstance();

Then we can use the create method of the factory to interpret the DocBlock. Please note that it is also possible to provide a class that has the getDocComment() method, such as an object of type ReflectionClass, the create method will read that if it exists.

$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);

The create method will yield an object of type \phpDocumentor\Reflection\DocBlock whose methods can be queried:

// Contains the summary for this DocBlock
$summary = $docblock->getSummary();

// Contains \phpDocumentor\Reflection\DocBlock\Description object
$description = $docblock->getDescription();

// You can either cast it to string
$description = (string) $docblock->getDescription();

// Or use the render method to get a string representation of the Description.
$description = $docblock->getDescription()->render();

For more examples it would be best to review the scripts in the /examples folder.