src
7 years ago
.gitattributes
7 years ago
.gitignore
7 years ago
LICENSE
7 years ago
README.md
7 years ago
composer.json
7 years ago
README.md
59 lines
| 1 | # PhpDocReader |
| 2 | |
| 3 | [](https://travis-ci.org/mnapoli/PhpDocReader](https://travis-ci.org/mnapoli/PhpDocReader](https://travis-ci.org/mnapoli/PhpDocReader) |
| 4 |  |
| 5 | |
| 6 | This project is used by: |
| 7 | |
| 8 | - [](http://php-di.org/PHP-DI](http://php-di.org/](http://php-di.org/) |
| 9 | - [](https://github.com/balihoo/phockito-unit-php-diphockito-unit-php-di](https://github.com/balihoo/phockito-unit-php-di](https://github.com/balihoo/phockito-unit-php-di) |
| 10 | |
| 11 | Fork the README to add your project here. |
| 12 | |
| 13 | ## Features |
| 14 | |
| 15 | PhpDocReader parses `@var` and `@param` values in PHP docblocks: |
| 16 | |
| 17 | ```php |
| 18 | |
| 19 | use My\Cache\Backend; |
| 20 | |
| 21 | class Cache |
| 22 | { |
| 23 | /** |
| 24 | * @var Backend |
| 25 | */ |
| 26 | protected $backend; |
| 27 | |
| 28 | /** |
| 29 | * @param Backend $backend |
| 30 | */ |
| 31 | public function __construct($backend) |
| 32 | { |
| 33 | } |
| 34 | } |
| 35 | ``` |
| 36 | |
| 37 | It supports namespaced class names with the same resolution rules as PHP: |
| 38 | |
| 39 | - fully qualified name (starting with `\`) |
| 40 | - imported class name (eg. `use My\Cache\Backend;`) |
| 41 | - relative class name (from the current namespace, like `SubNamespace\MyClass`) |
| 42 | - aliased class name (eg. `use My\Cache\Backend as FooBar;`) |
| 43 | |
| 44 | Primitive types (`@var string`) are ignored (returns null), only valid class names are returned. |
| 45 | |
| 46 | ## Usage |
| 47 | |
| 48 | ```php |
| 49 | $reader = new PhpDocReader(); |
| 50 | |
| 51 | // Read a property type (@var phpdoc) |
| 52 | $property = new ReflectionProperty($className, $propertyName); |
| 53 | $propertyClass = $reader->getPropertyClass($property); |
| 54 | |
| 55 | // Read a parameter type (@param phpdoc) |
| 56 | $parameter = new ReflectionParameter(array($className, $methodName), $parameterName); |
| 57 | $parameterClass = $reader->getParameterClass($parameter); |
| 58 | ``` |
| 59 |