You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Copy file name to clipboardExpand all lines: README.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,66 @@ For the complete list of supported PHPDoc features check out PHPStan documentati
17
17
18
18
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).
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`:
0 commit comments