open-code-modeling / php-code-ast
Open Code Modeling PHP Code AST
Installs: 1 144
Dependents: 4
Suggesters: 0
Security: 0
Stars: 6
Watchers: 4
Forks: 1
Open Issues: 7
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- nikic/php-parser: ^4.2
Requires (Dev)
- laminas/laminas-filter: ^2.9
- open-code-modeling/php-filter: ^0.1.1
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5.0
- prooph/php-cs-fixer-config: ^v0.4.0
- psalm/plugin-phpunit: ^0.15.0
- roave/security-advisories: dev-master
- vimeo/psalm: ^4.4
- 0.13.x-dev
- 0.12.x-dev
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.x-dev
- 0.11.1
- 0.11.0
- 0.10.x-dev
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.x-dev
- 0.9.0
- 0.8.x-dev
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.x-dev
- 0.7.1
- 0.7.0
- 0.6.x-dev
- 0.6.1
- 0.6.0
- 0.5.x-dev
- 0.5.1
- 0.5.0
- 0.4.0
- v0.3.1
- v0.3.0
- v0.2.3
- v0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-dependabot/composer/symfony/process-5.4.46
- dev-feature/php-attributes-readonly-props
- dev-master
- dev-feature/node-collector
This package is auto-updated.
Last update: 2024-12-06 19:25:37 UTC
README
PHP code generation based on AST.
Installation
$ composer require open-code-modeling/php-code-ast
Usage
See unit tests in
tests
folder for comprehensive examples.
Let's start with a straightforward example of generating a class with the ClassBuilder
:
<?php $parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7); $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]); $ast = $parser->parse(''); $classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service'); $classBuilder ->setFinal(true) ->setExtends('BaseClass') ->setNamespaceImports('Foo\\Bar') ->setImplements('\\Iterator', 'Bar'); $nodeTraverser = new PhpParser\NodeTraverser(); $classBuilder->injectVisitors($nodeTraverser, $parser); print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
Will print the following output:
<?php declare (strict_types=1); namespace My\Awesome\Service; use Foo\Bar; final class TestClass extends BaseClass implements \Iterator, Bar { }
All this is done via PHP AST and it checks if the AST token is already defined. So it will not override your code.
You can also use the code and PHP AST visitor classes to generate code via the low level API.
To add a method toInt()
to the class above you can add this via the ClassMethod
visitor.
<?php $method = new OpenCodeModeling\CodeAst\Code\MethodGenerator( 'toInt', [], OpenCodeModeling\CodeAst\Code\MethodGenerator::FLAG_PUBLIC, new OpenCodeModeling\CodeAst\Code\BodyGenerator($this->parser, 'return $this->myValue;') ); $method->setReturnType('int'); $nodeTraverser->addVisitor(new OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod($method)); print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
This will print the following output.
<?php declare (strict_types=1); namespace My\Awesome\Service; use Foo\Bar; final class TestClass extends BaseClass implements \Iterator, Bar { public function toInt() : int { return $this->myValue; } }
Now, change the body of the toInt()
method to something else. You will see that your changes will NOT be overwritten.
Reverse usage
It is also possible to create a factory class from parsed PHP AST. You can create an instance of
OpenCodeModeling\CodeAst\Builder\ClassBuilder
by calling OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes()
.
<?php $parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7); $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]); $expected = <<<'EOF' <?php declare (strict_types=1); namespace My\Awesome\Service; use Foo\Bar; final class TestClass extends BaseClass implements \Iterator, Bar { private const PRIV = 'private'; } EOF; $ast = $parser->parse($expected); $classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast); $classBuilder->getName(); // TestClass $classBuilder->getExtends(); // BaseClass $classBuilder->isFinal(); // true $classBuilder->isStrict(); // true $classBuilder->isAbstract(); // false