|
| 1 | +<a href="https://github.com/php-type-language" target="_blank"> |
| 2 | + <picture> |
| 3 | + <img align="center" src="https://github.com/php-type-language/.github/blob/master/assets/dark.png?raw=true"> |
| 4 | + </picture> |
| 5 | +</a> |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + <a href="https://packagist.org/packages/type-lang/reflection-converter"><img src="https://poser.pugx.org/type-lang/reflection-converter/require/php?style=for-the-badge" alt="PHP 8.1+"></a> |
| 11 | + <a href="https://packagist.org/packages/type-lang/reflection-converter"><img src="https://poser.pugx.org/type-lang/reflection-converter/version?style=for-the-badge" alt="Latest Stable Version"></a> |
| 12 | + <a href="https://packagist.org/packages/type-lang/reflection-converter"><img src="https://poser.pugx.org/type-lang/reflection-converter/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a> |
| 13 | + <a href="https://raw.githubusercontent.com/php-type-language/reflection-converter/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/reflection-converter/license?style=for-the-badge" alt="License MIT"></a> |
| 14 | +</p> |
| 15 | +<p align="center"> |
| 16 | + <a href="https://github.com/php-type-language/reflection-converter/actions"><img src="https://github.com/php-type-language/reflection-converter/workflows/tests/badge.svg"></a> |
| 17 | +</p> |
| 18 | + |
| 19 | +Provides a set of methods for converting PHP reflection objects into the |
| 20 | +Type Language AST nodes. |
| 21 | + |
| 22 | +Read [documentation pages](https://phpdoc.io) for more information. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +Type Language Reflection Converter is available as Composer repository and can |
| 27 | +be installed using the following command in a root of your project: |
| 28 | + |
| 29 | +```sh |
| 30 | +$ composer require type-lang/reflection-converter |
| 31 | +``` |
| 32 | + |
| 33 | +## Quick Start |
| 34 | + |
| 35 | +```php |
| 36 | +// Type contains ReflectionNamedType{ name: "void" } |
| 37 | +$function = new \ReflectionFunction(function(): void {}); |
| 38 | +$type = $function->getReturnType(); |
| 39 | + |
| 40 | +$converter = new \TypeLang\ReflectionConverter\Converter(); |
| 41 | +$node = $converter->convert($type); |
| 42 | + |
| 43 | +var_dump($node); |
| 44 | +``` |
| 45 | + |
| 46 | +**Expected Output:** |
| 47 | +``` |
| 48 | +TypeLang\Parser\Node\Stmt\NamedTypeNode { |
| 49 | + +offset: 0 |
| 50 | + +name: TypeLang\Parser\Node\Name { |
| 51 | + +offset: 0 |
| 52 | + -parts: array:1 [ |
| 53 | + 0 => TypeLang\Parser\Node\Identifier { |
| 54 | + +offset: 0 |
| 55 | + +value: "void" |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + +arguments: null |
| 60 | + +fields: null |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Creating From Reflection |
| 65 | + |
| 66 | +```php |
| 67 | +$class = new \ReflectionClass(Path\To\Example::class); |
| 68 | + |
| 69 | +// Printer component provided by "type-lang/printer" package. |
| 70 | +$printer = new \TypeLang\Printer\PrettyPrinter(); |
| 71 | + |
| 72 | +$converter = new \TypeLang\ReflectionConverter\Converter(); |
| 73 | + |
| 74 | +// Dump all constants with its types. |
| 75 | +foreach ($class->getReflectionConstants() as $constant) { |
| 76 | + // Creates type node AST from a constant's type. |
| 77 | + if ($type = $converter->convertConstantType($constant)); { |
| 78 | + echo 'const ' . $constant->name . ' has type ' . $printer->print($type) . "\n"; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Dump all properties with its types. |
| 83 | +foreach ($class->getProperties() as $property) { |
| 84 | + // Creates type node AST from a property's type. |
| 85 | + if ($type = $converter->convertPropertyType($property)); { |
| 86 | + echo 'property ' . $property->name . ' has type ' . $printer->print($type) . "\n"; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Dump all methods with its types. |
| 91 | +foreach ($class->getMethods() as $method) { |
| 92 | + // Creates type node AST from any function's return type. |
| 93 | + if ($type = $converter->convertFunctionType($method)); { |
| 94 | + echo 'function ' . $method->name . ' has type ' . $printer->print($type) . "\n"; |
| 95 | + } |
| 96 | + |
| 97 | + // Creates type node AST from a parameter's type. |
| 98 | + foreach ($method->getParameters() as $parameter) { |
| 99 | + if ($type = $converter->convertParameterType($parameter)); { |
| 100 | + echo 'parameter ' . $parameter->name . ' has type ' . $printer->print($type) . "\n"; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments