Param.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\BuilderHelpers;
  5. use PhpParser\Modifiers;
  6. use PhpParser\Node;
  7. class Param implements PhpParser\Builder {
  8. protected string $name;
  9. protected ?Node\Expr $default = null;
  10. /** @var Node\Identifier|Node\Name|Node\ComplexType|null */
  11. protected ?Node $type = null;
  12. protected bool $byRef = false;
  13. protected int $flags = 0;
  14. protected bool $variadic = false;
  15. /** @var list<Node\AttributeGroup> */
  16. protected array $attributeGroups = [];
  17. /**
  18. * Creates a parameter builder.
  19. *
  20. * @param string $name Name of the parameter
  21. */
  22. public function __construct(string $name) {
  23. $this->name = $name;
  24. }
  25. /**
  26. * Sets default value for the parameter.
  27. *
  28. * @param mixed $value Default value to use
  29. *
  30. * @return $this The builder instance (for fluid interface)
  31. */
  32. public function setDefault($value) {
  33. $this->default = BuilderHelpers::normalizeValue($value);
  34. return $this;
  35. }
  36. /**
  37. * Sets type for the parameter.
  38. *
  39. * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
  40. *
  41. * @return $this The builder instance (for fluid interface)
  42. */
  43. public function setType($type) {
  44. $this->type = BuilderHelpers::normalizeType($type);
  45. if ($this->type == 'void') {
  46. throw new \LogicException('Parameter type cannot be void');
  47. }
  48. return $this;
  49. }
  50. /**
  51. * Make the parameter accept the value by reference.
  52. *
  53. * @return $this The builder instance (for fluid interface)
  54. */
  55. public function makeByRef() {
  56. $this->byRef = true;
  57. return $this;
  58. }
  59. /**
  60. * Make the parameter variadic
  61. *
  62. * @return $this The builder instance (for fluid interface)
  63. */
  64. public function makeVariadic() {
  65. $this->variadic = true;
  66. return $this;
  67. }
  68. /**
  69. * Makes the (promoted) parameter public.
  70. *
  71. * @return $this The builder instance (for fluid interface)
  72. */
  73. public function makePublic() {
  74. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
  75. return $this;
  76. }
  77. /**
  78. * Makes the (promoted) parameter protected.
  79. *
  80. * @return $this The builder instance (for fluid interface)
  81. */
  82. public function makeProtected() {
  83. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
  84. return $this;
  85. }
  86. /**
  87. * Makes the (promoted) parameter private.
  88. *
  89. * @return $this The builder instance (for fluid interface)
  90. */
  91. public function makePrivate() {
  92. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
  93. return $this;
  94. }
  95. /**
  96. * Makes the (promoted) parameter readonly.
  97. *
  98. * @return $this The builder instance (for fluid interface)
  99. */
  100. public function makeReadonly() {
  101. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
  102. return $this;
  103. }
  104. /**
  105. * Adds an attribute group.
  106. *
  107. * @param Node\Attribute|Node\AttributeGroup $attribute
  108. *
  109. * @return $this The builder instance (for fluid interface)
  110. */
  111. public function addAttribute($attribute) {
  112. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  113. return $this;
  114. }
  115. /**
  116. * Returns the built parameter node.
  117. *
  118. * @return Node\Param The built parameter node
  119. */
  120. public function getNode(): Node {
  121. return new Node\Param(
  122. new Node\Expr\Variable($this->name),
  123. $this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups
  124. );
  125. }
  126. }