Property.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. use PhpParser\Node\Identifier;
  8. use PhpParser\Node\Name;
  9. use PhpParser\Node\Stmt;
  10. use PhpParser\Node\ComplexType;
  11. class Property implements PhpParser\Builder {
  12. protected string $name;
  13. protected int $flags = 0;
  14. protected ?Node\Expr $default = null;
  15. /** @var array<string, mixed> */
  16. protected array $attributes = [];
  17. /** @var null|Identifier|Name|ComplexType */
  18. protected ?Node $type = null;
  19. /** @var list<Node\AttributeGroup> */
  20. protected array $attributeGroups = [];
  21. /** @var list<Node\PropertyHook> */
  22. protected array $hooks = [];
  23. /**
  24. * Creates a property builder.
  25. *
  26. * @param string $name Name of the property
  27. */
  28. public function __construct(string $name) {
  29. $this->name = $name;
  30. }
  31. /**
  32. * Makes the property public.
  33. *
  34. * @return $this The builder instance (for fluid interface)
  35. */
  36. public function makePublic() {
  37. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
  38. return $this;
  39. }
  40. /**
  41. * Makes the property protected.
  42. *
  43. * @return $this The builder instance (for fluid interface)
  44. */
  45. public function makeProtected() {
  46. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
  47. return $this;
  48. }
  49. /**
  50. * Makes the property private.
  51. *
  52. * @return $this The builder instance (for fluid interface)
  53. */
  54. public function makePrivate() {
  55. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
  56. return $this;
  57. }
  58. /**
  59. * Makes the property static.
  60. *
  61. * @return $this The builder instance (for fluid interface)
  62. */
  63. public function makeStatic() {
  64. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC);
  65. return $this;
  66. }
  67. /**
  68. * Makes the property readonly.
  69. *
  70. * @return $this The builder instance (for fluid interface)
  71. */
  72. public function makeReadonly() {
  73. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
  74. return $this;
  75. }
  76. /**
  77. * Makes the property abstract. Requires at least one property hook to be specified as well.
  78. *
  79. * @return $this The builder instance (for fluid interface)
  80. */
  81. public function makeAbstract() {
  82. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::ABSTRACT);
  83. return $this;
  84. }
  85. /**
  86. * Makes the property final.
  87. *
  88. * @return $this The builder instance (for fluid interface)
  89. */
  90. public function makeFinal() {
  91. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
  92. return $this;
  93. }
  94. /**
  95. * Gives the property private(set) visibility.
  96. *
  97. * @return $this The builder instance (for fluid interface)
  98. */
  99. public function makePrivateSet() {
  100. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE_SET);
  101. return $this;
  102. }
  103. /**
  104. * Gives the property protected(set) visibility.
  105. *
  106. * @return $this The builder instance (for fluid interface)
  107. */
  108. public function makeProtectedSet() {
  109. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED_SET);
  110. return $this;
  111. }
  112. /**
  113. * Sets default value for the property.
  114. *
  115. * @param mixed $value Default value to use
  116. *
  117. * @return $this The builder instance (for fluid interface)
  118. */
  119. public function setDefault($value) {
  120. $this->default = BuilderHelpers::normalizeValue($value);
  121. return $this;
  122. }
  123. /**
  124. * Sets doc comment for the property.
  125. *
  126. * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
  127. *
  128. * @return $this The builder instance (for fluid interface)
  129. */
  130. public function setDocComment($docComment) {
  131. $this->attributes = [
  132. 'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
  133. ];
  134. return $this;
  135. }
  136. /**
  137. * Sets the property type for PHP 7.4+.
  138. *
  139. * @param string|Name|Identifier|ComplexType $type
  140. *
  141. * @return $this
  142. */
  143. public function setType($type) {
  144. $this->type = BuilderHelpers::normalizeType($type);
  145. return $this;
  146. }
  147. /**
  148. * Adds an attribute group.
  149. *
  150. * @param Node\Attribute|Node\AttributeGroup $attribute
  151. *
  152. * @return $this The builder instance (for fluid interface)
  153. */
  154. public function addAttribute($attribute) {
  155. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  156. return $this;
  157. }
  158. /**
  159. * Adds a property hook.
  160. *
  161. * @return $this The builder instance (for fluid interface)
  162. */
  163. public function addHook(Node\PropertyHook $hook) {
  164. $this->hooks[] = $hook;
  165. return $this;
  166. }
  167. /**
  168. * Returns the built class node.
  169. *
  170. * @return Stmt\Property The built property node
  171. */
  172. public function getNode(): PhpParser\Node {
  173. if ($this->flags & Modifiers::ABSTRACT && !$this->hooks) {
  174. throw new PhpParser\Error('Only hooked properties may be declared abstract');
  175. }
  176. return new Stmt\Property(
  177. $this->flags !== 0 ? $this->flags : Modifiers::PUBLIC,
  178. [
  179. new Node\PropertyItem($this->name, $this->default)
  180. ],
  181. $this->attributes,
  182. $this->type,
  183. $this->attributeGroups,
  184. $this->hooks
  185. );
  186. }
  187. }