菜谱项目

Method.php 2.9KB

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