1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Add namespace builder

This commit is contained in:
Stepan Anchugov 2014-12-11 21:24:10 +05:00 committed by Nikita Popov
parent 4387454fe0
commit 01643e06d3
4 changed files with 113 additions and 4 deletions

View File

@ -0,0 +1,59 @@
<?php
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class Namespace_ extends PhpParser\BuilderAbstract
{
private $name;
private $stmts = array();
/**
* Creates a namespace builder.
*
* @param Node\Name|string $name Name of the namespace
*/
public function __construct($name) {
$this->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);
}
}

View File

@ -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.
*

View File

@ -0,0 +1,37 @@
<?php
namespace PhpParser\Builder;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class NamespaceTest extends \PHPUnit_Framework_TestCase
{
protected function createNamespaceBuilder($fqn) {
return new Namespace_($fqn);
}
public function testCreation() {
$stmt1 = new Stmt\Class_('SomeClass');
$stmt2 = new Stmt\Interface_('SomeInterface');
$stmt3 = new Stmt\Function_('someFunction');
$expected = new Stmt\Namespace_(
new Node\Name('Some\Namespace'),
array($stmt1, $stmt2, $stmt3)
);
$node = $this->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);
}
}

View File

@ -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_'),