matthiasnoback / php-parser-instantiation-printer
v0.3.0
2024-04-17 09:40 UTC
Requires
- php: ^8.0
- nikic/php-parser: ^5.0
Requires (Dev)
- phpunit/phpunit: ^9.4
- symfony/process: ^5.1
- symfony/var-dumper: ^5.1
This package is auto-updated.
Last update: 2024-09-17 10:31:29 UTC
README
本项目提供了一个所谓的 InstantiationPrinter。我不是唯一一个尝试构建此类工具的人: nikic/PHP-Parser#566
示例: $instance = new self();
在 PHP-Parser 节点中这将变为
array(
0: Stmt_Expression(
expr: Expr_Assign(
var: Expr_Variable(
name: instance
)
expr: Expr_New(
class: Name(
parts: array(
0: self
)
)
args: array(
)
)
)
)
)
由 InstantiationPrinter
打印出来时,它变为
new PhpParser\Node\Stmt\Expression( new PhpParser\Node\Expr\Assign( new PhpParser\Node\Expr\Variable('instance'), new PhpParser\Node\Expr\New_( new PhpParser\Node\Name('self') ) ) );
用法
将此库安装到您的项目中
composer require --dev matthiasnoback/php-parser-instantiation-printer
您现在将拥有一个命令行工具,该工具根据提供的 PHP 脚本生成节点
bin/print-node-instantiation-code temp.php
(如果您项目中没有 bin/
目录,请尝试 vendor/bin
)
您也可以实例化自己的 InstantiationPrinter
服务。请查看 print-node-instantiation-code
脚本中的代码,了解如何实现此操作。