Class_.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Name;
  8. use PhpParser\Node\Stmt;
  9. class Class_ extends Declaration {
  10. protected string $name;
  11. protected ?Name $extends = null;
  12. /** @var list<Name> */
  13. protected array $implements = [];
  14. protected int $flags = 0;
  15. /** @var list<Stmt\TraitUse> */
  16. protected array $uses = [];
  17. /** @var list<Stmt\ClassConst> */
  18. protected array $constants = [];
  19. /** @var list<Stmt\Property> */
  20. protected array $properties = [];
  21. /** @var list<Stmt\ClassMethod> */
  22. protected array $methods = [];
  23. /** @var list<Node\AttributeGroup> */
  24. protected array $attributeGroups = [];
  25. /**
  26. * Creates a class builder.
  27. *
  28. * @param string $name Name of the class
  29. */
  30. public function __construct(string $name) {
  31. $this->name = $name;
  32. }
  33. /**
  34. * Extends a class.
  35. *
  36. * @param Name|string $class Name of class to extend
  37. *
  38. * @return $this The builder instance (for fluid interface)
  39. */
  40. public function extend($class) {
  41. $this->extends = BuilderHelpers::normalizeName($class);
  42. return $this;
  43. }
  44. /**
  45. * Implements one or more interfaces.
  46. *
  47. * @param Name|string ...$interfaces Names of interfaces to implement
  48. *
  49. * @return $this The builder instance (for fluid interface)
  50. */
  51. public function implement(...$interfaces) {
  52. foreach ($interfaces as $interface) {
  53. $this->implements[] = BuilderHelpers::normalizeName($interface);
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Makes the class abstract.
  59. *
  60. * @return $this The builder instance (for fluid interface)
  61. */
  62. public function makeAbstract() {
  63. $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::ABSTRACT);
  64. return $this;
  65. }
  66. /**
  67. * Makes the class final.
  68. *
  69. * @return $this The builder instance (for fluid interface)
  70. */
  71. public function makeFinal() {
  72. $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::FINAL);
  73. return $this;
  74. }
  75. /**
  76. * Makes the class readonly.
  77. *
  78. * @return $this The builder instance (for fluid interface)
  79. */
  80. public function makeReadonly() {
  81. $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::READONLY);
  82. return $this;
  83. }
  84. /**
  85. * Adds a statement.
  86. *
  87. * @param Stmt|PhpParser\Builder $stmt The statement to add
  88. *
  89. * @return $this The builder instance (for fluid interface)
  90. */
  91. public function addStmt($stmt) {
  92. $stmt = BuilderHelpers::normalizeNode($stmt);
  93. if ($stmt instanceof Stmt\Property) {
  94. $this->properties[] = $stmt;
  95. } elseif ($stmt instanceof Stmt\ClassMethod) {
  96. $this->methods[] = $stmt;
  97. } elseif ($stmt instanceof Stmt\TraitUse) {
  98. $this->uses[] = $stmt;
  99. } elseif ($stmt instanceof Stmt\ClassConst) {
  100. $this->constants[] = $stmt;
  101. } else {
  102. throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Adds an attribute group.
  108. *
  109. * @param Node\Attribute|Node\AttributeGroup $attribute
  110. *
  111. * @return $this The builder instance (for fluid interface)
  112. */
  113. public function addAttribute($attribute) {
  114. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  115. return $this;
  116. }
  117. /**
  118. * Returns the built class node.
  119. *
  120. * @return Stmt\Class_ The built class node
  121. */
  122. public function getNode(): PhpParser\Node {
  123. return new Stmt\Class_($this->name, [
  124. 'flags' => $this->flags,
  125. 'extends' => $this->extends,
  126. 'implements' => $this->implements,
  127. 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
  128. 'attrGroups' => $this->attributeGroups,
  129. ], $this->attributes);
  130. }
  131. }