Param.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\BuilderHelpers;
  5. use PhpParser\Node;
  6. class Param implements PhpParser\Builder
  7. {
  8. protected $name;
  9. protected $default = null;
  10. /** @var Node\Identifier|Node\Name|Node\NullableType|null */
  11. protected $type = null;
  12. protected $byRef = false;
  13. protected $variadic = false;
  14. protected $flags = 0;
  15. /** @var Node\AttributeGroup[] */
  16. protected $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. * Sets type for the parameter.
  52. *
  53. * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
  54. *
  55. * @return $this The builder instance (for fluid interface)
  56. *
  57. * @deprecated Use setType() instead
  58. */
  59. public function setTypeHint($type) {
  60. return $this->setType($type);
  61. }
  62. /**
  63. * Make the parameter accept the value by reference.
  64. *
  65. * @return $this The builder instance (for fluid interface)
  66. */
  67. public function makeByRef() {
  68. $this->byRef = true;
  69. return $this;
  70. }
  71. /**
  72. * Make the parameter variadic
  73. *
  74. * @return $this The builder instance (for fluid interface)
  75. */
  76. public function makeVariadic() {
  77. $this->variadic = true;
  78. return $this;
  79. }
  80. /**
  81. * Makes the (promoted) parameter public.
  82. *
  83. * @return $this The builder instance (for fluid interface)
  84. */
  85. public function makePublic() {
  86. $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PUBLIC);
  87. return $this;
  88. }
  89. /**
  90. * Makes the (promoted) parameter protected.
  91. *
  92. * @return $this The builder instance (for fluid interface)
  93. */
  94. public function makeProtected() {
  95. $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PROTECTED);
  96. return $this;
  97. }
  98. /**
  99. * Makes the (promoted) parameter private.
  100. *
  101. * @return $this The builder instance (for fluid interface)
  102. */
  103. public function makePrivate() {
  104. $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PRIVATE);
  105. return $this;
  106. }
  107. /**
  108. * Makes the (promoted) parameter readonly.
  109. *
  110. * @return $this The builder instance (for fluid interface)
  111. */
  112. public function makeReadonly() {
  113. $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_READONLY);
  114. return $this;
  115. }
  116. /**
  117. * Adds an attribute group.
  118. *
  119. * @param Node\Attribute|Node\AttributeGroup $attribute
  120. *
  121. * @return $this The builder instance (for fluid interface)
  122. */
  123. public function addAttribute($attribute) {
  124. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  125. return $this;
  126. }
  127. /**
  128. * Returns the built parameter node.
  129. *
  130. * @return Node\Param The built parameter node
  131. */
  132. public function getNode() : Node {
  133. return new Node\Param(
  134. new Node\Expr\Variable($this->name),
  135. $this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups
  136. );
  137. }
  138. }