Skip to content

Next-gen phpDoc parser with support for intersection types and generics

License

Notifications You must be signed in to change notification settings

phpstan/phpdoc-parser

Repository files navigation

PHPDoc Parser for PHPStan

Build StatusLatest Stable VersionLicensePHPStan Enabled

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.

This parser also supports parsing Doctrine Annotations. The AST nodes live in the PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine namespace.

Installation

composer require phpstan/phpdoc-parser 

Basic usage

<?phprequire_once__DIR__ . '/vendor/autoload.php'; usePHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; usePHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; usePHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; usePHPStan\PhpDocParser\Lexer\Lexer; usePHPStan\PhpDocParser\ParserConfig; usePHPStan\PhpDocParser\Parser\ConstExprParser; usePHPStan\PhpDocParser\Parser\PhpDocParser; usePHPStan\PhpDocParser\Parser\TokenIterator; usePHPStan\PhpDocParser\Parser\TypeParser; // basic setup$config = newParserConfig(usedAttributes: []); $lexer = newLexer($config); $constExprParser = newConstExprParser($config); $typeParser = newTypeParser($config, $constExprParser); $phpDocParser = newPhpDocParser($config, $typeParser, $constExprParser); // parsing and reading a PHPDoc string$tokens = newTokenIterator($lexer->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.

<?phprequire_once__DIR__ . '/vendor/autoload.php'; usePHPStan\PhpDocParser\Ast\NodeTraverser; usePHPStan\PhpDocParser\Ast\NodeVisitor\CloningVisitor; usePHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; usePHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; usePHPStan\PhpDocParser\Lexer\Lexer; usePHPStan\PhpDocParser\ParserConfig; usePHPStan\PhpDocParser\Parser\ConstExprParser; usePHPStan\PhpDocParser\Parser\PhpDocParser; usePHPStan\PhpDocParser\Parser\TokenIterator; usePHPStan\PhpDocParser\Parser\TypeParser; usePHPStan\PhpDocParser\Printer\Printer; // basic setup with enabled required lexer attributes$config = newParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]); $lexer = newLexer($config); $constExprParser = newConstExprParser($config); $typeParser = newTypeParser($config, $constExprParser); $phpDocParser = newPhpDocParser($config, $typeParser, $constExprParser); $tokens = newTokenIterator($lexer->tokenize('/** @param Lorem $a */')); $phpDocNode = $phpDocParser->parse($tokens); // PhpDocNode$cloningTraverser = newNodeTraverser([newCloningVisitor()]); /** @var PhpDocNode $newPhpDocNode */ [$newPhpDocNode] = $cloningTraverser->traverse([$phpDocNode]); // change something in $newPhpDocNode$newPhpDocNode->getParamTagValues()[0]->type = newIdentifierTypeNode('Ipsum'); // print changed PHPDoc$printer = newPrinter(); $newPhpDoc = $printer->printFormatPreserving($newPhpDocNode, $phpDocNode, $tokens); echo$newPhpDoc; // '/** @param Ipsum $a */'

Code of Conduct

This project adheres to a Contributor Code of Conduct. 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 
close