Property.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /**
  22. * Creates a property builder.
  23. *
  24. * @param string $name Name of the property
  25. */
  26. public function __construct(string $name) {
  27. $this->name = $name;
  28. }
  29. /**
  30. * Makes the property public.
  31. *
  32. * @return $this The builder instance (for fluid interface)
  33. */
  34. public function makePublic() {
  35. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
  36. return $this;
  37. }
  38. /**
  39. * Makes the property protected.
  40. *
  41. * @return $this The builder instance (for fluid interface)
  42. */
  43. public function makeProtected() {
  44. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
  45. return $this;
  46. }
  47. /**
  48. * Makes the property private.
  49. *
  50. * @return $this The builder instance (for fluid interface)
  51. */
  52. public function makePrivate() {
  53. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
  54. return $this;
  55. }
  56. /**
  57. * Makes the property static.
  58. *
  59. * @return $this The builder instance (for fluid interface)
  60. */
  61. public function makeStatic() {
  62. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC);
  63. return $this;
  64. }
  65. /**
  66. * Makes the property readonly.
  67. *
  68. * @return $this The builder instance (for fluid interface)
  69. */
  70. public function makeReadonly() {
  71. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
  72. return $this;
  73. }
  74. /**
  75. * Sets default value for the property.
  76. *
  77. * @param mixed $value Default value to use
  78. *
  79. * @return $this The builder instance (for fluid interface)
  80. */
  81. public function setDefault($value) {
  82. $this->default = BuilderHelpers::normalizeValue($value);
  83. return $this;
  84. }
  85. /**
  86. * Sets doc comment for the property.
  87. *
  88. * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
  89. *
  90. * @return $this The builder instance (for fluid interface)
  91. */
  92. public function setDocComment($docComment) {
  93. $this->attributes = [
  94. 'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
  95. ];
  96. return $this;
  97. }
  98. /**
  99. * Sets the property type for PHP 7.4+.
  100. *
  101. * @param string|Name|Identifier|ComplexType $type
  102. *
  103. * @return $this
  104. */
  105. public function setType($type) {
  106. $this->type = BuilderHelpers::normalizeType($type);
  107. return $this;
  108. }
  109. /**
  110. * Adds an attribute group.
  111. *
  112. * @param Node\Attribute|Node\AttributeGroup $attribute
  113. *
  114. * @return $this The builder instance (for fluid interface)
  115. */
  116. public function addAttribute($attribute) {
  117. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  118. return $this;
  119. }
  120. /**
  121. * Returns the built class node.
  122. *
  123. * @return Stmt\Property The built property node
  124. */
  125. public function getNode(): PhpParser\Node {
  126. return new Stmt\Property(
  127. $this->flags !== 0 ? $this->flags : Modifiers::PUBLIC,
  128. [
  129. new Node\PropertyItem($this->name, $this->default)
  130. ],
  131. $this->attributes,
  132. $this->type,
  133. $this->attributeGroups
  134. );
  135. }
  136. }