FunctionLike.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Stmt;
  6. abstract class FunctionLike extends Declaration
  7. {
  8. protected $returnByRef = false;
  9. protected $params = array();
  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 $type One of array, callable, string, int, float, bool,
  52. * or a class/interface name.
  53. *
  54. * @return $this The builder instance (for fluid interface)
  55. */
  56. public function setReturnType($type)
  57. {
  58. if (in_array($type, array('array', 'callable', 'string', 'int', 'float', 'bool'))) {
  59. $this->returnType = $type;
  60. } else {
  61. $this->returnType = $this->normalizeName($type);
  62. }
  63. return $this;
  64. }
  65. }