Blame view

vendor/nikic/php-parser/doc/4_Code_generation.markdown 2.68 KB
d06254b2   Etienne Pallier   bugfix plugin mig...
1
2
Code generation
===============
c4650843   Etienne Pallier   Ajout du dossier ...
3

d06254b2   Etienne Pallier   bugfix plugin mig...
4
5
6
It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the
pretty printer to convert it to PHP code. To simplify code generation, the project comes with builders which allow
creating node trees using a fluid interface, instead of instantiating all nodes manually. Builders are available for
c4650843   Etienne Pallier   Ajout du dossier ...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
the following syntactic elements:

 * namespaces and use statements
 * classes, interfaces and traits
 * methods, functions and parameters
 * properties

Here is an example:

```php
use PhpParser\BuilderFactory;
use PhpParser\PrettyPrinter;
use PhpParser\Node;

$factory = new BuilderFactory;
$node = $factory->namespace('Name\Space')
d06254b2   Etienne Pallier   bugfix plugin mig...
23
24
25
    ->addStmt($factory->use('Some\Other\Thingy')->as('SomeOtherClass'))
    ->addStmt($factory->class('SomeClass')
        ->extend('SomeOtherClass')
c4650843   Etienne Pallier   Ajout du dossier ...
26
27
28
        ->implement('A\Few', '\Interfaces')
        ->makeAbstract() // ->makeFinal()

c4650843   Etienne Pallier   Ajout du dossier ...
29
30
31
        ->addStmt($factory->method('someMethod')
            ->makePublic()
            ->makeAbstract() // ->makeFinal()
d06254b2   Etienne Pallier   bugfix plugin mig...
32
33
            ->setReturnType('bool')
            ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
c4650843   Etienne Pallier   Ajout du dossier ...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            ->setDocComment('/**
                              * This method does something.
                              *
                              * @param SomeClass And takes a parameter
                              */')
        )

        ->addStmt($factory->method('anotherMethod')
            ->makeProtected() // ->makePublic() [default], ->makePrivate()
            ->addParam($factory->param('someParam')->setDefault('test'))
            // it is possible to add manually created nodes
            ->addStmt(new Node\Expr\Print_(new Node\Expr\Variable('someParam')))
        )

        // properties will be correctly reordered above the methods
        ->addStmt($factory->property('someProperty')->makeProtected())
        ->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
    )

    ->getNode()
;

$stmts = array($node);
$prettyPrinter = new PrettyPrinter\Standard();
echo $prettyPrinter->prettyPrintFile($stmts);
```

This will produce the following output with the standard pretty printer:

```php
<?php

namespace Name\Space;

use Some\Other\Thingy as SomeClass;
d06254b2   Etienne Pallier   bugfix plugin mig...
69
abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
c4650843   Etienne Pallier   Ajout du dossier ...
70
{
c4650843   Etienne Pallier   Ajout du dossier ...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    protected $someProperty;
    private $anotherProperty = array(1, 2, 3);
    /**
     * This method does something.
     *
     * @param SomeClass And takes a parameter
     */
    public abstract function someMethod(SomeClass $someParam) : bool;
    protected function anotherMethod($someParam = 'test')
    {
        print $someParam;
    }
}
```