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

Add trait builder (for completeness...)

This commit is contained in:
Nikita Popov 2014-12-19 17:58:47 +01:00
parent 55fdbc6dbc
commit 4387454fe0
5 changed files with 109 additions and 5 deletions

View File

@ -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:

View File

@ -0,0 +1,49 @@
<?php
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Trait_ extends Declaration
{
protected $name;
protected $methods = array();
/**
* Creates an interface builder.
*
* @param string $name Name of the interface
*/
public function __construct($name) {
$this->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);
}
}

View File

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

View File

@ -0,0 +1,43 @@
<?php
namespace PhpParser\Builder;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class TraitTest extends \PHPUnit_Framework_TestCase
{
protected function createTraitBuilder($class) {
return new Trait_($class);
}
public function testStmtAddition() {
$method1 = new Stmt\ClassMethod('test1');
$method2 = new Stmt\ClassMethod('test2');
$method3 = new Stmt\ClassMethod('test3');
$trait = $this->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()))
;
}
}

View File

@ -16,6 +16,7 @@ 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'),