matthiasnoback / php-parser-instantiation-printer
Installs: 54 809
Dependents: 1
Suggesters: 0
Security: 0
Stars: 13
Watchers: 3
Forks: 2
Open Issues: 0
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-11-17 10:50:16 UTC
README
This project provides a so-called InstantiationPrinter. I'm not the only one who has attempted to build such a thing: nikic/PHP-Parser#566
Example: $instance = new self();
In PHP-Parser nodes this will be:
array(
0: Stmt_Expression(
expr: Expr_Assign(
var: Expr_Variable(
name: instance
)
expr: Expr_New(
class: Name(
parts: array(
0: self
)
)
args: array(
)
)
)
)
)
When printed by the InstantiationPrinter
this becomes:
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') ) ) );
Usage
Install this library in your project:
composer require --dev matthiasnoback/php-parser-instantiation-printer
You'll have command-line tool now, that generates the nodes based on the provided PHP script:
bin/print-node-instantiation-code temp.php
(if you don't have a bin/
directory in your project, try vendor/bin
)
You can also instantiate your own InstantiationPrinter
service.
Take a look at the code in the print-node-instantiation-code
script to find out how to accomplish this.