Class_.php 3.6 KB

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