From 4387454fe02c5d0ca72a8c65bba3dfa68cb91929 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 19 Dec 2014 17:58:47 +0100 Subject: [PATCH] Add trait builder (for completeness...) --- doc/4_Code_generation.markdown | 4 +-- lib/PhpParser/Builder/Trait_.php | 49 +++++++++++++++++++++++++++ lib/PhpParser/BuilderFactory.php | 15 ++++++-- test/PhpParser/Builder/TraitTest.php | 43 +++++++++++++++++++++++ test/PhpParser/BuilderFactoryTest.php | 3 +- 5 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 lib/PhpParser/Builder/Trait_.php create mode 100644 test/PhpParser/Builder/TraitTest.php diff --git a/doc/4_Code_generation.markdown b/doc/4_Code_generation.markdown index 707323b..e345ed1 100644 --- a/doc/4_Code_generation.markdown +++ b/doc/4_Code_generation.markdown @@ -3,8 +3,8 @@ 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, methods, functions, parameters and properties. The builders allow creating node trees using a -fluid interface, instead of instantiating all nodes manually. +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: diff --git a/lib/PhpParser/Builder/Trait_.php b/lib/PhpParser/Builder/Trait_.php new file mode 100644 index 0000000..a1953ce --- /dev/null +++ b/lib/PhpParser/Builder/Trait_.php @@ -0,0 +1,49 @@ +name = $name; + } + + /** + * Adds a statement. + * + * @param Stmt|PhpParser\Builder $stmt The statement to add + * + * @return $this The builder instance (for fluid interface) + */ + public function addStmt($stmt) { + $stmt = $this->normalizeNode($stmt); + if (!$stmt instanceof Stmt\ClassMethod) { + throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); + } + + $this->methods[] = $stmt; + + return $this; + } + + /** + * Returns the built trait node. + * + * @return Stmt\Trait_ The built interface node + */ + public function getNode() { + return new Stmt\Trait_($this->name, $this->methods, $this->attributes); + } +} diff --git a/lib/PhpParser/BuilderFactory.php b/lib/PhpParser/BuilderFactory.php index 8267904..061f22d 100644 --- a/lib/PhpParser/BuilderFactory.php +++ b/lib/PhpParser/BuilderFactory.php @@ -27,7 +27,7 @@ class BuilderFactory } /** - * Creates a interface builder. + * Creates an interface builder. * * @param string $name Name of the interface * @@ -37,6 +37,17 @@ class BuilderFactory return new Builder\Interface_($name); } + /** + * Creates a trait builder. + * + * @param string $name Name of the trait + * + * @return Builder\Trait_ The created trait builder + */ + protected function _trait($name) { + return new Builder\Trait_($name); + } + /** * Creates a method builder. * @@ -88,4 +99,4 @@ class BuilderFactory throw new \LogicException(sprintf('Method "%s" does not exist', $name)); } -} \ No newline at end of file +} diff --git a/test/PhpParser/Builder/TraitTest.php b/test/PhpParser/Builder/TraitTest.php new file mode 100644 index 0000000..d52185c --- /dev/null +++ b/test/PhpParser/Builder/TraitTest.php @@ -0,0 +1,43 @@ +createTraitBuilder('TestTrait') + ->setDocComment('/** Nice trait */') + ->addStmt($method1) + ->addStmts(array($method2, $method3)) + ->getNode(); + $this->assertEquals(new Stmt\Trait_('TestTrait', array( + $method1, $method2, $method3 + ), array( + 'comments' => array( + new Comment\Doc('/** Nice trait */') + ) + )), $trait); + } + + /** + * @expectedException \LogicException + * @expectedExceptionMessage Unexpected node of type "Stmt_Echo" + */ + public function testInvalidStmtError() { + $this->createTraitBuilder('Test') + ->addStmt(new Stmt\Echo_(array())) + ; + } +} diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 87f70b6..6efc958 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -16,10 +16,11 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase return array( array('class', 'PhpParser\Builder\Class_'), array('interface', 'PhpParser\Builder\Interface_'), + array('trait', 'PhpParser\Builder\Trait_'), array('method', 'PhpParser\Builder\Method'), array('function', 'PhpParser\Builder\Function_'), array('property', 'PhpParser\Builder\Property'), array('param', 'PhpParser\Builder\Param'), ); } -} \ No newline at end of file +}