説明なし

FunctionLike.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\BuilderHelpers;
  4. use PhpParser\Node;
  5. abstract class FunctionLike extends Declaration
  6. {
  7. protected $returnByRef = false;
  8. protected $params = [];
  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 = BuilderHelpers::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\Identifier|Node\ComplexType $type
  52. *
  53. * @return $this The builder instance (for fluid interface)
  54. */
  55. public function setReturnType($type) {
  56. $this->returnType = BuilderHelpers::normalizeType($type);
  57. return $this;
  58. }
  59. }