1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/lib/PhpParser/Builder/Function_.php
2014-04-02 09:44:45 +02:00

115 lines
2.5 KiB
PHP

<?php
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class 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 self The builder instance (for fluid interface)
*/
public function makeReturnByRef() {
$this->returnByRef = true;
return $this;
}
/**
* Adds a parameter.
*
* @param Node\Param|Param $param The parameter to add
*
* @return self The builder instance (for fluid interface)
*/
public function addParam($param) {
$param = $this->normalizeNode($param);
if (!$param instanceof 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 self The builder instance (for fluid interface)
*/
public function addParams(array $params) {
foreach ($params as $param) {
$this->addParam($param);
}
return $this;
}
/**
* Adds a statement.
*
* @param Node|PhpParser\Builder $stmt The statement to add
*
* @return self 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 self 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 Stmt\Function_ The built function node
*/
public function getNode() {
return new Stmt\Function_($this->name, array(
'byRef' => $this->returnByRef,
'params' => $this->params,
'stmts' => $this->stmts,
));
}
}