Class_.php 3.1 KB

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