annotate vendor/nikic/php-parser/doc/4_Code_generation.markdown @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 Code generation
Chris@0 2 ===============
Chris@0 3
Chris@0 4 It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the
Chris@0 5 pretty printer to convert it to PHP code. To simplify code generation, the project comes with builders which allow
Chris@0 6 creating node trees using a fluid interface, instead of instantiating all nodes manually. Builders are available for
Chris@0 7 the following syntactic elements:
Chris@0 8
Chris@0 9 * namespaces and use statements
Chris@0 10 * classes, interfaces and traits
Chris@0 11 * methods, functions and parameters
Chris@0 12 * properties
Chris@0 13
Chris@0 14 Here is an example:
Chris@0 15
Chris@0 16 ```php
Chris@0 17 use PhpParser\BuilderFactory;
Chris@0 18 use PhpParser\PrettyPrinter;
Chris@0 19 use PhpParser\Node;
Chris@0 20
Chris@0 21 $factory = new BuilderFactory;
Chris@0 22 $node = $factory->namespace('Name\Space')
Chris@0 23 ->addStmt($factory->use('Some\Other\Thingy')->as('SomeOtherClass'))
Chris@0 24 ->addStmt($factory->class('SomeClass')
Chris@0 25 ->extend('SomeOtherClass')
Chris@0 26 ->implement('A\Few', '\Interfaces')
Chris@0 27 ->makeAbstract() // ->makeFinal()
Chris@0 28
Chris@0 29 ->addStmt($factory->method('someMethod')
Chris@0 30 ->makePublic()
Chris@0 31 ->makeAbstract() // ->makeFinal()
Chris@0 32 ->setReturnType('bool')
Chris@0 33 ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
Chris@0 34 ->setDocComment('/**
Chris@0 35 * This method does something.
Chris@0 36 *
Chris@0 37 * @param SomeClass And takes a parameter
Chris@0 38 */')
Chris@0 39 )
Chris@0 40
Chris@0 41 ->addStmt($factory->method('anotherMethod')
Chris@0 42 ->makeProtected() // ->makePublic() [default], ->makePrivate()
Chris@0 43 ->addParam($factory->param('someParam')->setDefault('test'))
Chris@0 44 // it is possible to add manually created nodes
Chris@0 45 ->addStmt(new Node\Expr\Print_(new Node\Expr\Variable('someParam')))
Chris@0 46 )
Chris@0 47
Chris@0 48 // properties will be correctly reordered above the methods
Chris@0 49 ->addStmt($factory->property('someProperty')->makeProtected())
Chris@0 50 ->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
Chris@0 51 )
Chris@0 52
Chris@0 53 ->getNode()
Chris@0 54 ;
Chris@0 55
Chris@0 56 $stmts = array($node);
Chris@0 57 $prettyPrinter = new PrettyPrinter\Standard();
Chris@0 58 echo $prettyPrinter->prettyPrintFile($stmts);
Chris@0 59 ```
Chris@0 60
Chris@0 61 This will produce the following output with the standard pretty printer:
Chris@0 62
Chris@0 63 ```php
Chris@0 64 <?php
Chris@0 65
Chris@0 66 namespace Name\Space;
Chris@0 67
Chris@0 68 use Some\Other\Thingy as SomeClass;
Chris@0 69 abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
Chris@0 70 {
Chris@0 71 protected $someProperty;
Chris@0 72 private $anotherProperty = array(1, 2, 3);
Chris@0 73 /**
Chris@0 74 * This method does something.
Chris@0 75 *
Chris@0 76 * @param SomeClass And takes a parameter
Chris@0 77 */
Chris@0 78 public abstract function someMethod(SomeClass $someParam) : bool;
Chris@0 79 protected function anotherMethod($someParam = 'test')
Chris@0 80 {
Chris@0 81 print $someParam;
Chris@0 82 }
Chris@0 83 }
Chris@0 84 ```