Method.php 2.8 KB

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