FunctionLike.php 1.8 KB

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