frzb / php-doc-reader
PhpDocReader parses @var and @param types
v2.0.0
2023-12-26 01:12 UTC
Requires
- php: >=8.1
- doctrine/annotations: ^1.14
- fp4php/functional: ^6.0
- friendsofphp/php-cs-fixer: ^3.42
- frzb/dependency-injection: ^2.0
- symfony/framework-bundle: ^6.0|^7.0
Requires (Dev)
- dg/bypass-finals: ^1.5
- mnapoli/hard-mode: ~0.3.0
- php-coveralls/php-coveralls: ^2.7
- phpunit/php-code-coverage: ^10.1
- phpunit/phpunit: ^10.5
- symfony/phpunit-bridge: ^6.0|^7.0
- symfony/test-pack: ^1.0
README
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);