Completed
Push — master ( 1004b2...715e3e )
by Tony
01:56
created

ClassGenerator::generateMapSetAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.2
cc 1
eloc 17
nc 1
nop 2
1
<?php
2
3
namespace ClassConfig;
4
5
use ClassConfig\Annotation\Config;
6
use Nette\InvalidArgumentException;
7
use Nette\PhpGenerator\ClassType;
8
use Nette\PhpGenerator\PhpNamespace;
9
use Nette\PhpGenerator\Property;
10
11
/**
12
 * Class ClassGenerator
13
 * @package ClassConfig
14
 */
15
class ClassGenerator
16
{
17
    /**
18
     * @var ClassType
19
     */
20
    protected $class;
21
22
    /**
23
     * @var string
24
     */
25
    protected $ownerCanonicalClassName;
26
27
    /**
28
     * @param string $value
29
     * @return string
30
     */
31
    protected static function camelCase(string $value): string
32
    {
33
        return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $value))));
34
    }
35
36
    /**
37
     * @param string $type
38
     * @return string
39
     */
40
    protected function getTypeHint(string $type)
41
    {
42
        if ('[]' === substr($type, -2)) {
43
            return 'array';
44
        }
45
46
        if ('mixed' === $type) {
47
            return '';
48
        }
49
50
        return $type;
51
    }
52
53
    /**
54
     * @param string $type
55
     * @return string
56
     */
57
    protected function getCommentTypeHint(string $type)
58
    {
59
        if (preg_match('/^(.+?)((?:\[\])+)$/', $type, $match)) {
60
            $type = $match[1];
61
            $brackets = $match[2];
62
        } else {
63
            $brackets = '';
64
        }
65
66
        if (!in_array($type, ['string', 'int', 'float', 'bool', 'mixed'], true)) {
67
            $this->class->getNamespace()->addUse($type);
68
            return $this->class->getNamespace()->unresolveName($type) . $brackets;
69
        }
70
71
        return $type . $brackets;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function getCanonicalClassName(): string
78
    {
79
        return $this->class->getNamespace()->getName() . '\\' . $this->class->getName();
80
    }
81
82
    /**
83
     * ClassGenerator constructor.
84
     *
85
     * @param Config $annotation
86
     * @param string $className
87
     * @param string $classNamespace
88
     * @param string $ownerCanonicalClassName
89
     */
90
    public function __construct(
91
        Config $annotation,
0 ignored issues
show
Unused Code introduced by
The parameter $annotation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
        string $className,
93
        string $classNamespace,
94
        string $ownerCanonicalClassName
95
    ) {
96
        $this->class = (new PhpNamespace($classNamespace))->addClass($className);
97
        $this->ownerCanonicalClassName = $ownerCanonicalClassName;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function __toString()
104
    {
105
        $this->class
106
            ->getNamespace()
107
            ->addUse(AbstractConfig::class);
108
109
        $this->class
110
            ->setFinal(true)
111
            ->addExtend(AbstractConfig::class)
112
            ->addComment(
113
                'THIS IS AN AUTOMATICALLY GENERATED FILE, PLEASE DO NOT MODIFY IT.' . PHP_EOL .
114
                'YOU MAY SAFELY DELETE THE FILE AS IT WILL BE REGENERATED ON-DEMAND.'
115
            );
116
117
        $this->class
118
            ->addMethod('end')
119
            ->addComment(
120
                '@return ' . $this->getCommentTypeHint($this->ownerCanonicalClassName)
121
            )->setReturnType($this->getTypeHint($this->ownerCanonicalClassName))
122
            ->setBody(
123
                '/** @var ' . $this->getCommentTypeHint($this->ownerCanonicalClassName) . ' $owner */' . PHP_EOL .
124
                '$owner = $this->___owner;' . PHP_EOL .
125
                'return $owner;'
126
            );
127
128
        return '<?php' . PHP_EOL . PHP_EOL . (string) $this->class->getNamespace();
129
    }
130
131
    /**
132
     * @param string $name
133
     * @param string $type
134
     * @param null $default
135
     * @return ClassGenerator
136
     */
137
    public function generateProperty(string $name, string $type, $default = null): ClassGenerator
138
    {
139
        $this->class
140
            ->addProperty('__' . $name . '__', [null, $default])
141
            ->addComment(
142
                '@var ' . $this->getCommentTypeHint($type . '[]')
143
            )->setVisibility('private');
144
        return $this;
145
    }
146
147
    /**
148
     * @param string $name
149
     * @param string $type
150
     * @return ClassGenerator
151
     */
152 View Code Duplication
    public function generateGet(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $this->class
155
            ->addMethod(static::camelCase('get_' . $name))
156
            ->addComment(
157
                '@return null|' . $this->getCommentTypeHint($type)
158
            )->setReturnType($this->getTypeHint($type))
159
            ->setReturnNullable(true)
160
            ->setBody(
161
                'if (isset($this->__' . $name . '__[0])) {' . PHP_EOL .
162
                '    return $this->__' . $name . '__[0];' . PHP_EOL .
163
                '}' . PHP_EOL .
164
                'return $this->__' . $name . '__[1];'
165
            );
166
        return $this;
167
    }
168
169
    /**
170
     * @param string $name
171
     * @param string $type
172
     * @return ClassGenerator
173
     */
174
    public function generateSet(string $name, string $type): ClassGenerator
175
    {
176
        $this->class
177
            ->addMethod(static::camelCase('set_' . $name))
178
            ->addComment(
179
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
180
                '@return ' . $this->class->getName()
181
            )->setReturnType($this->getCanonicalClassName())
182
            ->setBody(
183
                '$this->__' . $name . '__[0] = $value;' . PHP_EOL .
184
                'return $this;'
185
            )->addParameter('value')
186
            ->setTypeHint($this->getTypeHint($type));
187
        return $this;
188
    }
189
190
    /**
191
     * @param string $name
192
     * @return ClassGenerator
193
     */
194 View Code Duplication
    public function generateIsset(string $name): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196
        $this->class
197
            ->addMethod(static::camelCase('isset_' . $name))
198
            ->addComment(
199
                '@return bool'
200
            )->setReturnType('bool')
201
            ->setBody(
202
                'return isset($this->__' . $name . '__[0]);'
203
            );
204
        return $this;
205
    }
206
207
    /**
208
     * @param string $name
209
     * @return ClassGenerator
210
     */
211 View Code Duplication
    public function generateUnset(string $name): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213
        $this->class
214
            ->addMethod(static::camelCase('unset_' . $name))
215
            ->addComment(
216
                '@return ' . $this->class->getName()
217
            )->setReturnType($this->getCanonicalClassName())
218
            ->setBody(
219
                'unset($this->__' . $name . '__[0]);' . PHP_EOL .
220
                'return $this;'
221
            );
222
        return $this;
223
    }
