@@ -5,14 +5,14 @@ PHP code generation based on AST.
5
5
## Installation
6
6
7
7
``` bash
8
- $ composer require open-code-modeling/php-code-ast --dev
8
+ $ composer require open-code-modeling/php-code-ast
9
9
```
10
10
11
11
## Usage
12
12
13
13
> See unit tests in ` tests ` folder for comprehensive examples.
14
14
15
- Let's start with a straightforward example of generating a class with the ` ClassFactory ` :
15
+ Let's start with a straightforward example of generating a class with the ` ClassBuilder ` :
16
16
17
17
``` php
18
18
<?php
@@ -22,16 +22,16 @@ $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);
22
22
23
23
$ast = $parser->parse('');
24
24
25
- $classFactory = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
26
- $classFactory
25
+ $classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
26
+ $classBuilder
27
27
->setFinal(true)
28
28
->setExtends('BaseClass')
29
29
->setNamespaceImports('Foo\\Bar')
30
30
->setImplements('\\Iterator', 'Bar');
31
31
32
32
$nodeTraverser = new PhpParser\NodeTraverser();
33
33
34
- $classFactory ->injectVisitors($nodeTraverser, $parser);
34
+ $classBuilder ->injectVisitors($nodeTraverser, $parser);
35
35
36
36
print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
37
37
```
@@ -93,12 +93,15 @@ Now, change the body of the `toInt()` method to something else. You will see tha
93
93
94
94
### Reverse usage
95
95
96
- It is also possible to create a factory class from parsed PHP AST. You can create an instance of ` OpenCodeModeling\CodeAst\Factory\ClassFactory ` by
97
- calling ` OpenCodeModeling\CodeAst\Factory\ClassFactory ::fromNodes() ` .
96
+ It is also possible to create a factory class from parsed PHP AST. You can create an instance of
97
+ ` OpenCodeModeling\CodeAst\Builder\ClassBuilder ` by calling ` OpenCodeModeling\CodeAst\Builder\ClassBuilder ::fromNodes() ` .
98
98
99
99
``` php
100
100
<?php
101
- $expected = <<<'EOF'
101
+ $parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7);
102
+ $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);
103
+
104
+ $expected = <<<'EOF'
102
105
<?php
103
106
104
107
declare (strict_types=1);
@@ -114,12 +117,11 @@ EOF;
114
117
115
118
$ast = $parser->parse($expected);
116
119
117
- $classFactory = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast);
118
-
119
- $classFactory->getName(); // TestClass
120
- $classFactory->getExtends(); // BaseClass
121
- $classFactory->isFinal(); // true
122
- $classFactory->isStrict(); // true
123
- $classFactory->isAbstract(); // false
120
+ $classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast);
124
121
122
+ $classBuilder->getName(); // TestClass
123
+ $classBuilder->getExtends(); // BaseClass
124
+ $classBuilder->isFinal(); // true
125
+ $classBuilder->isStrict(); // true
126
+ $classBuilder->isAbstract(); // false
125
127
```
0 commit comments