diff --git a/lib/PhpParser/Builder/Namespace_.php b/lib/PhpParser/Builder/Namespace_.php new file mode 100644 index 0000000..2486416 --- /dev/null +++ b/lib/PhpParser/Builder/Namespace_.php @@ -0,0 +1,59 @@ +name = $this->normalizeName($name); + } + + /** + * Adds a statement. + * + * @param Node|PhpParser\Builder $stmt The statement to add + * + * @return $this The builder instance (for fluid interface) + */ + public function addStmt($stmt) { + $this->stmts[] = $this->normalizeNode($stmt); + + return $this; + } + + /** + * Adds multiple statements. + * + * @param array $stmts The statements to add + * + * @return $this The builder instance (for fluid interface) + */ + public function addStmts(array $stmts) { + foreach ($stmts as $stmt) { + $this->addStmt($stmt); + } + + return $this; + } + + /** + * Returns the built node. + * + * @return Node The built node + */ + public function getNode() { + return new Stmt\Namespace_($this->name, $this->stmts); + } +} diff --git a/lib/PhpParser/BuilderFactory.php b/lib/PhpParser/BuilderFactory.php index 061f22d..ab1c5a5 100644 --- a/lib/PhpParser/BuilderFactory.php +++ b/lib/PhpParser/BuilderFactory.php @@ -5,16 +5,28 @@ namespace PhpParser; use PhpParser\Builder; /** - * "class", "interface" and "function" are reserved keywords, so the methods are defined as _class(), - * _interface() and _function() in the class and are made available as class(), interface() and function() - * through __call() magic. + * The following methods use reserved keywords, so their implementation is defined with an underscore and made available + * with the reserved name through __call() magic. * + * @method Builder\Namespace_ namespace(string $name) Creates a namespace builder. * @method Builder\Class_ class(string $name) Creates a class builder. - * @method Builder\Function_ function(string $name) Creates a function builder * @method Builder\Interface_ interface(string $name) Creates an interface builder. + * @method Builder\Trait_ trait(string $name) Creates a trait builder. + * @method Builder\Function_ function(string $name) Creates a function builder */ class BuilderFactory { + /** + * Creates a namespace builder. + * + * @param string $name Fully qualified namespace + * + * @return Builder\Namespace_ The created namespace builder + */ + protected function _namespace($name) { + return new Builder\Namespace_($name); + } + /** * Creates a class builder. * diff --git a/test/PhpParser/Builder/NamespaceTest.php b/test/PhpParser/Builder/NamespaceTest.php new file mode 100644 index 0000000..872d7ad --- /dev/null +++ b/test/PhpParser/Builder/NamespaceTest.php @@ -0,0 +1,37 @@ +createNamespaceBuilder('Some\Namespace') + ->addStmt($stmt1) + ->addStmts(array($stmt2, $stmt3)) + ->getNode() + ; + $this->assertEquals($expected, $node); + + $node = $this->createNamespaceBuilder(new Node\Name(array('Some', 'Namespace'))) + ->addStmts(array($stmt1, $stmt2)) + ->addStmt($stmt3) + ->getNode() + ; + $this->assertEquals($expected, $node); + } +} diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 6efc958..4eac850 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -14,6 +14,7 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase public function provideTestFactory() { return array( + array('namespace', 'PhpParser\Builder\Namespace_'), array('class', 'PhpParser\Builder\Class_'), array('interface', 'PhpParser\Builder\Interface_'), array('trait', 'PhpParser\Builder\Trait_'),