224
225
    /**
226
     * @param string $name
227
     * @param string $type
228
     * @return ClassGenerator
229
     */
230 View Code Duplication
    public function generateListSet(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232
        $this->class
233
            ->addMethod(static::camelCase('set_' . $name))
234
            ->addComment(
235
                '@param ' . $this->getCommentTypeHint($type) . ' $values' . PHP_EOL .
236
                '@return ' . $this->class->getName()
237
            )->setReturnType($this->getCanonicalClassName())
238
            ->setBody(
239
                '$this->' . static::camelCase('clear_' . $name) . '();' . PHP_EOL .
240
                'foreach ($values as $value) {' . PHP_EOL .
241
                '    $this->' . static::camelCase('push_' . $name) . '($value);' . PHP_EOL .
242
                '}' . PHP_EOL .
243
                'return $this;'
244
            )->addParameter('values')
245
            ->setTypeHint($this->getTypeHint($type));
246
        return $this;
247
    }
248
249
    /**
250
     * @param string $name
251
     * @param string $type
252
     * @return ClassGenerator
253
     */
254 View Code Duplication
    public function generateListGetAt(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
    {
256
        $this->class
257
            ->addMethod(static::camelCase('get_' . $name . '_at'))
258
            ->addComment(
259
                '@param int $index' . PHP_EOL .
260
                '@return ' . $this->getCommentTypeHint($type)
261
            )->setReturnType($this->getTypeHint($type))
262
            ->setReturnNullable(true)
263
            ->setBody(
264
                'if (isset($this->__' . $name . '__[0]) && array_key_exists($index, $this->__' . $name . '__[0])) {' .
265
                PHP_EOL . '    return $this->__' . $name . '__[0][$index];' . PHP_EOL .
266
                '}' . PHP_EOL .
267
                'return null;'
268
            )->addParameter('index')
269
            ->setTypeHint('int');
270
        return $this;
271
    }
272
273
    /**
274
     * @param string $name
275
     * @param string $type
276
     * @return ClassGenerator
277
     */
278
    public function generateListSetAt(string $name, string $type): ClassGenerator
279
    {
280
        $method = $this->class
281
            ->addMethod(static::camelCase('set_' . $name . '_at'))
282
            ->addComment(
283
                '@param int $index' . PHP_EOL .
284
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
285
                '@return ' . $this->class->getName()
286
            )->setReturnType($this->getCanonicalClassName())
287
            ->setBody(
288
                'if (0 > $index || (0 < $index && (!isset($this->__' . $name . '__[0]) ||' . PHP_EOL .
289
                '    empty($this->__' . $name . '__[0])) || $index > count($this->__' . $name . '__[0]))) {' . PHP_EOL .
290
                '    return $this;' . PHP_EOL .
291
                '}' . PHP_EOL . PHP_EOL .
292
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
293
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
294
                '}' . PHP_EOL . PHP_EOL .
295
                '$this->__' . $name . '__[0][$index] = $value;' . PHP_EOL .
296
                'return $this;'
297
            );
298
299
        $method->addParameter('index')->setTypeHint('int');
300
        $method->addParameter('value')->setTypeHint($this->getTypeHint($type));
301
302
        return $this;
303
    }
304
305
    /**
306
     * @param string $name
307
     * @param string $type
308
     * @return ClassGenerator
309
     */
310 View Code Duplication
    public function generateListPush(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
311
    {
312
        $this->class
313
            ->addMethod(static::camelCase('push_' . $name))
314
            ->addComment(
315
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
316
                '@return ' . $this->class->getName()
317
            )->setReturnType($this->getCanonicalClassName())
318
            ->setBody(
319
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
320
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
321
                '}' . PHP_EOL .
322
                'array_push($this->__' . $name . '__[0], $value);' . PHP_EOL .
323
                'return $this;'
324
            )->addParameter('value')
325
            ->setTypeHint($this->getTypeHint($type));
326
        return $this;
327
    }
328
329
    /**
330
     * @param string $name
331
     * @param string $type
332
     * @return ClassGenerator
333
     */
334 View Code Duplication
    public function generateListUnshift(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
    {
336
        $this->class
337
            ->addMethod(static::camelCase('unshift_' . $name))
338
            ->addComment(
339
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
340
                '@return ' . $this->class->getName()
341
            )->setReturnType($this->getCanonicalClassName())
342
            ->setBody(
343
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
344
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
345
                '}' . PHP_EOL .
346
                'array_unshift($this->__' . $name . '__[0], $value);' . PHP_EOL .
347
                'return $this;'
348
            )->addParameter('value')
349
            ->setTypeHint($this->getTypeHint($type));
350
        return $this;
351
    }
352
353
    /**
354
     * @param string $name
355
     * @param string $type
356
     * @return ClassGenerator
357
     */
358 View Code Duplication
    public function generateMapSet(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
359
    {
360
        $this->class
361
            ->addMethod(static::camelCase('set_' . $name))
362
            ->addComment(
363
                '@param ' . $this->getCommentTypeHint($type) . ' $values' . PHP_EOL .
364
                '@return ' . $this->class->getName()
365
            )->setReturnType($this->getCanonicalClassName())
366
            ->setBody(
367
                '$this->' . static::camelCase('clear_' . $name) . '();' . PHP_EOL .
368
                'foreach ($values as $key => $value) {' . PHP_EOL .
369
                '    $this->' . static::camelCase('set_' . $name . '_at') . '($key, $value);' . PHP_EOL .
370
                '}' . PHP_EOL .
371
                'return $this;'
372
            )->addParameter('values')
373
            ->setTypeHint($this->getTypeHint($type));
374
        return $this;
375
    }
376
377
    /**
378
     * @param string $name
379
     * @param string $type
380
     * @return ClassGenerator
381
     */
382 View Code Duplication
    public function generateMapGetAt(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
    {
384
        $this->class
385
            ->addMethod(static::camelCase('get_' . $name . '_at'))
386
            ->addComment(
387
                '@param mixed $key' . PHP_EOL .
388
                '@return ' . $this->getCommentTypeHint($type)
389
            )->setReturnType($this->getTypeHint($type))
390
            ->setReturnNullable(true)
391
            ->setBody(
392
                'if (isset($this->__' . $name . '__[0]) && array_key_exists($key, $this->__' . $name . '__[0])) {' .
393
                PHP_EOL . '    return $this->__' . $name . '__[0][$key];' . PHP_EOL .
394
                '}' . PHP_EOL .
395
                'return null;'
396
            )->addParameter('key');
397
        return $this;
398
    }
399
400
    /**
401
     * @param string $name
402
     * @param string $type
403
     * @return ClassGenerator
404
     */
405
    public function generateMapSetAt(string $name, string $type): ClassGenerator
406
    {
407
        $method = $this->class
408
            ->addMethod(static::camelCase('set_' . $name . '_at'))
409
            ->addComment(
410
                '@param mixed $key' . PHP_EOL .
411
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
412
                '@return ' . $this->class->getName()
413
            )->setReturnType($this->getCanonicalClassName())
414
            ->setBody(
415
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
416
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
417
                '}' . PHP_EOL . PHP_EOL .
418
                '$this->__' . $name . '__[0][$key] = $value;' . PHP_EOL .
419
                'return $this;'
420
            );
421
422
        $method->addParameter('key');
423
        $method->addParameter('value')->setTypeHint($this->getTypeHint($type));
424
425
        return $this;
426
    }
427
428
    /**
429
     * @param string $name
430
     * @param string $type
431
     * @return ClassGenerator
432
     */
433 View Code Duplication
    public function generateArrayPop(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
434
    {
435
        $this->class
436
            ->addMethod(static::camelCase('pop_' . $name))
437
            ->addComment(
438
                '@return null|' . $this->getCommentTypeHint($type)
439
            )->setReturnType($this->getTypeHint($type))
440
            ->setReturnNullable(true)
441
            ->setBody(
442
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
443
                '    return null;' . PHP_EOL .
444
                '}' . PHP_EOL .
445
                'return array_pop($this->__' . $name . '__[0]);'
446
            );
447
        return $this;
448
    }
449
450
    /**
451
     * @param string $name
452
     * @param string $type
453
     * @return ClassGenerator
454
     */
455 View Code Duplication
    public function generateArrayShift(string $name, string $type): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
456
    {
457
        $this->class
458
            ->addMethod(static::camelCase('shift_' . $name))
459
            ->addComment(
460
                '@return null|' . $this->getCommentTypeHint($type)
461
            )->setReturnType($this->getTypeHint($type))
462
            ->setReturnNullable(true)
463
            ->setBody(
464
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
465
                '    return null;' . PHP_EOL .
466
                '}' . PHP_EOL .
467
                'return array_shift($this->__' . $name . '__[0]);'
468
            );
469
        return $this;
470
    }
471
472
    /**
473
     * @param string $name
474
     * @return ClassGenerator
475
     */
476 View Code Duplication
    public function generateArrayClear(string $name): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
477
    {
478
        $this->class
479
            ->addMethod(static::camelCase('clear_' . $name))
480
            ->addComment(
481
                '@return ' . $this->class->getName()
482
            )->setReturnType($this->getCanonicalClassName())
483
            ->setBody(
484
                'unset($this->__' . $name . '__[0]);' . PHP_EOL .
485
                'return $this;'
486
            );
487
        return $this;
488
    }
489
490
    /**
491
     * @param string $name
492
     * @param string $type
493
     * @return ClassGenerator
494
     */
495
    public function generateConfigGet(string $name, string $type): ClassGenerator
496
    {
497
        $this->class
498
            ->addMethod(static::camelCase('get_' . $name))
499
            ->addComment(
500
                '@return null|' . $this->getCommentTypeHint($type)
501
            )->setReturnType($this->getTypeHint($type))
502
            ->setBody(
503
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
504
                '    $this->__' . $name . '__[0] = new ' . $this->getCommentTypeHint($type) .
505
                '($this->___owner, $this, \'' . $name . '\');' . PHP_EOL .
506
                '}' . PHP_EOL .
507
                'return $this->__' . $name . '__[0];'
508
            );
509
        return $this;
510
    }
511
512
    /**
513
     * @param string $name
514
     * @return ClassGenerator
515
     */
516
    public function generateConfigSet(string $name): ClassGenerator
517
    {
518
        $this->class
519
            ->addMethod(static::camelCase('set_' . $name))
520
            ->addComment(
521
                '@return ' . $this->class->getName()
522
            )->setReturnType($this->getCanonicalClassName())
523
            ->setBody(
524
                '// config is immutable' . PHP_EOL .
525
                'return $this;'
526
            );
527
        return $this;
528
    }
529
530
    /**
531
     * @param string $name
532
     * @return ClassGenerator
533
     */
534 View Code Duplication
    public function generateConfigIsset(string $name): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
535
    {
536
        $this->class
537
            ->addMethod(static::camelCase('isset_' . $name))
538
            ->addComment(
539
                '@return bool'
540
            )->setReturnType('bool')
541
            ->setBody(
542
                '// config is immutable' . PHP_EOL .
543
                'return true;'
544
            );
545
        return $this;
546
    }
547
548
    /**
549
     * @param string $name
550
     * @return ClassGenerator
551
     */
552
    public function generateConfigUnset(string $name): ClassGenerator
553
    {
554
        $this->class
555
            ->addMethod(static::camelCase('unset_' . $name))
556
            ->addComment(
557
                '@return ' . $this->class->getName()
558
            )->setReturnType($this->getCanonicalClassName())
559
            ->setBody(
560
                '// config is immutable' . PHP_EOL .
561
                'return $this;'
562
            );
563
        return $this;
564
    }
565
566
    /**
567
     * @return ClassGenerator
568
     */
569 View Code Duplication
    public function generateMagicGet(): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
570
    {
571
        $cases = '';
572
573
        /** @var Property $property */
574
        foreach ($this->class->getProperties() as $property) {
575
            $getter = static::camelCase('get_' . $property->getName());
576
577
            try {
578
                $this->class->getMethod($getter);
579
            } catch (InvalidArgumentException $e) {
580
                continue;
581
            }
582
583
            $cases .=
584
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
585
                '        return $this->' . $getter . '();' . PHP_EOL . PHP_EOL;
586
        }
587
588
        $this->class
589
            ->addMethod('__get')
590
            ->addComment(
591
                '@inheritDoc'
592
            )->setBody(
593
                'switch ($name) {' . PHP_EOL .
594
                $cases .
595
                '    default:' . PHP_EOL .
596
                '        return null;' . PHP_EOL .
597
                '}'
598
            )->addParameter('name');
599
        return $this;
600
    }
601
602
    /**
603
     * @return ClassGenerator
604
     */
605
    public function generateMagicSet(): ClassGenerator
606
    {
607
        $cases = '';
608
609
        /** @var Property $property */
610
        foreach ($this->class->getProperties() as $property) {
611
            $setter = static::camelCase('set_' . $property->getName());
612
613
            try {
614
                $setterMethod = $this->class->getMethod($setter);
615
            } catch (InvalidArgumentException $e) {
616
                continue;
617
            }
618
619
            $cases .=
620
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
621
                '        return $this->' . $setter . '(' . (0 < count($setterMethod->getParameters()) ? '$value' : '') .
622
                ');' . PHP_EOL . PHP_EOL;
623
        }
624
625
        $method = $this->class
626
            ->addMethod('__set')
627
            ->addComment(
628
                '@inheritDoc'
629
            )->setBody(
630
                'switch ($name) {' . PHP_EOL .
631
                $cases .
632
                '    default:' . PHP_EOL .
633
                '        return null;' . PHP_EOL .
634
                '}'
635
            );
636
637
        $method->addParameter('name');
638
        $method->addParameter('value');
639
640
        return $this;
641
    }
642
643
    /**
644
     * @return ClassGenerator
645
     */
646 View Code Duplication
    public function generateMagicIsset(): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
647
    {
648
        $cases = '';
649
650
        /** @var Property $property */
651
        foreach ($this->class->getProperties() as $property) {
652
            $isset = static::camelCase('isset_' . $property->getName());
653
654
            try {
655
                $this->class->getMethod($isset);
656
            } catch (InvalidArgumentException $e) {
657
                continue;
658
            }
659
660
            $cases .=
661
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
662
                '        return $this->' . $isset . '();' . PHP_EOL . PHP_EOL;
663
        }
664
665
        $this->class
666
            ->addMethod('__isset')
667
            ->addComment(
668
                '@inheritDoc'
669
            )->setBody(
670
                'switch ($name) {' . PHP_EOL .
671
                $cases .
672
                '    default:' . PHP_EOL .
673
                '        return false;' . PHP_EOL .
674
                '}'
675
            )->addParameter('name');
676
        return $this;
677
    }
678
679
    /**
680
     * @return ClassGenerator
681
     */
682 View Code Duplication
    public function generateMagicUnset(): ClassGenerator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
683
    {
684
        $cases = '';
685
686
        /** @var Property $property */
687
        foreach ($this->class->getProperties() as $property) {
688
            $unset = static::camelCase('unset_' . $property->getName());
689
690
            try {
691
                $this->class->getMethod($unset);
692
            } catch (InvalidArgumentException $e) {
693
                continue;
694
            }
695
696
            $cases .=
697
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
698
                '        return $this->' . $unset . '();' . PHP_EOL . PHP_EOL;
699
        }
700
701
        $this->class
702
            ->addMethod('__unset')
703
            ->addComment(
704
                '@inheritDoc'
705
            )->setBody(
706
                'switch ($name) {' . PHP_EOL .
707
                $cases .
708
                '    default:' . PHP_EOL .
709
                '        return $this;' . PHP_EOL .
710
                '}'
711
            )->addParameter('name');
712
        return $this;
713
    }
714
}