1<?php declare(strict_types=1); 2 3namespace PhpParser\Builder; 4 5use PhpParser\BuilderHelpers; 6use PhpParser\Node; 7 8abstract class FunctionLike extends Declaration { 9 protected bool $returnByRef = false; 10 /** @var Node\Param[] */ 11 protected array $params = []; 12 13 /** @var Node\Identifier|Node\Name|Node\ComplexType|null */ 14 protected ?Node $returnType = null; 15 16 /** 17 * Make the function return by reference. 18 * 19 * @return $this The builder instance (for fluid interface) 20 */ 21 public function makeReturnByRef() { 22 $this->returnByRef = true; 23 24 return $this; 25 } 26 27 /** 28 * Adds a parameter. 29 * 30 * @param Node\Param|Param $param The parameter to add 31 * 32 * @return $this The builder instance (for fluid interface) 33 */ 34 public function addParam($param) { 35 $param = BuilderHelpers::normalizeNode($param); 36 37 if (!$param instanceof Node\Param) { 38 throw new \LogicException(sprintf('Expected parameter node, got "%s"', $param->getType())); 39 } 40 41 $this->params[] = $param; 42 43 return $this; 44 } 45 46 /** 47 * Adds multiple parameters. 48 * 49 * @param (Node\Param|Param)[] $params The parameters to add 50 * 51 * @return $this The builder instance (for fluid interface) 52 */ 53 public function addParams(array $params) { 54 foreach ($params as $param) { 55 $this->addParam($param); 56 } 57 58 return $this; 59 } 60 61 /** 62 * Sets the return type for PHP 7. 63 * 64 * @param string|Node\Name|Node\Identifier|Node\ComplexType $type 65 * 66 * @return $this The builder instance (for fluid interface) 67 */ 68 public function setReturnType($type) { 69 $this->returnType = BuilderHelpers::normalizeType($type); 70 71 return $this; 72 } 73} 74