Method.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }