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