|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ClassGeneration package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antonio Spinelli <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ClassGeneration; |
|
13
|
|
|
|
|
14
|
|
|
use ClassGeneration\DocBlock\Tag; |
|
15
|
|
|
use ClassGeneration\DocBlock\TagInterface; |
|
16
|
|
|
use ClassGeneration\Element\ElementAbstract; |
|
17
|
|
|
use ClassGeneration\Element\ElementInterface; |
|
18
|
|
|
use ClassGeneration\PhpClassInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Method ClassGeneration |
|
22
|
|
|
* @author Antonio Spinelli <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Method extends ElementAbstract implements MethodInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Method's name |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $name; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Method's arguments. |
|
35
|
|
|
* @var ArgumentCollection |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $arguments; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Method's code. |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $code; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $visibility = Visibility::TYPE_PUBLIC; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Sets like a final. |
|
52
|
|
|
* @var bool |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $isFinal; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sets like an abstract. |
|
58
|
|
|
* @var bool |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $isAbstract; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Sets like an interface. |
|
64
|
|
|
* @var bool |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $isInterface; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sets like a static. |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $isStatic; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Documentation Block. |
|
76
|
|
|
* @var DocBlockInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $docBlock; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
32 |
|
public function init() |
|
84
|
|
|
{ |
|
85
|
32 |
|
$this->setArgumentCollection(new ArgumentCollection()); |
|
86
|
32 |
|
$this->setDocBlock(new DocBlock()); |
|
87
|
32 |
|
$this->setVisibility(Visibility::TYPE_PUBLIC); |
|
88
|
32 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return PhpClass |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getParent() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return parent::getParent(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
13 |
|
public function setParent(ElementInterface $parent) |
|
102
|
|
|
{ |
|
103
|
13 |
|
if (!$parent instanceof PhpClassInterface) { |
|
104
|
1 |
|
throw new \InvalidArgumentException('Only accept instances from ClassGeneration\PhpClassInterface'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
13 |
|
parent::setParent($parent); |
|
108
|
|
|
|
|
109
|
13 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
24 |
|
public function getName() |
|
116
|
|
|
{ |
|
117
|
24 |
|
return $this->name; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritdoc |
|
122
|
|
|
*/ |
|
123
|
26 |
|
public function setName($name) |
|
124
|
|
|
{ |
|
125
|
26 |
|
$this->name = lcfirst(str_replace(" ", "", ucwords(strtr($name, "_-", " ")))); |
|
126
|
26 |
|
$this->setCode('//TODO: implements the ' . $this->name . ' method'); |
|
127
|
|
|
|
|
128
|
26 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @inheritdoc |
|
133
|
|
|
*/ |
|
134
|
18 |
|
public function getArgumentCollection() |
|
135
|
|
|
{ |
|
136
|
18 |
|
return $this->arguments; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritdoc |
|
141
|
|
|
*/ |
|
142
|
8 |
|
public function addArgument(ArgumentInterface $argument) |
|
143
|
|
|
{ |
|
144
|
8 |
|
$this->getArgumentCollection()->add($argument); |
|
145
|
8 |
|
$tag = Tag::createFromArgument($argument); |
|
146
|
8 |
|
$this->getDocBlock()->addTag($tag); |
|
147
|
|
|
|
|
148
|
8 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @inheritdoc |
|
153
|
|
|
*/ |
|
154
|
32 |
|
public function setArgumentCollection(ArgumentCollection $arguments) |
|
155
|
|
|
{ |
|
156
|
32 |
|
$this->arguments = $arguments; |
|
157
|
|
|
|
|
158
|
32 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @inheritdoc |
|
163
|
|
|
* @return MethodInterface |
|
164
|
|
|
*/ |
|
165
|
7 |
|
public function setDescription($description) |
|
166
|
|
|
{ |
|
167
|
7 |
|
$this->docBlock->setDescription($description); |
|
168
|
|
|
|
|
169
|
7 |
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @inheritdoc |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function getDescription() |
|
176
|
|
|
{ |
|
177
|
1 |
|
return $this->docBlock->getDescription(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @inheritdoc |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function setReturns(TagInterface $tag) |
|
184
|
|
|
{ |
|
185
|
1 |
|
$tag->setName(TagInterface::TAG_RETURN); |
|
186
|
1 |
|
$this->getDocBlock()->addTag($tag); |
|
187
|
|
|
|
|
188
|
1 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @inheritdoc |
|
193
|
|
|
*/ |
|
194
|
1 |
|
public function getReturns() |
|
195
|
|
|
{ |
|
196
|
1 |
|
return $this->getDocBlock()->getTagsByName('return')->current(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @inheritdoc |
|
201
|
|
|
*/ |
|
202
|
12 |
|
public function getCode() |
|
203
|
|
|
{ |
|
204
|
12 |
|
return $this->code; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @inheritdoc |
|
209
|
|
|
*/ |
|
210
|
27 |
|
public function setCode($code) |
|
211
|
|
|
{ |
|
212
|
27 |
|
$this->code = $code; |
|
213
|
|
|
|
|
214
|
27 |
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* {@inheritdoc} |
|
219
|
|
|
*/ |
|
220
|
16 |
|
public function getVisibility() |
|
221
|
|
|
{ |
|
222
|
16 |
|
return $this->visibility; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @inheritdoc |
|
227
|
|
|
* @return Method |
|
228
|
|
|
*/ |
|
229
|
32 |
|
public function setVisibility($visibility) |
|
230
|
|
|
{ |
|
231
|
32 |
|
Visibility::isValid($visibility); |
|
232
|
32 |
|
$this->visibility = $visibility; |
|
233
|
|
|
|
|
234
|
32 |
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* {@inheritdoc} |
|
239
|
|
|
*/ |
|
240
|
16 |
|
public function isFinal() |
|
241
|
|
|
{ |
|
242
|
16 |
|
return $this->isFinal; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* {@inheritdoc} |
|
247
|
|
|
* @return Method |
|
248
|
|
|
*/ |
|
249
|
2 |
|
public function setIsFinal($isFinal = true) |
|
250
|
|
|
{ |
|
251
|
2 |
|
$this->isFinal = (bool)$isFinal; |
|
252
|
|
|
|
|
253
|
2 |
|
return $this; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* {@inheritdoc} |
|
258
|
|
|
*/ |
|
259
|
18 |
|
public function isAbstract() |
|
260
|
|
|
{ |
|
261
|
18 |
|
return $this->isAbstract; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* {@inheritdoc} |
|
266
|
|
|
* @return Method |
|
267
|
|
|
*/ |
|
268
|
5 |
|
public function setIsAbstract($isAbstract = true) |
|
269
|
|
|
{ |
|
270
|
5 |
|
if ($this->isInterface()) { |
|
271
|
1 |
|
throw new \RuntimeException('This method is an interface and it not be an abstract too.'); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
4 |
|
$this->isAbstract = (bool)$isAbstract; |
|
275
|
|
|
|
|
276
|
4 |
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* {@inheritdoc} |
|
281
|
|
|
*/ |
|
282
|
16 |
|
public function isStatic() |
|
283
|
|
|
{ |
|
284
|
16 |
|
return $this->isStatic; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* {@inheritdoc} |
|
289
|
|
|
* @return Method |
|
290
|
|
|
*/ |
|
291
|
2 |
|
public function setIsStatic($isStatic = true) |
|
292
|
|
|
{ |
|
293
|
2 |
|
$this->isStatic = (bool)$isStatic; |
|
294
|
|
|
|
|
295
|
2 |
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @inheritdoc |
|
300
|
|
|
*/ |
|
301
|
18 |
|
public function isInterface() |
|
302
|
|
|
{ |
|
303
|
18 |
|
return $this->isInterface; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @inheritdoc |
|
308
|
|
|
* @return Method |
|
309
|
|
|
*/ |
|
310
|
4 |
|
public function setIsInterface($isInterface = true) |
|
311
|
|
|
{ |
|
312
|
4 |
|
if ($this->isAbstract()) { |
|
313
|
1 |
|
throw new \RuntimeException('This method is an abstract and it not be an interface too.'); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
3 |
|
$this->isInterface = (bool)$isInterface; |
|
317
|
|
|
|
|
318
|
3 |
|
return $this; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* {@inheritdoc} |
|
323
|
|
|
*/ |
|
324
|
17 |
|
public function getDocBlock() |
|
325
|
|
|
{ |
|
326
|
17 |
|
return $this->docBlock; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* {@inheritdoc} |
|
331
|
|
|
* @return Method |
|
332
|
|
|
*/ |
|
333
|
32 |
|
public function setDocBlock(DocBlockInterface $docBlock) |
|
334
|
|
|
{ |
|
335
|
32 |
|
$this->docBlock = $docBlock; |
|
336
|
|
|
|
|
337
|
32 |
|
return $this; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
11 |
|
protected function codeToString() |
|
341
|
|
|
{ |
|
342
|
11 |
|
$this->setTabulation($this->getTabulation() * 2); |
|
343
|
11 |
|
$tabulationFormatted = $this->getTabulationFormatted(); |
|
344
|
|
|
|
|
345
|
|
|
$code = PHP_EOL |
|
346
|
11 |
|
. $tabulationFormatted |
|
347
|
11 |
|
. preg_replace("/\n/", PHP_EOL . $tabulationFormatted, $this->getCode()) |
|
348
|
11 |
|
. PHP_EOL; |
|
349
|
|
|
|
|
350
|
11 |
|
return $code; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @inheritdoc |
|
355
|
|
|
*/ |
|
356
|
16 |
|
public function toString() |
|
357
|
|
|
{ |
|
358
|
16 |
|
$tabulationFormatted = $this->getTabulationFormatted(); |
|
359
|
|
|
|
|
360
|
16 |
|
$code = $this->toStringCode(); |
|
361
|
|
|
|
|
362
|
16 |
|
$method = $this->getDocBlock()->toString() |
|
363
|
|
|
. $tabulationFormatted |
|
364
|
16 |
|
. $this->toStringFunction() |
|
365
|
16 |
|
. $this->getName() |
|
366
|
16 |
|
. '(' |
|
367
|
16 |
|
. $this->getArgumentCollection()->toString() |
|
368
|
16 |
|
. ')' |
|
369
|
16 |
|
. $code |
|
370
|
16 |
|
. PHP_EOL; |
|
371
|
|
|
|
|
372
|
16 |
|
return $method; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Get string with method type. |
|
377
|
|
|
* @return string |
|
378
|
|
|
*/ |
|
379
|
16 |
|
protected function toStringFunction() |
|
380
|
|
|
{ |
|
381
|
16 |
|
$string = ($this->isFinal() ? 'final ' : '') |
|
382
|
16 |
|
. ($this->isAbstract() ? 'abstract ' : '') |
|
383
|
16 |
|
. $this->getVisibility() . ' ' |
|
384
|
16 |
|
. ($this->isStatic() ? 'static ' : '') |
|
385
|
16 |
|
. 'function '; |
|
386
|
|
|
|
|
387
|
16 |
|
return $string; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* Get string code. |
|
392
|
|
|
* @return string |
|
393
|
|
|
*/ |
|
394
|
16 |
|
public function toStringCode() |
|
395
|
|
|
{ |
|
396
|
16 |
|
if (!$this->isInterface() && !$this->isAbstract()) { |
|
397
|
11 |
|
$tabulationFormatted = $this->getTabulationFormatted(); |
|
398
|
11 |
|
$code = PHP_EOL . $tabulationFormatted |
|
399
|
11 |
|
. '{' |
|
400
|
11 |
|
. $this->codeToString() |
|
401
|
11 |
|
. $tabulationFormatted |
|
402
|
11 |
|
. '}'; |
|
403
|
|
|
|
|
404
|
11 |
|
return $code; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
5 |
|
return ';'; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* Create a Get Method from Property of Class. |
|
412
|
|
|
* |
|
413
|
|
|
* @param PropertyInterface $property |
|
414
|
|
|
* |
|
415
|
|
|
* @return Method |
|
416
|
|
|
*/ |
|
417
|
1 |
|
public static function createGetterFromProperty(PropertyInterface $property) |
|
418
|
|
|
{ |
|
419
|
1 |
|
$method = new self(); |
|
420
|
1 |
|
$method->setName('get_' . $property->getName()); |
|
421
|
1 |
|
$method->setCode('return $this->' . $property->getName() . ';'); |
|
422
|
|
|
|
|
423
|
1 |
|
return $method; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Generate Set Method from Property. |
|
428
|
|
|
* Add a set method in the class based on Object Property. |
|
429
|
|
|
* |
|
430
|
|
|
* @param PropertyInterface $property |
|
431
|
|
|
* |
|
432
|
|
|
* @return Method |
|
433
|
|
|
*/ |
|
434
|
1 |
|
public static function createSetterFromProperty(PropertyInterface $property) |
|
435
|
|
|
{ |
|
436
|
1 |
|
$argument = Argument::createFromProperty($property); |
|
437
|
|
|
|
|
438
|
1 |
|
$code = "\$this->{$property->getName()} = {$argument->getNameFormatted()};" |
|
439
|
|
|
. PHP_EOL |
|
440
|
1 |
|
. 'return $this;'; |
|
441
|
|
|
|
|
442
|
1 |
|
$method = new self; |
|
443
|
|
|
$method |
|
444
|
1 |
|
->setName('set_' . $property->getName()) |
|
445
|
1 |
|
->setCode($code) |
|
446
|
1 |
|
->getArgumentCollection()->add($argument); |
|
447
|
|
|
|
|
448
|
1 |
|
return $method; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** |
|
452
|
|
|
* Creates a method from Reflection Method. |
|
453
|
|
|
* |
|
454
|
|
|
* @param \ReflectionMethod $reflected |
|
455
|
|
|
* |
|
456
|
|
|
* @return Method |
|
457
|
|
|
*/ |
|
458
|
4 |
|
public static function createFromReflection(\ReflectionMethod $reflected) |
|
459
|
|
|
{ |
|
460
|
4 |
|
$method = new self(); |
|
461
|
4 |
|
$method->setName($reflected->getName()); |
|
462
|
|
|
|
|
463
|
4 |
|
foreach ($reflected->getParameters() as $parameterReflected) { |
|
464
|
2 |
|
$argument = new Argument(); |
|
465
|
2 |
|
$argument->setName($parameterReflected->getName()); |
|
466
|
2 |
|
$method->addArgument($argument); |
|
467
|
4 |
|
} |
|
468
|
|
|
|
|
469
|
4 |
|
return $method; |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|