mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Add function builder
This commit is contained in:
parent
9e5c95b6aa
commit
b8b68a969c
109
lib/PHPParser/Builder/Function.php
Normal file
109
lib/PHPParser/Builder/Function.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Builder_Function extends PHPParser_BuilderAbstract
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $returnByRef;
|
||||
protected $params;
|
||||
protected $stmts;
|
||||
|
||||
/**
|
||||
* Creates a function builder.
|
||||
*
|
||||
* @param string $name Name of the function
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
|
||||
$this->returnByRef = false;
|
||||
$this->params = array();
|
||||
$this->stmts = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the function return by reference.
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeReturnByRef() {
|
||||
$this->returnByRef = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a parameter.
|
||||
*
|
||||
* @param PHPParser_Node_Param|PHPParser_Builder_Param $param The parameter to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParam($param) {
|
||||
$param = $this->normalizeNode($param);
|
||||
|
||||
if (!$param instanceof PHPParser_Node_Param) {
|
||||
throw new LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
|
||||
}
|
||||
|
||||
$this->params[] = $param;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple parameters.
|
||||
*
|
||||
* @param array $params The parameters to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParams(array $params) {
|
||||
foreach ($params as $param) {
|
||||
$this->addParam($param);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement.
|
||||
*
|
||||
* @param PHPParser_Node|PHPParser_Builder $stmt The statement to add
|
||||
*
|
||||
* @return PHPParser_Builder_Function 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 PHPParser_Builder_Function The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmts(array $stmts) {
|
||||
foreach ($stmts as $stmt) {
|
||||
$this->addStmt($stmt);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built function node.
|
||||
*
|
||||
* @return PHPParser_Node_Stmt_Function The built function node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new PHPParser_Node_Stmt_Function($this->name, array(
|
||||
'byRef' => $this->returnByRef,
|
||||
'params' => $this->params,
|
||||
'stmts' => $this->stmts,
|
||||
));
|
||||
}
|
||||
}
|
@ -1,13 +1,21 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @method PHPParser_Builder_Class class(string $name) Creates a class builder.
|
||||
* "class" and "function" are reserved keywords, so the methods are defined as _class()
|
||||
* and _function() in the class and are made available as class() and function() through
|
||||
* __call() magic.
|
||||
*
|
||||
* @method PHPParser_Builder_Class class(string $name) Creates a class builder.
|
||||
* @method PHPParser_Builder_Function function(string $name) Creates a function builder
|
||||
*/
|
||||
class PHPParser_BuilderFactory
|
||||
{
|
||||
/*
|
||||
* "class" is a reserved keyword, so we implement the method as _class()
|
||||
* and redirect class() to it through __call magic
|
||||
/**
|
||||
* Creates a class builder.
|
||||
*
|
||||
* @param string $name Name of the class
|
||||
*
|
||||
* @return PHPParser_Builder_Class The created class builder
|
||||
*/
|
||||
protected function _class($name) {
|
||||
return new PHPParser_Builder_Class($name);
|
||||
@ -46,9 +54,22 @@ class PHPParser_BuilderFactory
|
||||
return new PHPParser_Builder_Property($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function builder.
|
||||
*
|
||||
* @param string $name Name of the function
|
||||
*
|
||||
* @return PHPParser_Builder_Property The created function builder
|
||||
*/
|
||||
protected function _function($name) {
|
||||
return new PHPParser_Builder_Function($name);
|
||||
}
|
||||
|
||||
public function __call($name, array $args) {
|
||||
if ('class' === $name) {
|
||||
return call_user_func_array(array($this, '_class'), $args);
|
||||
} elseif ('function' === $name) {
|
||||
return call_user_func_array(array($this, '_function'), $args);
|
||||
}
|
||||
|
||||
throw new LogicException(sprintf('Method "%s" does not exist', $name));
|
||||
|
70
test/PHPParser/Tests/Builder/FunctionTest.php
Normal file
70
test/PHPParser/Tests/Builder/FunctionTest.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Tests_Builder_FunctionTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function createFunctionBuilder($name) {
|
||||
return new PHPParser_Builder_Function($name);
|
||||
}
|
||||
|
||||
public function testReturnByRef() {
|
||||
$node = $this->createFunctionBuilder('test')
|
||||
->makeReturnByRef()
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
new PHPParser_Node_Stmt_Function('test', array(
|
||||
'byRef' => true
|
||||
)),
|
||||
$node
|
||||
);
|
||||
}
|
||||
|
||||
public function testParams() {
|
||||
$param1 = new PHPParser_Node_Param('test1');
|
||||
$param2 = new PHPParser_Node_Param('test2');
|
||||
$param3 = new PHPParser_Node_Param('test3');
|
||||
|
||||
$node = $this->createFunctionBuilder('test')
|
||||
->addParam($param1)
|
||||
->addParams(array($param2, $param3))
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
new PHPParser_Node_Stmt_Function('test', array(
|
||||
'params' => array($param1, $param2, $param3)
|
||||
)),
|
||||
$node
|
||||
);
|
||||
}
|
||||
|
||||
public function testStmts() {
|
||||
$stmt1 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test1'));
|
||||
$stmt2 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test2'));
|
||||
$stmt3 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test3'));
|
||||
|
||||
$node = $this->createFunctionBuilder('test')
|
||||
->addStmt($stmt1)
|
||||
->addStmts(array($stmt2, $stmt3))
|
||||
->getNode()
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
new PHPParser_Node_Stmt_Function('test', array(
|
||||
'stmts' => array($stmt1, $stmt2, $stmt3)
|
||||
)),
|
||||
$node
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException LogicException
|
||||
* @expectedExceptionMessage Expected parameter node, got "Name"
|
||||
*/
|
||||
public function testInvalidParamError() {
|
||||
$this->createFunctionBuilder('test')
|
||||
->addParam(new PHPParser_Node_Name('foo'))
|
||||
;
|
||||
}
|
||||
}
|
@ -21,4 +21,9 @@ class PHPParser_Tests_BuilderFactoryTest extends PHPUnit_Framework_TestCase
|
||||
$factory = new PHPParser_BuilderFactory;
|
||||
$this->assertInstanceOf('PHPParser_Builder_Property', $factory->property('test'));
|
||||
}
|
||||
|
||||
public function testCreateFunctionBuilder() {
|
||||
$factory = new PHPParser_BuilderFactory;
|
||||
$this->assertInstanceOf('PHPParser_Builder_Function', $factory->function('test'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user