Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpstan/phpdoc-parser
Next-gen phpDoc parser with support for intersection types and generics
https://github.com/phpstan/phpdoc-parser
php php7 phpdoc phpstan static-analysis static-analyzer static-code-analysis testing
Last synced: 26 days ago
JSON representation
Next-gen phpDoc parser with support for intersection types and generics
- Host: GitHub
- URL: https://github.com/phpstan/phpdoc-parser
- Owner: phpstan
- License: mit
- Created: 2017-11-19T11:48:19.000Z (almost 7 years ago)
- Default Branch: 1.23.x
- Last Pushed: 2024-04-05T08:04:45.000Z (7 months ago)
- Last Synced: 2024-04-14T05:41:30.344Z (7 months ago)
- Topics: php, php7, phpdoc, phpstan, static-analysis, static-analyzer, static-code-analysis, testing
- Language: PHP
- Size: 824 KB
- Stars: 1,231
- Watchers: 7
- Forks: 60
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-php - PHPDoc Parser - Next-gen phpDoc parser with support for intersection types and generics (Table of Contents / Static Analysis)
README
PHPDoc Parser for PHPStan
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/1.23.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/1.23.x/namespace-PHPStan.PhpDocParser.Ast.PhpDoc.Doctrine.html). The support needs to be turned on by setting `bool $parseDoctrineAnnotations` to `true` in `Lexer` and `PhpDocParser` class constructors.
## Installation
```
composer require phpstan/phpdoc-parser
```## Basic usage
```php
tokenize('/** @param Lorem $a */'));
$phpDocNode = $phpDocParser->parse($tokens); // PhpDocNode
$paramTags = $phpDocNode->getParamTagValues(); // ParamTagValueNode[]
echo $paramTags[0]->parameterName; // '$a'
echo $paramTags[0]->type; // IdentifierTypeNode - 'Lorem'
```### Format-preserving printer
This component can be used to modify the AST
and print it again as close as possible to the original.It's heavily inspired by format-preserving printer component in [nikic/PHP-Parser](https://github.com/nikic/PHP-Parser).
```php
true, 'indexes' => true];$lexer = new Lexer();
$constExprParser = new ConstExprParser(true, true, $usedAttributes);
$typeParser = new TypeParser($constExprParser, true, $usedAttributes);
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes);$tokens = new TokenIterator($lexer->tokenize('/** @param Lorem $a */'));
$phpDocNode = $phpDocParser->parse($tokens); // PhpDocNode$cloningTraverser = new NodeTraverser([new CloningVisitor()]);
/** @var PhpDocNode $newPhpDocNode */
[$newPhpDocNode] = $cloningTraverser->traverse([$phpDocNode]);// change something in $newPhpDocNode
$newPhpDocNode->getParamTagValues()[0]->type = new IdentifierTypeNode('Ipsum');// print changed PHPDoc
$printer = new Printer();
$newPhpDoc = $printer->printFormatPreserving($newPhpDocNode, $phpDocNode, $tokens);
echo $newPhpDoc; // '/** @param Ipsum $a */'
```## Code of Conduct
This project adheres to a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project and its community, you are expected to uphold this code.
## Building
Initially you need to run `composer install`, or `composer update` in case you aren't working in a folder which was built before.
Afterwards you can either run the whole build including linting and coding standards using
make
or run only tests using
make tests