You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.7 KiB

3 years ago
  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\Stmt;
  7. class Method extends FunctionLike
  8. {
  9. protected $name;
  10. protected $flags = 0;
  11. /** @var array|null */
  12. protected $stmts = [];
  13. /** @var Node\AttributeGroup[] */
  14. protected $attributeGroups = [];
  15. /**
  16. * Creates a method builder.
  17. *
  18. * @param string $name Name of the method
  19. */
  20. public function __construct(string $name) {
  21. $this->name = $name;
  22. }
  23. /**
  24. * Makes the method public.
  25. *
  26. * @return $this The builder instance (for fluid interface)
  27. */
  28. public function makePublic() {
  29. $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
  30. return $this;
  31. }
  32. /**
  33. * Makes the method protected.
  34. *
  35. * @return $this The builder instance (for fluid interface)
  36. */
  37. public function makeProtected() {
  38. $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
  39. return $this;
  40. }
  41. /**
  42. * Makes the method private.
  43. *
  44. * @return $this The builder instance (for fluid interface)
  45. */
  46. public function makePrivate() {
  47. $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
  48. return $this;
  49. }
  50. /**
  51. * Makes the method static.
  52. *
  53. * @return $this The builder instance (for fluid interface)
  54. */
  55. public function makeStatic() {
  56. $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
  57. return $this;
  58. }
  59. /**
  60. * Makes the method abstract.
  61. *
  62. * @return $this The builder instance (for fluid interface)
  63. */
  64. public function makeAbstract() {
  65. if (!empty($this->stmts)) {
  66. throw new \LogicException('Cannot make method with statements abstract');
  67. }
  68. $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
  69. $this->stmts = null; // abstract methods don't have statements
  70. return $this;
  71. }
  72. /**
  73. * Makes the method final.
  74. *
  75. * @return $this The builder instance (for fluid interface)
  76. */
  77. public function makeFinal() {
  78. $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
  79. return $this;
  80. }
  81. /**
  82. * Adds a statement.
  83. *
  84. * @param Node|PhpParser\Builder $stmt The statement to add
  85. *
  86. * @return $this The builder instance (for fluid interface)
  87. */
  88. public function addStmt($stmt) {
  89. if (null === $this->stmts) {
  90. throw new \LogicException('Cannot add statements to an abstract method');
  91. }
  92. $this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
  93. return $this;
  94. }
  95. /**
  96. * Adds an attribute group.
  97. *
  98. * @param Node\Attribute|Node\AttributeGroup $attribute
  99. *
  100. * @return $this The builder instance (for fluid interface)
  101. */
  102. public function addAttribute($attribute) {
  103. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  104. return $this;
  105. }
  106. /**
  107. * Returns the built method node.
  108. *
  109. * @return Stmt\ClassMethod The built method node
  110. */
  111. public function getNode() : Node {
  112. return new Stmt\ClassMethod($this->name, [
  113. 'flags' => $this->flags,
  114. 'byRef' => $this->returnByRef,
  115. 'params' => $this->params,
  116. 'returnType' => $this->returnType,
  117. 'stmts' => $this->stmts,
  118. 'attrGroups' => $this->attributeGroups,
  119. ], $this->attributes);
  120. }
  121. }