Param.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\Node;
  5. class Param extends PhpParser\BuilderAbstract
  6. {
  7. protected $name;
  8. protected $default = null;
  9. /** @var string|Node\Name|Node\NullableType|null */
  10. protected $type = null;
  11. protected $byRef = false;
  12. /**
  13. * Creates a parameter builder.
  14. *
  15. * @param string $name Name of the parameter
  16. */
  17. public function __construct($name) {
  18. $this->name = $name;
  19. }
  20. /**
  21. * Sets default value for the parameter.
  22. *
  23. * @param mixed $value Default value to use
  24. *
  25. * @return $this The builder instance (for fluid interface)
  26. */
  27. public function setDefault($value) {
  28. $this->default = $this->normalizeValue($value);
  29. return $this;
  30. }
  31. /**
  32. * Sets type hint for the parameter.
  33. *
  34. * @param string|Node\Name|Node\NullableType $type Type hint to use
  35. *
  36. * @return $this The builder instance (for fluid interface)
  37. */
  38. public function setTypeHint($type) {
  39. $this->type = $this->normalizeType($type);
  40. if ($this->type === 'void') {
  41. throw new \LogicException('Parameter type cannot be void');
  42. }
  43. return $this;
  44. }
  45. /**
  46. * Make the parameter accept the value by reference.
  47. *
  48. * @return $this The builder instance (for fluid interface)
  49. */
  50. public function makeByRef() {
  51. $this->byRef = true;
  52. return $this;
  53. }
  54. /**
  55. * Returns the built parameter node.
  56. *
  57. * @return Node\Param The built parameter node
  58. */
  59. public function getNode() {
  60. return new Node\Param(
  61. $this->name, $this->default, $this->type, $this->byRef
  62. );
  63. }
  64. }