frzb/php-doc-reader

PhpDocReader 解析 @var 和 @param 类型

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

This package is auto-updated.

Last update: 2024-08-26 02:32:07 UTC


README

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

特性

PhpDocReader 解析 PHP DocBlock 中的 @var@param

use My\Cache\Backend;

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

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

它支持使用与 PHP 相同的解析规则进行命名空间类名

  • 完全限定名称(以 \ 开头)
  • 导入类名(例如,use My\Cache\Backend;
  • 相对类名(从当前命名空间开始,如 SubNamespace\MyClass
  • 别名类名(例如,use My\Cache\Backend as FooBar;

原始类型(如 @var string)将被忽略(返回 null),只返回有效的类名。

用法

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