Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

phpstan/phpdoc-parser

Repository files navigation

Build StatusLatest Stable VersionLicensePHPStan Enabled

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.

This parser also supports parsingDoctrine Annotations. The AST nodes live in thePHPStan\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 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 */'

Code of Conduct

This project adheres to aContributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.

Building

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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp