diff --git a/doc/4_Code_generation.markdown b/doc/4_Code_generation.markdown index e345ed1..cbfa422 100644 --- a/doc/4_Code_generation.markdown +++ b/doc/4_Code_generation.markdown @@ -3,39 +3,41 @@ Code generation 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 a set of builders for -classes, interfaces, traits, methods, functions, parameters and properties. The builders allow creating node trees using -a fluid interface, instead of instantiating all nodes manually. +namespaces, classes, interfaces, traits, methods, functions, parameters and properties. The builders allow creating node +trees using a fluid interface, instead of instantiating all nodes manually. Here is an example: ```php class('SomeClass') - ->extend('SomeOtherClass') - ->implement('A\Few', 'Interfaces') - ->makeAbstract() // ->makeFinal() - - ->addStmt($factory->method('someMethod') +$node = $factory->namespace('Name\Space') + ->addStmt($factory->class('SomeClass') + ->extend('SomeOtherClass') + ->implement('A\Few', 'Interfaces') ->makeAbstract() // ->makeFinal() - ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) - ->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 PhpParser\Node\Expr\Print_(new PhpParser\Node\Expr\Variable('someParam'))) - ) + ->addStmt($factory->method('someMethod') + ->makeAbstract() // ->makeFinal() + ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) + ->setDocComment('/** + * This method does something. + * + * @param SomeClass And takes a parameter + */') + ) - // properties will be correctly reordered above the methods - ->addStmt($factory->property('someProperty')->makeProtected()) - ->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3))) + ->addStmt($factory->method('anotherMethod') + ->makeProtected() // ->makePublic() [default], ->makePrivate() + ->addParam($factory->param('someParam')->setDefault('test')) + // it is possible to add manually created nodes + ->addStmt(new PhpParser\Node\Expr\Print_(new PhpParser\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() ; @@ -50,6 +52,8 @@ This will produce the following output with the standard pretty printer: ```php createNamespaceBuilder('Some\Namespace') + $node = $this->createNamespaceBuilder('Name\Space') ->addStmt($stmt1) ->addStmts(array($stmt2, $stmt3)) ->getNode() ; $this->assertEquals($expected, $node); - $node = $this->createNamespaceBuilder(new Node\Name(array('Some', 'Namespace'))) + $node = $this->createNamespaceBuilder(new Node\Name(array('Name', 'Space'))) ->addStmts(array($stmt1, $stmt2)) ->addStmt($stmt3) ->getNode() diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 4eac850..d43f760 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -2,6 +2,8 @@ namespace PhpParser; +use PhpParser\Node\Expr; + class BuilderFactoryTest extends \PHPUnit_Framework_TestCase { /** @@ -24,4 +26,66 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase array('param', 'PhpParser\Builder\Param'), ); } + + public function testIntegration() { + $factory = new BuilderFactory; + $node = $factory->namespace('Name\Space') + ->addStmt($factory + ->class('SomeClass') + ->extend('SomeOtherClass') + ->implement('A\Few', 'Interfaces') + ->makeAbstract() + + ->addStmt($factory->method('someMethod') + ->makeAbstract() + ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) + ->setDocComment('/** + * This method does something. + * + * @param SomeClass And takes a parameter + */')) + + ->addStmt($factory->method('anotherMethod') + ->makeProtected() + ->addParam($factory->param('someParam')->setDefault('test')) + ->addStmt(new Expr\Print_(new Expr\Variable('someParam')))) + + ->addStmt($factory->property('someProperty')->makeProtected()) + ->addStmt($factory->property('anotherProperty') + ->makePrivate() + ->setDefault(array(1, 2, 3)))) + ->getNode() + ; + + $expected = <<<'EOC' +prettyPrintFile($stmts); + + $this->assertEquals( + str_replace("\r\n", "\n", $expected), + str_replace("\r\n", "\n", $generated) + ); + } }