frzb/php-doc-reader

PhpDocReader parses @var and @param types

v2.0.0 2023-12-26 01:12 UTC

This package is auto-updated.

Last update: 2024-11-26 03:05:52 UTC


README

CI Bugs Security Rating Maintainability Rating Code Smells Lines of Code Coverage Technical Debt Reliability Rating Duplicated Lines (%) Vulnerabilities

Features

PhpDocReader parses @var and @param values in PHP DocBlock:

use My\Cache\Backend;

class Cache
{
    /**
     * @var Backend
     */
    protected $backend;

    /**
     * @param Backend $backend
     */
    public function __construct($backend)
    {
    }
}

It supports namespaced class names with the same resolution rules as PHP:

  • fully qualified name (starting with \)
  • imported class name (eg. use My\Cache\Backend;)
  • relative class name (from the current namespace, like SubNamespace\MyClass)
  • aliased class name (eg. use My\Cache\Backend as FooBar;)

Primitive types (@var string) are ignored (returns null), only valid class names are returned.

Usage

$reader = new Reader();

// Read a property type (@var phpdoc)
$property = new ReflectionProperty($className, $propertyName);
$propertyClass = $reader->getPropertyClass($property);

// Read a parameter type (@param phpdoc)
$parameter = new ReflectionParameter([$className, $methodName], $parameterName);
$parameterClass = $reader->getParameterClass($parameter);