PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PHPParser/Builder/Method.php

https://bitbucket.org/nilopc_repo/sf2_backup-php-parser
PHP | 187 lines | 83 code | 31 blank | 73 comment | 3 complexity | 6f566dfbe0f2af5057053d996ea2bcae MD5 | raw file
Possible License(s): BSD-3-Clause
  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. }