Method.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. class PHPParser_Builder_Method extends PHPParser_BuilderAbstract
  3. {
  4. protected $name;
  5. protected $type;
  6. protected $returnByRef;
  7. protected $params;
  8. protected $stmts;
  9. /**
  10. * Creates a method builder.
  11. *
  12. * @param string $name Name of the method
  13. */
  14. public function __construct($name) {
  15. $this->name = $name;
  16. $this->type = 0;
  17. $this->returnByRef = false;
  18. $this->params = array();
  19. $this->stmts = array();
  20. }
  21. /**
  22. * Makes the method public.
  23. *
  24. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  25. */
  26. public function makePublic() {
  27. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC);
  28. return $this;
  29. }
  30. /**
  31. * Makes the method protected.
  32. *
  33. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  34. */
  35. public function makeProtected() {
  36. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED);
  37. return $this;
  38. }
  39. /**
  40. * Makes the method private.
  41. *
  42. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  43. */
  44. public function makePrivate() {
  45. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE);
  46. return $this;
  47. }
  48. /**
  49. * Makes the method static.
  50. *
  51. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  52. */
  53. public function makeStatic() {
  54. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_STATIC);
  55. return $this;
  56. }
  57. /**
  58. * Makes the method abstract.
  59. *
  60. * @return PHPParser_Builder_Method 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->setModifier(PHPParser_Node_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 PHPParser_Builder_Method The builder instance (for fluid interface)
  74. */
  75. public function makeFinal() {
  76. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_FINAL);
  77. return $this;
  78. }
  79. /**
  80. * Make the method return by reference.
  81. *
  82. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  83. */
  84. public function makeReturnByRef() {
  85. $this->returnByRef = true;
  86. return $this;
  87. }
  88. /**
  89. * Adds a parameter.
  90. *
  91. * @param PHPParser_Node_Param|PHPParser_Builder_Param $param The parameter to add
  92. *
  93. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  94. */
  95. public function addParam($param) {
  96. $param = $this->normalizeNode($param);
  97. if (!$param instanceof PHPParser_Node_Param) {
  98. throw new LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
  99. }
  100. $this->params[] = $param;
  101. return $this;
  102. }
  103. /**
  104. * Adds multiple parameters.
  105. *
  106. * @param array $params The parameters to add
  107. *
  108. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  109. */
  110. public function addParams(array $params) {
  111. foreach ($params as $param) {
  112. $this->addParam($param);
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Adds a statement.
  118. *
  119. * @param PHPParser_Node|PHPParser_Builder $stmt The statement to add
  120. *
  121. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  122. */
  123. public function addStmt($stmt) {
  124. if (null === $this->stmts) {
  125. throw new LogicException('Cannot add statements to an abstract method');
  126. }
  127. $this->stmts[] = $this->normalizeNode($stmt);
  128. return $this;
  129. }
  130. /**
  131. * Adds multiple statements.
  132. *
  133. * @param array $stmts The statements to add
  134. *
  135. * @return PHPParser_Builder_Method The builder instance (for fluid interface)
  136. */
  137. public function addStmts(array $stmts) {
  138. foreach ($stmts as $stmt) {
  139. $this->addStmt($stmt);
  140. }
  141. return $this;
  142. }
  143. /**
  144. * Returns the built method node.
  145. *
  146. * @return PHPParser_Node_Stmt_ClassMethod The built method node
  147. */
  148. public function getNode() {
  149. return new PHPParser_Node_Stmt_ClassMethod($this->name, array(
  150. 'type' => $this->type !== 0 ? $this->type : PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
  151. 'byRef' => $this->returnByRef,
  152. 'params' => $this->params,
  153. 'stmts' => $this->stmts,
  154. ));
  155. }
  156. }