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:
parent
55fdbc6dbc
commit
4387454fe0
@ -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:
|
||||
|
||||
|
49
lib/PhpParser/Builder/Trait_.php
Normal file
49
lib/PhpParser/Builder/Trait_.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
test/PhpParser/Builder/TraitTest.php
Normal file
43
test/PhpParser/Builder/TraitTest.php
Normal 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()))
|
||||
;
|
||||
}
|
||||
}
|
@ -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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user