|
14 | 14 |
|
15 | 15 | --- |
16 | 16 |
|
17 | | -Provides a set of methods for converting PHP Reflection objects into the |
18 | | -TypeLang AST Nodes. |
| 17 | +Converts native PHP types exposed by Reflection objects into **TypeLang** |
| 18 | +`TypeLang\Type\*` AST nodes. |
19 | 19 |
|
20 | | -Read [documentation pages](https://typelang.dev) for more information. |
| 20 | +Given a `ReflectionClassConstant`, `ReflectionProperty`, `ReflectionParameter` |
| 21 | +or `ReflectionFunctionAbstract`, it returns the matching type node (or `null` |
| 22 | +when no type is declared). |
| 23 | + |
| 24 | +Full documentation is available at [typelang.dev](https://typelang.dev). |
21 | 25 |
|
22 | 26 | ## Installation |
23 | 27 |
|
24 | | -TypeLang Reader is available as Composer repository and can |
25 | | -be installed using the following command in a root of your project: |
| 28 | +Install the package via [Composer](https://getcomposer.org): |
26 | 29 |
|
27 | 30 | ```sh |
28 | 31 | composer require type-lang/reader |
29 | 32 | ``` |
30 | 33 |
|
31 | | -## Quick Start |
| 34 | +**Requirements:** |
| 35 | +- PHP 8.4+ |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +`TypeLang\Reader\ReflectionReader` exposes a `find*Type()` method for every kind |
| 40 | +of Reflection object: |
32 | 41 |
|
33 | 42 | ```php |
34 | | -$reader = new \TypeLang\Reader\ReflectionReader(); |
| 43 | +$reader = new TypeLang\Reader\ReflectionReader(); |
35 | 44 |
|
36 | 45 | $node = $reader->findFunctionType( |
37 | | - function: new \ReflectionFunction(function(): void {}), |
| 46 | + new ReflectionFunction(function (): void {}), |
38 | 47 | ); |
39 | 48 |
|
40 | 49 | var_dump($node); |
| 50 | +// object(TypeLang\Type\NamedTypeNode) { |
| 51 | +// ["name"] => "void" |
| 52 | +// ... |
| 53 | +// } |
41 | 54 | ``` |
42 | 55 |
|
43 | | -**Expected Output:** |
44 | | -``` |
45 | | -TypeLang\Parser\Node\Stmt\NamedTypeNode { |
46 | | - +offset: 0 |
47 | | - +name: TypeLang\Parser\Node\Name { |
48 | | - +offset: 0 |
49 | | - -parts: array:1 [ |
50 | | - 0 => TypeLang\Parser\Node\Identifier { |
51 | | - +offset: 0 |
52 | | - +value: "void" |
53 | | - } |
54 | | - ] |
55 | | - } |
56 | | - +arguments: null |
57 | | - +fields: null |
58 | | -} |
59 | | -``` |
60 | | - |
61 | | -### Creating From Reflection |
| 56 | +Combine it with [`type-lang/printer`](https://packagist.org/packages/type-lang/printer) |
| 57 | +to dump the resolved types of a whole class: |
62 | 58 |
|
63 | 59 | ```php |
64 | | -$class = new \ReflectionClass(Path\To\Example::class); |
| 60 | +$class = new ReflectionClass(Path\To\Example::class); |
| 61 | +$reader = new TypeLang\Reader\ReflectionReader(); |
| 62 | +$printer = new TypeLang\Printer\PrettyTypePrinter(); |
65 | 63 |
|
66 | | -// Printer component provided by "type-lang/printer" package. |
67 | | -$printer = new \TypeLang\Printer\PrettyTypePrinter(); |
68 | | - |
69 | | -$converter = new \TypeLang\Reader\ReflectionReader(); |
70 | | - |
71 | | -// Dump all constants with its types. |
72 | | -foreach ($class->getReflectionConstants() as $constant) { |
73 | | - // Creates type node AST from a constant's type. |
74 | | - if ($type = $converter->findConstantType($constant)) { |
75 | | - echo 'const ' . $constant->name . ' has type ' . $printer->print($type) . "\n"; |
76 | | - } |
77 | | -} |
78 | | - |
79 | | -// Dump all properties with its types. |
80 | 64 | foreach ($class->getProperties() as $property) { |
81 | | - // Creates type node AST from a property's type. |
82 | | - if ($type = $converter->findPropertyType($property)) { |
83 | | - echo 'property ' . $property->name . ' has type ' . $printer->print($type) . "\n"; |
| 65 | + if ($type = $reader->findPropertyType($property)) { |
| 66 | + echo "property {$property->name}: {$printer->print($type)}\n"; |
84 | 67 | } |
85 | 68 | } |
86 | 69 |
|
87 | | -// Dump all methods with its types. |
88 | 70 | foreach ($class->getMethods() as $method) { |
89 | | - // Creates type node AST from any function's return type. |
90 | | - if ($type = $converter->findFunctionType($method)) { |
91 | | - echo 'function ' . $method->name . ' has type ' . $printer->print($type) . "\n"; |
92 | | - } |
93 | | - |
94 | | - // Creates type node AST from a parameter's type. |
95 | | - foreach ($method->getParameters() as $parameter) { |
96 | | - if ($type = $converter->findParameterType($parameter)) { |
97 | | - echo 'parameter ' . $parameter->name . ' has type ' . $printer->print($type) . "\n"; |
98 | | - } |
| 71 | + if ($type = $reader->findFunctionType($method)) { |
| 72 | + echo "method {$method->name}(): {$printer->print($type)}\n"; |
99 | 73 | } |
100 | 74 | } |
101 | 75 | ``` |
| 76 | + |
| 77 | +Constants (`findConstantType()`) and parameters (`findParameterType()`) are read |
| 78 | +the same way. |
0 commit comments