Method.php 3.2 KB

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