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