Completed
Push — master ( a043a6...558d27 )
by Tony
01:30
created

ClassGenerator::__toString()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 0
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
        return '[]' === substr($type, -2) ? 'array' : $type;
43
    }
44
45
    /**
46
     * @param string $type
47
     * @return string
48
     */
49
    protected function getCommentTypeHint(string $type)
50
    {
51
        $brackets = '[]' === substr($type, -2) ? '[]' : '';
52
        $type = $brackets ? substr($type, 0, -2) : $type;
53
54
        if (!in_array($type, ['string', 'int', 'float', 'bool'], true)) {
55
            $this->class->getNamespace()->addUse($type);
56
            return $this->class->getNamespace()->unresolveName($type) . $brackets;
57
        }
58
59
        return $type . $brackets;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    protected function getCanonicalClassName(): string
66
    {
67
        return $this->class->getNamespace()->getName() . '\\' . $this->class->getName();
68
    }
69
70
    /**
71
     * ClassGenerator constructor.
72
     *
73
     * @param Config $annotation
74
     * @param string $className
75
     * @param string $classNamespace
76
     * @param string $ownerCanonicalClassName
77
     */
78
    public function __construct(
79
        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...
80
        string $className,
81
        string $classNamespace,
82
        string $ownerCanonicalClassName
83
    ) {
84
        $this->class = (new PhpNamespace($classNamespace))->addClass($className);
85
        $this->ownerCanonicalClassName = $ownerCanonicalClassName;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function __toString()
92
    {
93
        $this->class
94
            ->getNamespace()
95
            ->addUse(AbstractConfig::class);
96
97
        $this->class
98
            ->setFinal(true)
99
            ->addExtend(AbstractConfig::class)
100
            ->addComment(
101
                'THIS IS AN AUTOMATICALLY GENERATED FILE, PLEASE DO NOT MODIFY IT.' . PHP_EOL .
102
                'YOU MAY SAFELY DELETE THE FILE AS IT WILL BE REGENERATED ON-DEMAND.'
103
            );
104
105
        $this->class
106
            ->addProperty('___owner')
107
            ->addComment(
108
                '@var ' . $this->getCommentTypeHint($this->ownerCanonicalClassName)
109
            )->setVisibility('private');
110
111
        $this->class
112
            ->addMethod('end')
113
            ->addComment(
114
                '@return ' . $this->getCommentTypeHint($this->ownerCanonicalClassName)
115
            )->setReturnType($this->getTypeHint($this->ownerCanonicalClassName))
116
            ->setBody(
117
                'return $this->___owner;'
118
            );
119
120
        return '<?php' . PHP_EOL . PHP_EOL . (string) $this->class->getNamespace();
121
    }
122
123
    /**
124
     * @param string $name
125
     * @param string $type
126
     * @param null $default
127
     * @return ClassGenerator
128
     */
129
    public function generateProperty(string $name, string $type, $default = null): ClassGenerator
130
    {
131
        $this->class
132
            ->addProperty('__' . $name . '__', [null, $default])
133
            ->addComment(
134
                '@var ' . $this->getCommentTypeHint($type) . '[]'
135
            )->setVisibility('private');
136
        return $this;
137
    }
138
139
    /**
140
     * @param string $name
141
     * @param string $type
142
     * @return ClassGenerator
143
     */
144 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...
145
    {
146
        $this->class
147
            ->addMethod(static::camelCase('get_' . $name))
148
            ->addComment(
149
                '@return null|' . $this->getCommentTypeHint($type)
150
            )->setReturnType($this->getTypeHint($type))
151
            ->setReturnNullable(true)
152
            ->setBody(
153
                'return isset($this->__' . $name . '__[0]) ? $this->__' . $name . '__[0] : $this->__' . $name . '__[1];'
154
            );
155
        return $this;
156
    }
157
158
    /**
159
     * @param string $name
160
     * @param string $type
161
     * @return ClassGenerator
162
     */
163
    public function generateSet(string $name, string $type): ClassGenerator
164
    {
165
        $this->class
166
            ->addMethod(static::camelCase('set_' . $name))
167
            ->addComment(
168
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
169
                '@return ' . $this->class->getName()
170
            )->setReturnType($this->getCanonicalClassName())
171
            ->setBody(
172
                '$this->__' . $name . '__[0] = $value;' . PHP_EOL .
173
                'return $this;'
174
            )->addParameter('value')
175
            ->setTypeHint($this->getTypeHint($type));
176
        return $this;
177
    }
178
179
    /**
180
     * @param string $name
181
     * @return ClassGenerator
182
     */
183 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...
184
    {
185
        $this->class
186
            ->addMethod(static::camelCase('isset_' . $name))
187
            ->addComment(
188
                '@return bool'
189
            )->setReturnType('bool')
190
            ->setBody(
191
                'return isset($this->__' . $name . '__[0]);'
192
            );
193
        return $this;
194
    }
195
196
    /**
197
     * @param string $name
198
     * @return ClassGenerator
199
     */
200 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...
201
    {
202
        $this->class
203
            ->addMethod(static::camelCase('unset_' . $name))
204
            ->addComment(
205
                '@return ' . $this->class->getName()
206
            )->setReturnType($this->getCanonicalClassName())
207
            ->setBody(
208
                'unset($this->__' . $name . '__[0]);' . PHP_EOL .
209
                'return $this;'
210
            );
211
        return $this;
212
    }
213
214
    /**
215
     * @param string $name
216
     * @param string $type
217
     * @return ClassGenerator
218
     */
219 View Code Duplication
    public function generateArrayGet(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...
220
    {
221
        $this->class
222
            ->addMethod(static::camelCase('get_' . $name))
223
            ->addComment(
224
                '@return ' . $this->getCommentTypeHint($type)
225
            )->setReturnType($this->getTypeHint($type))
226
            ->setReturnNullable(true)
227
            ->setBody(
228
                'return isset($this->__' . $name . '__[0]) ? $this->__' . $name . '__[0] : null;'
229
            );
230
        return $this;
231
    }
232
233
    /**
234
     * @param string $name
235
     * @param string $type
236
     * @return ClassGenerator
237
     */
238
    public function generateArraySet(string $name, string $type): ClassGenerator
239
    {
240
        $this->class
241
            ->addMethod(static::camelCase('set_' . $name))
242
            ->addComment(
243
                '@param ' . $this->getCommentTypeHint($type) . ' $values' . PHP_EOL .
244
                '@return ' . $this->class->getName()
245
            )->setReturnType($this->getCanonicalClassName())
246
            ->setBody(
247
                '$this->' . static::camelCase('clear_' . $name) . '();' . PHP_EOL .
248
                'foreach ($values as $value) {' . PHP_EOL .
249
                '    $this->' . static::camelCase('push_' . $name) . '($value);' . PHP_EOL .
250
                '}' . PHP_EOL .
251
                'return $this;'
252
            )->addParameter('values')
253
            ->setTypeHint($this->getTypeHint($type));
254
        return $this;
255
    }
256
257
    /**
258
     * @param string $name
259
     * @param string $type
260
     * @return ClassGenerator
261
     */
262
    public function generateArrayGetAt(string $name, string $type): ClassGenerator
263
    {
264
        $this->class
265
            ->addMethod(static::camelCase('get_' . $name . '_at'))
266
            ->addComment(
267
                '@param int $index' . PHP_EOL .
268
                '@return ' . $this->getCommentTypeHint($type)
269
            )->setReturnType($this->getTypeHint($type))
270
            ->setReturnNullable(true)
271
            ->setBody(
272
                'if (isset($this->__' . $name . '__[0]) && array_key_exists($index, $this->__' . $name . '__[0])) {' .
273
                PHP_EOL . '    return $this->__' . $name . '__[0][$index];' . PHP_EOL .
274
                '}' . PHP_EOL .
275
                'return null;'
276
            )->addParameter('index')
277
            ->setTypeHint('int');
278
        return $this;
279
    }
280
281
    /**
282
     * @param string $name
283
     * @param string $type
284
     * @return ClassGenerator
285
     */
286
    public function generateArraySetAt(string $name, string $type): ClassGenerator
287
    {
288
        $method = $this->class
289
            ->addMethod(static::camelCase('set_' . $name . '_at'))
290
            ->addComment(
291
                '@param int $index' . PHP_EOL .
292
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
293
                '@return ' . $this->class->getName()
294
            )->setReturnType($this->getCanonicalClassName())
295
            ->setBody(
296
                'if (' . PHP_EOL .
297
                '    0 > $index ||' . PHP_EOL .
298
                '    (0 < $index && (!isset($this->__' . $name . '__[0]) || empty($this->__' . $name .
299
                '__[0])) || $index > count($this->__' . $name . '__[0]))' . PHP_EOL .
300
                ') {' . PHP_EOL .
301
                '    return $this;' . PHP_EOL .
302
                '}' . PHP_EOL . PHP_EOL .
303
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
304
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
305
                '}' . PHP_EOL . PHP_EOL .
306
                '$this->__' . $name . '__[0][$index] = $value;' . PHP_EOL .
307
                'return $this;'
308
            );
309
310
        $method->addParameter('index')->setTypeHint('int');
311
        $method->addParameter('value')->setTypeHint($this->getTypeHint($type));
312
313
        return $this;
314
    }
315
316
    /**
317
     * @param string $name
318
     * @return ClassGenerator
319
     */
320 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...
321
    {
322
        $this->class
323
            ->addMethod(static::camelCase('clear_' . $name))
324
            ->addComment(
325
                '@return ' . $this->class->getName()
326
            )->setReturnType($this->getCanonicalClassName())
327
            ->setBody(
328
                'unset($this->__' . $name . '__[0]);' . PHP_EOL .
329
                'return $this;'
330
            );
331
        return $this;
332
    }
333
334
    /**
335
     * @param string $name
336
     * @param string $type
337
     * @return ClassGenerator
338
     */
339 View Code Duplication
    public function generateArrayPush(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...
340
    {
341
        $this->class
342
            ->addMethod(static::camelCase('push_' . $name))
343
            ->addComment(
344
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
345
                '@return ' . $this->class->getName()
346
            )->setReturnType($this->getCanonicalClassName())
347
            ->setBody(
348
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
349
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
350
                '}' . PHP_EOL .
351
                'array_push($this->__' . $name . '__[0], $value);' . PHP_EOL .
352
                'return $this;'
353
            )->addParameter('value')
354
            ->setTypeHint($this->getTypeHint($type));
355
        return $this;
356
    }
357
358
    /**
359
     * @param string $name
360
     * @param string $type
361
     * @return ClassGenerator
362
     */
363 View Code Duplication
    public function generateArrayUnshift(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...
364
    {
365
        $this->class
366
            ->addMethod(static::camelCase('unshift_' . $name))
367
            ->addComment(
368
                '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL .
369
                '@return ' . $this->class->getName()
370
            )->setReturnType($this->getCanonicalClassName())
371
            ->setBody(
372
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
373
                '    $this->__' . $name . '__[0] = [];' . PHP_EOL .
374
                '}' . PHP_EOL .
375
                'array_unshift($this->__' . $name . '__[0], $value);' . PHP_EOL .
376
                'return $this;'
377
            )->addParameter('value')
378
            ->setTypeHint($this->getTypeHint($type));
379
        return $this;
380
    }
381
382
    /**
383
     * @param string $name
384
     * @param string $type
385
     * @return ClassGenerator
386
     */
387 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...
388
    {
389
        $this->class
390
            ->addMethod(static::camelCase('pop_' . $name))
391
            ->addComment(
392
                '@return null|' . $this->getCommentTypeHint($type)
393
            )->setReturnType($this->getTypeHint($type))
394
            ->setReturnNullable(true)
395
            ->setBody(
396
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
397
                '    return null;' . PHP_EOL .
398
                '}' . PHP_EOL .
399
                'return array_pop($this->__' . $name . '__[0]);'
400
            );
401
        return $this;
402
    }
403
404
    /**
405
     * @param string $name
406
     * @param string $type
407
     * @return ClassGenerator
408
     */
409 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...
410
    {
411
        $this->class
412
            ->addMethod(static::camelCase('shift_' . $name))
413
            ->addComment(
414
                '@return null|' . $this->getCommentTypeHint($type)
415
            )->setReturnType($this->getTypeHint($type))
416
            ->setReturnNullable(true)
417
            ->setBody(
418
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
419
                '    return null;' . PHP_EOL .
420
                '}' . PHP_EOL .
421
                'return array_shift($this->__' . $name . '__[0]);'
422
            );
423
        return $this;
424
    }
425
426
    /**
427
     * @param string $name
428
     * @param string $type
429
     * @return ClassGenerator
430
     */
431
    public function generateConfigGet(string $name, string $type): ClassGenerator
432
    {
433
        $this->class
434
            ->addMethod(static::camelCase('get_' . $name))
435
            ->addComment(
436
                '@return null|' . $this->getCommentTypeHint($type)
437
            )->setReturnType($this->getTypeHint($type))
438
            ->setBody(
439
                'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL .
440
                '    $this->__' . $name . '__[0] = new ' . $this->getCommentTypeHint($type) .
441
                '($this->___owner, $this, \'' . $name . '\');' . PHP_EOL .
442
                '}' . PHP_EOL .
443
                'return $this->__' . $name . '__[0];'
444
            );
445
        return $this;
446
    }
447
448
    /**
449
     * @param string $name
450
     * @return ClassGenerator
451
     */
452
    public function generateConfigSet(string $name): ClassGenerator
453
    {
454
        $this->class
455
            ->addMethod(static::camelCase('set_' . $name))
456
            ->addComment(
457
                '@return ' . $this->class->getName()
458
            )->setReturnType($this->getCanonicalClassName())
459
            ->setBody(
460
                '// config is immutable' . PHP_EOL .
461
                'return $this;'
462
            );
463
        return $this;
464
    }
465
466
    /**
467
     * @param string $name
468
     * @return ClassGenerator
469
     */
470 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...
471
    {
472
        $this->class
473
            ->addMethod(static::camelCase('isset_' . $name))
474
            ->addComment(
475
                '@return bool'
476
            )->setReturnType('bool')
477
            ->setBody(
478
                '// config is immutable' . PHP_EOL .
479
                'return true;'
480
            );
481
        return $this;
482
    }
483
484
    /**
485
     * @param string $name
486
     * @return ClassGenerator
487
     */
488
    public function generateConfigUnset(string $name): ClassGenerator
489
    {
490
        $this->class
491
            ->addMethod(static::camelCase('unset_' . $name))
492
            ->addComment(
493
                '@return ' . $this->class->getName()
494
            )->setReturnType($this->getCanonicalClassName())
495
            ->setBody(
496
                '// config is immutable' . PHP_EOL .
497
                'return $this;'
498
            );
499
        return $this;
500
    }
501
502
    /**
503
     * @return ClassGenerator
504
     */
505 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...
506
    {
507
        $cases = '';
508
509
        /** @var Property $property */
510
        foreach ($this->class->getProperties() as $property) {
511
            $getter = static::camelCase('get_' . $property->getName());
512
513
            try {
514
                $this->class->getMethod($getter);
515
            } catch (InvalidArgumentException $e) {
516
                continue;
517
            }
518
519
            $cases .=
520
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
521
                '        return $this->' . $getter . '();' . PHP_EOL . PHP_EOL;
522
        }
523
524
        $this->class
525
            ->addMethod('__get')
526
            ->addComment(
527
                '@inheritDoc'
528
            )->setBody(
529
                'switch ($name) {' . PHP_EOL .
530
                $cases .
531
                '    default:' . PHP_EOL .
532
                '        return null;' . PHP_EOL .
533
                '}'
534
            )->addParameter('name');
535
        return $this;
536
    }
537
538
    /**
539
     * @return ClassGenerator
540
     */
541
    public function generateMagicSet(): ClassGenerator
542
    {
543
        $cases = '';
544
545
        /** @var Property $property */
546
        foreach ($this->class->getProperties() as $property) {
547
            $setter = static::camelCase('set_' . $property->getName());
548
549
            try {
550
                $setterMethod = $this->class->getMethod($setter);
551
            } catch (InvalidArgumentException $e) {
552
                continue;
553
            }
554
555
            $cases .=
556
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
557
                '        return $this->' . $setter . '(' . (0 < count($setterMethod->getParameters()) ? '$value' : '') .
558
                ');' . PHP_EOL . PHP_EOL;
559
        }
560
561
        $method = $this->class
562
            ->addMethod('__set')
563
            ->addComment(
564
                '@inheritDoc'
565
            )->setBody(
566
                'switch ($name) {' . PHP_EOL .
567
                $cases .
568
                '    default:' . PHP_EOL .
569
                '        return null;' . PHP_EOL .
570
                '}'
571
            );
572
573
        $method->addParameter('name');
574
        $method->addParameter('value');
575
576
        return $this;
577
    }
578
579
    /**
580
     * @return ClassGenerator
581
     */
582 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...
583
    {
584
        $cases = '';
585
586
        /** @var Property $property */
587
        foreach ($this->class->getProperties() as $property) {
588
            $isset = static::camelCase('isset_' . $property->getName());
589
590
            try {
591
                $this->class->getMethod($isset);
592
            } catch (InvalidArgumentException $e) {
593
                continue;
594
            }
595
596
            $cases .=
597
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
598
                '        return $this->' . $isset . '();' . PHP_EOL . PHP_EOL;
599
        }
600
601
        $this->class
602
            ->addMethod('__isset')
603
            ->addComment(
604
                '@inheritDoc'
605
            )->setBody(
606
                'switch ($name) {' . PHP_EOL .
607
                $cases .
608
                '    default:' . PHP_EOL .
609
                '        return false;' . PHP_EOL .
610
                '}'
611
            )->addParameter('name');
612
        return $this;
613
    }
614
615
    /**
616
     * @return ClassGenerator
617
     */
618 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...
619
    {
620
        $cases = '';
621
622
        /** @var Property $property */
623
        foreach ($this->class->getProperties() as $property) {
624
            $unset = static::camelCase('unset_' . $property->getName());
625
626
            try {
627
                $this->class->getMethod($unset);
628
            } catch (InvalidArgumentException $e) {
629
                continue;
630
            }
631
632
            $cases .=
633
                '    case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL .
634
                '        return $this->' . $unset . '();' . PHP_EOL . PHP_EOL;
635
        }
636
637
        $this->class
638
            ->addMethod('__unset')
639
            ->addComment(
640
                '@inheritDoc'
641
            )->setBody(
642
                'switch ($name) {' . PHP_EOL .
643
                $cases .
644
                '    default:' . PHP_EOL .
645
                '        return $this;' . PHP_EOL .
646
                '}'
647
            )->addParameter('name');
648
        return $this;
649
    }
650
}