2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2014-12-19 17:21:46 +01:00
|
|
|
|
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
2017-04-24 21:15:11 +02:00
|
|
|
use PhpParser\BuilderHelpers;
|
2014-12-19 17:21:46 +01:00
|
|
|
use PhpParser\Node;
|
|
|
|
|
|
|
|
abstract class FunctionLike extends Declaration
|
|
|
|
{
|
|
|
|
protected $returnByRef = false;
|
2017-08-13 14:06:08 +02:00
|
|
|
protected $params = [];
|
2016-12-05 13:30:29 +01:00
|
|
|
|
|
|
|
/** @var string|Node\Name|Node\NullableType|null */
|
2016-04-09 11:41:38 +02:00
|
|
|
protected $returnType = null;
|
2014-12-19 17:21:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the function return by reference.
|
|
|
|
*
|
|
|
|
* @return $this 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 $this The builder instance (for fluid interface)
|
|
|
|
*/
|
|
|
|
public function addParam($param) {
|
2017-04-24 21:15:11 +02:00
|
|
|
$param = BuilderHelpers::normalizeNode($param);
|
2014-12-19 17:21:46 +01:00
|
|
|
|
|
|
|
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 $this The builder instance (for fluid interface)
|
|
|
|
*/
|
|
|
|
public function addParams(array $params) {
|
|
|
|
foreach ($params as $param) {
|
|
|
|
$this->addParam($param);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2016-04-09 11:41:38 +02:00
|
|
|
|
|
|
|
/**
|
2016-04-09 12:13:49 +02:00
|
|
|
* Sets the return type for PHP 7.
|
2016-04-09 11:41:38 +02:00
|
|
|
*
|
2017-04-24 21:15:11 +02:00
|
|
|
* @param string|Node\Name|Node\NullableType $type One of array, callable, string, int, float,
|
|
|
|
* bool, iterable, or a class/interface name.
|
2016-04-09 11:41:38 +02:00
|
|
|
*
|
|
|
|
* @return $this The builder instance (for fluid interface)
|
|
|
|
*/
|
2017-04-24 21:15:11 +02:00
|
|
|
public function setReturnType($type) {
|
|
|
|
$this->returnType = BuilderHelpers::normalizeType($type);
|
2016-04-09 11:41:38 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|