Ingen beskrivning

Param.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /** @var Node\AttributeGroup[] */
  15. protected $attributeGroups = [];
  16. /**
  17. * Creates a parameter builder.
  18. *
  19. * @param string $name Name of the parameter
  20. */
  21. public function __construct(string $name) {
  22. $this->name = $name;
  23. }
  24. /**
  25. * Sets default value for the parameter.
  26. *
  27. * @param mixed $value Default value to use
  28. *
  29. * @return $this The builder instance (for fluid interface)
  30. */
  31. public function setDefault($value) {
  32. $this->default = BuilderHelpers::normalizeValue($value);
  33. return $this;
  34. }
  35. /**
  36. * Sets type for the parameter.
  37. *
  38. * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
  39. *
  40. * @return $this The builder instance (for fluid interface)
  41. */
  42. public function setType($type) {
  43. $this->type = BuilderHelpers::normalizeType($type);
  44. if ($this->type == 'void') {
  45. throw new \LogicException('Parameter type cannot be void');
  46. }
  47. return $this;
  48. }
  49. /**
  50. * Sets type for the parameter.
  51. *
  52. * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
  53. *
  54. * @return $this The builder instance (for fluid interface)
  55. *
  56. * @deprecated Use setType() instead
  57. */
  58. public function setTypeHint($type) {
  59. return $this->setType($type);
  60. }
  61. /**
  62. * Make the parameter accept the value by reference.
  63. *
  64. * @return $this The builder instance (for fluid interface)
  65. */
  66. public function makeByRef() {
  67. $this->byRef = true;
  68. return $this;
  69. }
  70. /**
  71. * Make the parameter variadic
  72. *
  73. * @return $this The builder instance (for fluid interface)
  74. */
  75. public function makeVariadic() {
  76. $this->variadic = true;
  77. return $this;
  78. }
  79. /**
  80. * Adds an attribute group.
  81. *
  82. * @param Node\Attribute|Node\AttributeGroup $attribute
  83. *
  84. * @return $this The builder instance (for fluid interface)
  85. */
  86. public function addAttribute($attribute) {
  87. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  88. return $this;
  89. }
  90. /**
  91. * Returns the built parameter node.
  92. *
  93. * @return Node\Param The built parameter node
  94. */
  95. public function getNode() : Node {
  96. return new Node\Param(
  97. new Node\Expr\Variable($this->name),
  98. $this->default, $this->type, $this->byRef, $this->variadic, [], 0, $this->attributeGroups
  99. );
  100. }
  101. }