/
Method.php
459 lines (369 loc) 路 8.39 KB
/
Method.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\PhpGenerator;
use Nette;
/**
* Method or function description.
*/
class Method extends Nette\Object
{
/** @var string|NULL */
private $name;
/** @var array of name => Parameter */
private $parameters = [];
/** @var array of name => bool */
private $uses = [];
/** @var string|FALSE */
private $body = '';
/** @var bool */
private $static = FALSE;
/** @var string|NULL public|protected|private */
private $visibility;
/** @var bool */
private $final = FALSE;
/** @var bool */
private $abstract = FALSE;
/** @var bool */
private $returnReference = FALSE;
/** @var bool */
private $variadic = FALSE;
/** @var string|NULL */
private $comment;
/** @var PhpNamespace|NULL */
private $namespace;
/** @var string|NULL */
private $returnType;
/**
* @return self
*/
public static function from($from)
{
if (is_string($from) && strpos($from, '::')) {
$from = new \ReflectionMethod($from);
} elseif (is_array($from)) {
$from = new \ReflectionMethod($from[0], $from[1]);
} elseif (!$from instanceof \ReflectionFunctionAbstract) {
$from = new \ReflectionFunction($from);
}
$method = new static;
$method->name = $from->isClosure() ? NULL : $from->getName();
foreach ($from->getParameters() as $param) {
$method->parameters[$param->getName()] = Parameter::from($param);
}
if ($from instanceof \ReflectionMethod) {
$method->static = $from->isStatic();
$method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL);
$method->final = $from->isFinal();
$method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface();
$method->body = $from->isAbstract() ? FALSE : '';
}
$method->returnReference = $from->returnsReference();
$method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
$method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
$returnType = $from->getReturnType();
$method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType;
}
return $method;
}
/**
* @return string PHP code
*/
public function __toString()
{
static $builtinTypes = ['array', 'self', 'parent', 'callable', 'string', 'bool', 'float', 'int', NULL];
$parameters = [];
foreach ($this->parameters as $param) {
$variadic = $this->variadic && $param === end($this->parameters);
$hint = !$this->namespace || in_array($param->getTypeHint(), $builtinTypes, TRUE)
? $param->getTypeHint()
: $this->namespace->unresolveName($param->getTypeHint());
$parameters[] = ($hint ? $hint . ' ' : '')
. ($param->isReference() ? '&' : '')
. ($variadic ? '...' : '')
. '$' . $param->getName()
. ($param->isOptional() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
}
$uses = [];
foreach ($this->uses as $param) {
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
}
$returnType = !$this->namespace || in_array($this->returnType, $builtinTypes, TRUE)
? $this->returnType
: $this->namespace->unresolveName($this->returnType);
return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
. ($this->abstract ? 'abstract ' : '')
. ($this->final ? 'final ' : '')
. ($this->visibility ? $this->visibility . ' ' : '')
. ($this->static ? 'static ' : '')
. 'function'
. ($this->returnReference ? ' &' : '')
. ' ' . $this->name
. '(' . implode(', ', $parameters) . ')'
. ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
. ($returnType ? ': ' . $returnType : '')
. ($this->abstract || $this->body === FALSE ? ';'
: ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
}
/**
* @param string|NULL
* @return self
*/
public function setName($name)
{
$this->name = $name ? (string) $name : NULL;
return $this;
}
/**
* @return string|NULL
*/
public function getName()
{
return $this->name;
}
/**
* @param Parameter[]
* @return self
*/
public function setParameters(array $val)
{
foreach ($val as $v) {
if (!$v instanceof Parameter) {
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].');
}
}
$this->parameters = $val;
return $this;
}
/**
* @return Parameter[]
*/
public function getParameters()
{
return $this->parameters;
}
/**
* @param string without $
* @return Parameter
*/
public function addParameter($name, $defaultValue = NULL)
{
$param = new Parameter;
if (func_num_args() > 1) {
$param->setOptional(TRUE)->setDefaultValue($defaultValue);
}
return $this->parameters[$name] = $param->setName($name);
}
/**
* @return self
*/
public function setUses(array $val)
{
$this->uses = $val;
return $this;
}
/**
* @return array
*/
public function getUses()
{
return $this->uses;
}
/**
* @return Parameter
*/
public function addUse($name)
{
$param = new Parameter;
return $this->uses[] = $param->setName($name);
}
/**
* @return self
*/
public function setBody($statement, array $args = NULL)
{
$this->body = func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement;
return $this;
}
/**
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* @return self
*/
public function addBody($statement, array $args = NULL)
{
$this->body .= (func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement) . "\n";
return $this;
}
/**
* @param bool
* @return self
*/
public function setStatic($val)
{
$this->static = (bool) $val;
return $this;
}
/**
* @return bool
*/
public function isStatic()
{
return $this->static;
}
/**
* @param string|NULL public|protected|private
* @return self
*/
public function setVisibility($val)
{
if (!in_array($val, ['public', 'protected', 'private', NULL], TRUE)) {
throw new Nette\InvalidArgumentException('Argument must be public|protected|private|NULL.');
}
$this->visibility = $val ? (string) $val : NULL;
return $this;
}
/**
* @return string
*/
public function getVisibility()
{
return $this->visibility;
}
/**
* @param bool
* @return self
*/
public function setFinal($val)
{
$this->final = (bool) $val;
return $this;
}
/**
* @return bool
*/
public function isFinal()
{
return $this->final;
}
/**
* @param bool
* @return self
*/
public function setAbstract($val)
{
$this->abstract = (bool) $val;
return $this;
}
/**
* @return bool
*/
public function isAbstract()
{
return $this->abstract;
}
/**
* @param bool
* @return self
*/
public function setReturnReference($val)
{
$this->returnReference = (bool) $val;
return $this;
}
/**
* @return bool
*/
public function getReturnReference()
{
return $this->returnReference;
}
/**
* @param bool
* @return self
*/
public function setVariadic($val)
{
$this->variadic = (bool) $val;
return $this;
}
/**
* @return bool
*/
public function isVariadic()
{
return $this->variadic;
}
/**
* @param string|NULL
* @return self
*/
public function setComment($val)
{
$this->comment = $val ? (string) $val : NULL;
return $this;
}
/**
* @return string|NULL
*/
public function getComment()
{
return $this->comment;
}
/**
* @param string
* @return self
*/
public function addComment($val)
{
$this->comment .= $this->comment ? "\n$val" : $val;
return $this;
}
/** @deprecated */
public function setDocuments(array $s)
{
return $this->setComment(implode("\n", $s));
}
/** @deprecated */
public function getDocuments()
{
return $this->comment ? [$this->comment] : [];
}
/** @deprecated */
public function addDocument($s)
{
return $this->addComment($s);
}
/**
* @return self
*/
public function setNamespace(PhpNamespace $val = NULL)
{
$this->namespace = $val;
return $this;
}
/**
* @param string|NULL
* @return self
*/
public function setReturnType($val)
{
$this->returnType = $val ? (string) $val : NULL;
return $this;
}
/**
* @return string|NULL
*/
public function getReturnType()
{
return $this->returnType;
}
}