/
ClassType.php
503 lines (410 loc) 路 9.43 KB
/
ClassType.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\PhpGenerator;
use Nette;
use Nette\Utils\Strings;
/**
* Class/Interface/Trait description.
*
* @property Method[] $methods
* @property Property[] $properties
*/
class ClassType
{
use Nette\SmartObject;
use Traits\CommentAware;
const TYPE_CLASS = 'class';
const TYPE_INTERFACE = 'interface';
const TYPE_TRAIT = 'trait';
/** @var PhpNamespace|NULL */
private $namespace;
/** @var string|NULL */
private $name;
/** @var string class|interface|trait */
private $type = 'class';
/** @var bool */
private $final = FALSE;
/** @var bool */
private $abstract = FALSE;
/** @var string|string[] */
private $extends = [];
/** @var string[] */
private $implements = [];
/** @var string[] */
private $traits = [];
/** @var Constant[] name => Constant */
private $consts = [];
/** @var Property[] name => Property */
private $properties = [];
/** @var Method[] name => Method */
private $methods = [];
/**
* @param string|object
* @return static
*/
public static function from($class)
{
if ($class instanceof \ReflectionClass) {
trigger_error(__METHOD__ . '() accepts only class name or object.', E_USER_DEPRECATED);
}
return (new Factory)->fromClassReflection(
$class instanceof \ReflectionClass ? $class : new \ReflectionClass($class)
);
}
/**
* @param string|NULL
*/
public function __construct($name = NULL, PhpNamespace $namespace = NULL)
{
$this->setName($name);
$this->namespace = $namespace;
}
/**
* @return string PHP code
*/
public function __toString()
{
$traits = [];
foreach ($this->traits as $trait => $resolutions) {
$traits[] = 'use ' . ($this->namespace ? $this->namespace->unresolveName($trait) : $trait)
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
}
$consts = [];
foreach ($this->consts as $const) {
$consts[] = Helpers::formatDocComment((string) $const->getComment())
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
. 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ';';
}
$properties = [];
foreach ($this->properties as $property) {
$properties[] = Helpers::formatDocComment((string) $property->getComment())
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
. ';';
}
$mapper = function (array $arr) {
return $this->namespace ? array_map([$this->namespace, 'unresolveName'], $arr) : $arr;
};
return Strings::normalize(
Helpers::formatDocComment($this->comment . "\n")
. ($this->abstract ? 'abstract ' : '')
. ($this->final ? 'final ' : '')
. ($this->name ? "$this->type $this->name " : '')
. ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '')
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
. ($this->name ? "\n" : '') . "{\n"
. Strings::indent(
($this->traits ? implode("\n", $traits) . "\n\n" : '')
. ($this->consts ? implode("\n", $consts) . "\n\n" : '')
. ($this->properties ? implode("\n\n", $properties) . "\n\n" : '')
. ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1)
. '}'
) . ($this->name ? "\n" : '');
}
/**
* @return PhpNamespace|NULL
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* @param string|NULL
* @return static
*/
public function setName($name)
{
if ($name !== NULL && !Helpers::isIdentifier($name)) {
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
}
$this->name = $name;
return $this;
}
/**
* @return string|NULL
*/
public function getName()
{
return $this->name;
}
/**
* @param string
* @return static
*/
public function setType($type)
{
if (!in_array($type, ['class', 'interface', 'trait'], TRUE)) {
throw new Nette\InvalidArgumentException('Argument must be class|interface|trait.');
}
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param bool
* @return static
*/
public function setFinal($state = TRUE)
{
$this->final = (bool) $state;
return $this;
}
/**
* @return bool
*/
public function isFinal()
{
return $this->final;
}
/**
* @param bool
* @return static
*/
public function setAbstract($state = TRUE)
{
$this->abstract = (bool) $state;
return $this;
}
/**
* @return bool
*/
public function isAbstract()
{
return $this->abstract;
}
/**
* @param string|string[]
* @return static
*/
public function setExtends($types)
{
if (!is_string($types) && !(is_array($types) && Nette\Utils\Arrays::every($types, 'is_string'))) {
throw new Nette\InvalidArgumentException('Argument must be string or string[].');
}
$this->extends = $types;
return $this;
}
/**
* @return string|string[]
*/
public function getExtends()
{
return $this->extends;
}
/**
* @param string
* @return static
*/
public function addExtend($type)
{
$this->extends = (array) $this->extends;
$this->extends[] = (string) $type;
return $this;
}
/**
* @param string[]
* @return static
*/
public function setImplements(array $types)
{
$this->implements = $types;
return $this;
}
/**
* @return string[]
*/
public function getImplements()
{
return $this->implements;
}
/**
* @param string
* @return static
*/
public function addImplement($type)
{
$this->implements[] = (string) $type;
return $this;
}
/**
* @param string[]
* @return static
*/
public function setTraits(array $traits)
{
$this->traits = array_fill_keys($traits, []);
return $this;
}
/**
* @return string[]
*/
public function getTraits()
{
return array_keys($this->traits);
}
/**
* @param string
* @return static
*/
public function addTrait($trait, array $resolutions = [])
{
$this->traits[$trait] = $resolutions;
return $this;
}
/**
* @deprecated use setConstants()
* @return static
*/
public function setConsts(array $consts)
{
trigger_error(__METHOD__ . '() is deprecated, use setConstants()', E_USER_DEPRECATED);
return $this->setConstants($consts);
}
/**
* @deprecated use getConstants()
* @return array
*/
public function getConsts()
{
trigger_error(__METHOD__ . '() is deprecated, use similar getConstants()', E_USER_DEPRECATED);
return array_map(function ($const) { return $const->getValue(); }, $this->consts);
}
/**
* @deprecated use addConstant()
* @param string
* @param mixed
* @return static
*/
public function addConst($name, $value)
{
trigger_error(__METHOD__ . '() is deprecated, use similar addConstant()', E_USER_DEPRECATED);
$this->addConstant($name, $value);
return $this;
}
/**
* @param Constant[]|mixed[]
* @return static
*/
public function setConstants(array $consts)
{
$this->consts = [];
foreach ($consts as $k => $v) {
$const = $v instanceof Constant ? $v : (new Constant($k))->setValue($v);
$this->consts[$const->getName()] = $const;
}
return $this;
}
/**
* @return Constant[]
*/
public function getConstants()
{
return $this->consts;
}
/**
* @param string
* @param mixed
* @return Constant
*/
public function addConstant($name, $value)
{
return $this->consts[$name] = (new Constant($name))->setValue($value);
}
/**
* @param Property[]
* @return static
*/
public function setProperties(array $props)
{
$this->properties = [];
foreach ($props as $v) {
if (!$v instanceof Property) {
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Property[].');
}
$this->properties[$v->getName()] = $v;
}
return $this;
}
/**
* @return Property[]
*/
public function getProperties()
{
return $this->properties;
}
/**
* @return Property
*/
public function getProperty($name)
{
if (!isset($this->properties[$name])) {
throw new Nette\InvalidArgumentException("Property '$name' not found.");
}
return $this->properties[$name];
}
/**
* @param string without $
* @param mixed
* @return Property
*/
public function addProperty($name, $value = NULL)
{
return $this->properties[$name] = (new Property($name))->setValue($value);
}
/**
* @param Method[]
* @return static
*/
public function setMethods(array $methods)
{
$this->methods = [];
foreach ($methods as $v) {
if (!$v instanceof Method) {
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Method[].');
}
$this->methods[$v->getName()] = $v->setNamespace($this->namespace);
}
return $this;
}
/**
* @return Method[]
*/
public function getMethods()
{
return $this->methods;
}
/**
* @return Method
*/
public function getMethod($name)
{
if (!isset($this->methods[$name])) {
throw new Nette\InvalidArgumentException("Method '$name' not found.");
}
return $this->methods[$name];
}
/**
* @param string
* @return Method
*/
public function addMethod($name)
{
$method = (new Method($name))->setNamespace($this->namespace);
if ($this->type === 'interface') {
$method->setBody(FALSE);
} else {
$method->setVisibility('public');
}
return $this->methods[$name] = $method;
}
}