phpstan / phpdoc-parser
支持可空、交集和泛型类型的PHPDoc解析器
1.31.0
2024-09-22 11:32 UTC
Requires
- php: ^7.2 || ^8.0
Requires (Dev)
- doctrine/annotations: ^2.0
- nikic/php-parser: ^4.15
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^1.5
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
- symfony/process: ^5.2
- 2.0.x-dev
- 1.31.0
- 1.30.1
- 1.30.0
- 1.29.1
- 1.29.0
- 1.28.0
- 1.27.0
- 1.26.0
- 1.25.0
- 1.24.5
- 1.24.4
- 1.24.3
- 1.24.2
- 1.24.1
- 1.24.0
- 1.23.x-dev
- 1.23.1
- 1.23.0
- 1.22.x-dev
- 1.22.1
- 1.22.0
- 1.21.x-dev
- 1.21.3
- 1.21.2
- 1.21.1
- 1.21.0
- 1.20.x-dev
- 1.20.4
- 1.20.3
- 1.20.2
- 1.20.1
- 1.20.0
- 1.19.1
- 1.19.0
- 1.18.1
- 1.18.0
- 1.17.1
- 1.17.0
- 1.16.1
- 1.16.0
- 1.15.3
- 1.15.2
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.1
- 1.13.0
- 1.12.1
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.x-dev
- 1.9.0
- 1.8.x-dev
- 1.8.0
- 1.7.0
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.x-dev
- 0.4.14
- 0.4.13
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3
- 0.2
- 0.1
- dev-renovate/major-github-actions
- dev-better-resolve-static-method-conflict
- dev-renovate/major-root-composer
- dev-revert-119-offset-access-type
- dev-pr/callable_alternative
- dev-pr/generics_variance_and_constraints
- dev-pr/wip
This package is auto-updated.
Last update: 2024-09-22 11:35:14 UTC
README
此库 phpstan/phpdoc-parser
使用AST(抽象语法树)表示PHPDocs。它支持解析和修改PHPDocs。
有关支持的PHPDoc功能的完整列表,请参阅PHPStan文档。PHPStan是该库的主要(但不是唯一)用户。
- PHPDoc基础知识(PHPDoc标签列表)
- PHPDoc类型(PHPDoc类型列表)
- phpdoc-parser API参考,包括所有AST节点类型等。
此解析器还支持解析Doctrine Annotations。AST节点位于PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine命名空间。
安装
composer require phpstan/phpdoc-parser
基本用法
<?php require_once __DIR__ . '/vendor/autoload.php'; use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\PhpDocParser\ParserConfig; use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; // basic setup $config = new ParserConfig(usedAttributes: []); $lexer = new Lexer($config); $constExprParser = new ConstExprParser($config); $typeParser = new TypeParser($config, $constExprParser); $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); // parsing and reading a PHPDoc string $tokens = new TokenIterator($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'
格式保留打印机
此组件可用于修改AST,并将其再次打印出来,尽可能接近原始格式。
它深受nikic/PHP-Parser中格式保留打印机组件的启发。
<?php require_once __DIR__ . '/vendor/autoload.php'; use PHPStan\PhpDocParser\Ast\NodeTraverser; use PHPStan\PhpDocParser\Ast\NodeVisitor\CloningVisitor; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\PhpDocParser\ParserConfig; use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\PhpDocParser\Printer\Printer; // basic setup with enabled required lexer attributes $config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]); $lexer = new Lexer($config); $constExprParser = new ConstExprParser($config); $typeParser = new TypeParser($config, $constExprParser); $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $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 */'
行为准则
此项目遵循贡献者行为准则。通过参与此项目和其社区,你应遵守此准则。
构建
最初,你需要运行composer install
,或者如果你不在之前构建的文件夹中工作,可以运行composer update
。
之后,你可以使用以下命令运行整个构建过程,包括代码审查和编码标准:
make
或者使用以下命令仅运行测试:
make tests