1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Builder; |
4: | |
5: | use PhpParser; |
6: | use PhpParser\BuilderHelpers; |
7: | use PhpParser\Node; |
8: | use PhpParser\Node\Stmt; |
9: | |
10: | class Method extends FunctionLike |
11: | { |
12: | protected $name; |
13: | protected $flags = 0; |
14: | |
15: | |
16: | protected $stmts = []; |
17: | |
18: | |
19: | protected $attributeGroups = []; |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | public function __construct(string $name) { |
27: | $this->name = $name; |
28: | } |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | public function makePublic() { |
36: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC); |
37: | |
38: | return $this; |
39: | } |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | public function makeProtected() { |
47: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED); |
48: | |
49: | return $this; |
50: | } |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | public function makePrivate() { |
58: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE); |
59: | |
60: | return $this; |
61: | } |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | public function makeStatic() { |
69: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC); |
70: | |
71: | return $this; |
72: | } |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | public function makeAbstract() { |
80: | if (!empty($this->stmts)) { |
81: | throw new \LogicException('Cannot make method with statements abstract'); |
82: | } |
83: | |
84: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT); |
85: | $this->stmts = null; |
86: | |
87: | return $this; |
88: | } |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | public function makeFinal() { |
96: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL); |
97: | |
98: | return $this; |
99: | } |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function addStmt($stmt) { |
109: | if (null === $this->stmts) { |
110: | throw new \LogicException('Cannot add statements to an abstract method'); |
111: | } |
112: | |
113: | $this->stmts[] = BuilderHelpers::normalizeStmt($stmt); |
114: | |
115: | return $this; |
116: | } |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | public function addAttribute($attribute) { |
126: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
127: | |
128: | return $this; |
129: | } |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | public function getNode() : Node { |
137: | return new Stmt\ClassMethod($this->name, [ |
138: | 'flags' => $this->flags, |
139: | 'byRef' => $this->returnByRef, |
140: | 'params' => $this->params, |
141: | 'returnType' => $this->returnType, |
142: | 'stmts' => $this->stmts, |
143: | 'attrGroups' => $this->attributeGroups, |
144: | ], $this->attributes); |
145: | } |
146: | } |
147: | |