菜谱项目

Param.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. protected $variadic = false;
  13. /**
  14. * Creates a parameter builder.
  15. *
  16. * @param string $name Name of the parameter
  17. */
  18. public function __construct($name) {
  19. $this->name = $name;
  20. }
  21. /**
  22. * Sets default value for the parameter.
  23. *
  24. * @param mixed $value Default value to use
  25. *
  26. * @return $this The builder instance (for fluid interface)
  27. */
  28. public function setDefault($value) {
  29. $this->default = $this->normalizeValue($value);
  30. return $this;
  31. }
  32. /**
  33. * Sets type hint for the parameter.
  34. *
  35. * @param string|Node\Name|Node\NullableType $type Type hint to use
  36. *
  37. * @return $this The builder instance (for fluid interface)
  38. */
  39. public function setTypeHint($type) {
  40. $this->type = $this->normalizeType($type);
  41. if ($this->type === 'void') {
  42. throw new \LogicException('Parameter type cannot be void');
  43. }
  44. return $this;
  45. }
  46. /**
  47. * Make the parameter accept the value by reference.
  48. *
  49. * @return $this The builder instance (for fluid interface)
  50. */
  51. public function makeByRef() {
  52. $this->byRef = true;
  53. return $this;
  54. }
  55. /**
  56. * Make the parameter variadic
  57. *
  58. * @return $this The builder instance (for fluid interface)
  59. */
  60. public function makeVariadic() {
  61. $this->variadic = true;
  62. return $this;
  63. }
  64. /**
  65. * Returns the built parameter node.
  66. *
  67. * @return Node\Param The built parameter node
  68. */
  69. public function getNode() {
  70. return new Node\Param(
  71. $this->name, $this->default, $this->type, $this->byRef, $this->variadic
  72. );
  73. }
  74. }