- Notifications
You must be signed in to change notification settings - Fork66
Next-gen phpDoc parser with support for intersection types and generics
License
phpstan/phpdoc-parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This libraryphpstan/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 (list of PHPDoc tags)
- PHPDoc Types (list of PHPDoc types)
- phpdoc-parser API Reference with all the AST node types etc.
This parser also supports parsingDoctrine Annotations. The AST nodes live in thePHPStan\PhpDocParser\Ast\PhpDoc\Doctrine namespace.
composer require phpstan/phpdoc-parser
<?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'
This component can be used to modify the ASTand print it again as close as possible to the original.
It's heavily inspired by format-preserving printer component innikic/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 */'
This project adheres to aContributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Initially you need to runcomposer install
, orcomposer 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
About
Next-gen phpDoc parser with support for intersection types and generics
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.