Skip to content

Commit 9672ba0

Browse files
committed
Improve packages readme
1 parent 4ed6587 commit 9672ba0

1 file changed

Lines changed: 33 additions & 56 deletions

File tree

README.md

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,88 +14,65 @@
1414

1515
---
1616

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.
1919

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).
2125

2226
## Installation
2327

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):
2629

2730
```sh
2831
composer require type-lang/reader
2932
```
3033

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:
3241

3342
```php
34-
$reader = new \TypeLang\Reader\ReflectionReader();
43+
$reader = new TypeLang\Reader\ReflectionReader();
3544

3645
$node = $reader->findFunctionType(
37-
function: new \ReflectionFunction(function(): void {}),
46+
new ReflectionFunction(function (): void {}),
3847
);
3948

4049
var_dump($node);
50+
// object(TypeLang\Type\NamedTypeNode) {
51+
// ["name"] => "void"
52+
// ...
53+
// }
4154
```
4255

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:
6258

6359
```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();
6563

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.
8064
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";
8467
}
8568
}
8669

87-
// Dump all methods with its types.
8870
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";
9973
}
10074
}
10175
```
76+
77+
Constants (`findConstantType()`) and parameters (`findParameterType()`) are read
78+
the same way.

0 commit comments

Comments
 (0)