PHPackages                             phpstan/phpdoc-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. phpstan/phpdoc-parser

ActiveLibrary[Testing &amp; Quality](/categories/testing)

phpstan/phpdoc-parser
=====================

PHPDoc parser with support for nullable, intersection and generic types

2.3.2(3mo ago)1.5k332.8M↓12.1%73[8 issues](https://github.com/phpstan/phpdoc-parser/issues)[11 PRs](https://github.com/phpstan/phpdoc-parser/pulls)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Nov 20Pushed yesterday6 watchersCompare

[ Source](https://github.com/phpstan/phpdoc-parser)[ Packagist](https://packagist.org/packages/phpstan/phpdoc-parser)[ RSS](/packages/phpstan-phpdoc-parser/feed)WikiDiscussions 2.3.x Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (139)Used By (20)

PHPDoc Parser for PHPStan
=========================

[](#phpdoc-parser-for-phpstan)

 [![Build Status](https://github.com/phpstan/phpdoc-parser/workflows/Build/badge.svg)](https://github.com/phpstan/phpdoc-parser/actions) [![Latest Stable Version](https://camo.githubusercontent.com/1ba4e629b7640c5e35f264b316d89c1b8dccb8536cf13e8542c3ed6aea94025e/68747470733a2f2f706f7365722e707567782e6f72672f7068707374616e2f706870646f632d7061727365722f762f737461626c65)](https://packagist.org/packages/phpstan/phpdoc-parser) [![License](https://camo.githubusercontent.com/a403740e4670e1c2c81f269fde61b92ebe3ec481c74f42131764609202c96ea5/68747470733a2f2f706f7365722e707567782e6f72672f7068707374616e2f7068707374616e2f6c6963656e7365)](https://choosealicense.com/licenses/mit/) [![PHPStan Enabled](https://camo.githubusercontent.com/441b5874ce4df0a2defc892979c96c46889b69cb32119d04f0b48626349f8bc9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d627269676874677265656e2e7376673f7374796c653d666c6174)](https://phpstan.org/)

This library `phpstan/phpdoc-parser` represents PHPDocs with an AST (Abstract Syntax Tree). It supports parsing and modifying PHPDocs.

For the complete list of supported PHPDoc features check out PHPStan documentation. PHPStan is the main (but not the only) user of this library.

- [PHPDoc Basics](https://phpstan.org/writing-php-code/phpdocs-basics) (list of PHPDoc tags)
- [PHPDoc Types](https://phpstan.org/writing-php-code/phpdoc-types) (list of PHPDoc types)
- [phpdoc-parser API Reference](https://phpstan.github.io/phpdoc-parser/2.3.x/namespace-PHPStan.PhpDocParser.html) with all the AST node types etc.

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

Features
--------

[](#features)

### Supported type syntax

[](#supported-type-syntax)

The parser supports a rich type system including:

- Basic types: `string`, `int`, `bool`, `null`, `self`, `static`, `$this`, etc.
- Nullable types: `?string`
- Union and intersection types: `string|int`, `Foo&Bar`
- Generic types with variance: `array`, `Collection`
- Array shapes: `array{name: string, age: int, ...}`
- Object shapes: `object{name: string, age: int}`
- Callable/closure types: `callable(string): bool`, `Closure(int): void`
- Conditional types: `($input is string ? string : int)`
- Offset access types: `T[K]`
- Constant type expressions: `self::CONST*`, `123`, `'string'`

### Constant expression parsing

[](#constant-expression-parsing)

Constant expressions used in PHPDoc tags are parsed via `ConstExprParser`:

- Scalar values: integers, floats, strings, `true`, `false`, `null`
- Arrays: `{1, 2, 'key' => 'value'}`
- Class constant fetches: `ClassName::CONSTANT`

### AST node traversal

[](#ast-node-traversal)

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.

```
use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeTraverser;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;

$visitor = new class extends AbstractNodeVisitor {
    public function enterNode(Node $node) {
        if ($node instanceof IdentifierTypeNode) {
            // inspect or transform the node
        }
        return $node;
    }
};

$traverser = new NodeTraverser([$visitor]);
$traverser->traverse([$phpDocNode]);
```

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

### Node attributes

[](#node-attributes)

Nodes can carry attributes such as line numbers, token indexes, and comments. Enable them via `ParserConfig`:

```
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]);
```

These attributes are required for the format-preserving printer and can also be used for mapping AST nodes back to source positions.

Installation
------------

[](#installation)

```
composer require phpstan/phpdoc-parser

```

Basic usage
-----------

[](#basic-usage)

```
