Skip to content

Commit f1ddda6

Browse files
phpstan-botclaude
authored andcommitted
Add missing feature descriptions to README
Document features that exist in the codebase but were not described in the README: supported type syntax (union, intersection, generic, conditional, array/object shapes, callable, offset access), constant expression parsing, AST node traversal with visitor pattern, and node attributes configuration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 53945a7 commit f1ddda6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,66 @@ For the complete list of supported PHPDoc features check out PHPStan documentati
1717

1818
This parser also supports parsing [Doctrine Annotations](https://github.com/doctrine/annotations). The AST nodes live in the [PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine namespace](https://phpstan.github.io/phpdoc-parser/2.1.x/namespace-PHPStan.PhpDocParser.Ast.PhpDoc.Doctrine.html).
1919

20+
## Features
21+
22+
### Supported type syntax
23+
24+
The parser supports a rich type system including:
25+
26+
- Basic types: `string`, `int`, `bool`, `null`, `self`, `static`, `$this`, etc.
27+
- Nullable types: `?string`
28+
- Union and intersection types: `string|int`, `Foo&Bar`
29+
- Generic types with variance: `array<string>`, `Collection<covariant T>`
30+
- Array shapes: `array{name: string, age: int, ...}`
31+
- Object shapes: `object{name: string, age: int}`
32+
- Callable/closure types: `callable(string): bool`, `Closure(int): void`
33+
- Conditional types: `($input is string ? string : int)`
34+
- Offset access types: `T[K]`
35+
- Constant type expressions: `self::CONST*`, `123`, `'string'`
36+
37+
### Constant expression parsing
38+
39+
Constant expressions used in PHPDoc tags are parsed via `ConstExprParser`:
40+
41+
- Scalar values: integers, floats, strings, `true`, `false`, `null`
42+
- Arrays: `{1, 2, 'key' => 'value'}`
43+
- Class constant fetches: `ClassName::CONSTANT`
44+
45+
### AST node traversal
46+
47+
The library provides a visitor-based traversal system (inspired by [nikic/PHP-Parser](https://github.com/nikic/PHP-Parser)) for reading and transforming the AST.
48+
49+
```php
50+
use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor;
51+
use PHPStan\PhpDocParser\Ast\Node;
52+
use PHPStan\PhpDocParser\Ast\NodeTraverser;
53+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
54+
55+
$visitor = new class extends AbstractNodeVisitor {
56+
public function enterNode(Node $node) {
57+
if ($node instanceof IdentifierTypeNode) {
58+
// inspect or transform the node
59+
}
60+
return $node;
61+
}
62+
};
63+
64+
$traverser = new NodeTraverser([$visitor]);
65+
$traverser->traverse([$phpDocNode]);
66+
```
67+
68+
The `NodeTraverser` supports `DONT_TRAVERSE_CHILDREN`, `STOP_TRAVERSAL`, `REMOVE_NODE`, and `DONT_TRAVERSE_CURRENT_AND_CHILDREN` control constants. A built-in `CloningVisitor` is included for creating deep copies of the AST (used by the format-preserving printer).
69+
70+
### Node attributes
71+
72+
Nodes can carry attributes such as line numbers, token indexes, and comments. Enable them via `ParserConfig`:
73+
74+
```php
75+
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]);
76+
```
77+
78+
These attributes are required for the format-preserving printer and can also be used for mapping AST nodes back to source positions.
79+
2080
## Installation
2181

2282
```

0 commit comments

Comments
 (0)