1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leaditin\Code\Member; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Leaditin\Code\Argument; |
7
|
|
|
use Leaditin\Code\DocBlock; |
8
|
|
|
use Leaditin\Code\Flag; |
9
|
|
|
use Leaditin\Code\Type; |
10
|
|
|
use Leaditin\Code\Visibility; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package Leaditin\Code |
14
|
|
|
* @author Igor Vuckovic <[email protected]> |
15
|
|
|
* @license MIT |
16
|
|
|
*/ |
17
|
|
|
class Method |
18
|
|
|
{ |
19
|
|
|
public const SCOPE_CLASS = 'class'; |
20
|
|
|
public const SCOPE_INTERFACE = 'interface'; |
21
|
|
|
public const SCOPE_TRAIT = 'trait'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Flag |
30
|
|
|
*/ |
31
|
|
|
protected $flag; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Argument[] |
35
|
|
|
*/ |
36
|
|
|
protected $arguments; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var null|string |
40
|
|
|
*/ |
41
|
|
|
protected $body; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var null|DocBlock |
45
|
|
|
*/ |
46
|
|
|
protected $docBlock; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var null|Type |
50
|
|
|
*/ |
51
|
|
|
protected $returnType; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $scope; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param null|Flag $flag |
62
|
|
|
* @param null|Argument[] $arguments |
63
|
|
|
* @param null|string $body |
64
|
|
|
* @param null|DocBlock $docBlock |
65
|
|
|
* @param null|Type $returnType |
66
|
|
|
*/ |
67
|
13 |
|
public function __construct( |
68
|
|
|
string $name, |
69
|
|
|
Flag $flag = null, |
70
|
|
|
array $arguments = null, |
71
|
|
|
string $body = null, |
72
|
|
|
DocBlock $docBlock = null, |
73
|
|
|
Type $returnType = null |
74
|
|
|
) { |
75
|
13 |
|
$this->name = $name; |
76
|
13 |
|
$this->setFlag($flag); |
77
|
13 |
|
$this->setArguments($arguments ?? []); |
78
|
12 |
|
$this->body = $body; |
79
|
12 |
|
$this->docBlock = $docBlock; |
80
|
12 |
|
$this->returnType = $returnType; |
81
|
12 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
2 |
|
public function name(): string |
87
|
|
|
{ |
88
|
2 |
|
return $this->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Argument[] |
93
|
|
|
*/ |
94
|
2 |
|
public function arguments(): array |
95
|
|
|
{ |
96
|
2 |
|
return $this->arguments; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return null|string |
101
|
|
|
*/ |
102
|
2 |
|
public function body(): ?string |
103
|
|
|
{ |
104
|
2 |
|
return $this->body; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return null|DocBlock |
109
|
|
|
*/ |
110
|
2 |
|
public function docBlock(): ?DocBlock |
111
|
|
|
{ |
112
|
2 |
|
return $this->docBlock; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return null|Type |
117
|
|
|
*/ |
118
|
2 |
|
public function returnType(): ?Type |
119
|
|
|
{ |
120
|
2 |
|
return $this->returnType; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param bool $isAbstract |
125
|
|
|
*/ |
126
|
1 |
|
public function setAbstract(bool $isAbstract): void |
127
|
|
|
{ |
128
|
1 |
|
$isAbstract ? $this->flag->addFlag(Flag::FLAG_ABSTRACT) : $this->flag->removeFlag(Flag::FLAG_ABSTRACT); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
1 |
|
public function isAbstract(): bool |
135
|
|
|
{ |
136
|
1 |
|
return $this->flag->hasFlag(Flag::FLAG_ABSTRACT); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param bool $isFinal |
141
|
|
|
*/ |
142
|
1 |
|
public function setFinal(bool $isFinal): void |
143
|
|
|
{ |
144
|
1 |
|
$isFinal ? $this->flag->addFlag(Flag::FLAG_FINAL) : $this->flag->removeFlag(Flag::FLAG_FINAL); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
1 |
|
public function isFinal(): bool |
151
|
|
|
{ |
152
|
1 |
|
return $this->flag->hasFlag(Flag::FLAG_FINAL); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param bool $isStatic |
157
|
|
|
*/ |
158
|
1 |
|
public function setStatic(bool $isStatic): void |
159
|
|
|
{ |
160
|
1 |
|
$isStatic ? $this->flag->addFlag(Flag::FLAG_STATIC) : $this->flag->removeFlag(Flag::FLAG_STATIC); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
1 |
|
public function isStatic(): bool |
167
|
|
|
{ |
168
|
1 |
|
return $this->flag->hasFlag(Flag::FLAG_STATIC); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $scope |
173
|
|
|
*/ |
174
|
3 |
|
public function setScope(string $scope): void |
175
|
|
|
{ |
176
|
3 |
|
$this->scope = $scope; |
177
|
3 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
3 |
|
public function scope(): string |
183
|
|
|
{ |
184
|
3 |
|
return $this->scope ?? static::SCOPE_CLASS; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return Visibility |
189
|
|
|
*/ |
190
|
4 |
|
public function visibility(): Visibility |
191
|
|
|
{ |
192
|
4 |
|
return Visibility::fromFlag($this->flag); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param null|Flag $flag |
197
|
|
|
*/ |
198
|
13 |
|
protected function setFlag(?Flag $flag): void |
199
|
|
|
{ |
200
|
13 |
|
if ($flag === null) { |
201
|
9 |
|
$flag = new Flag(Flag::FLAG_PUBLIC); |
202
|
|
|
} |
203
|
|
|
|
204
|
13 |
|
$this->flag = $flag; |
205
|
13 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Argument[] $arguments |
209
|
|
|
*/ |
210
|
13 |
|
protected function setArguments(array $arguments): void |
211
|
|
|
{ |
212
|
13 |
|
$this->arguments = []; |
213
|
|
|
|
214
|
13 |
|
foreach ($arguments as $argument) { |
215
|
2 |
|
if (!$argument instanceof Argument) { |
216
|
1 |
|
throw new InvalidArgumentException(sprintf('Argument must be instance of %s', Argument::class)); |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
$this->arguments[] = $argument; |
220
|
|
|
} |
221
|
12 |
|
} |
222
|
|
|
} |
223
|
|
|
|