Completed
Pull Request — master (#11)
by Eric
04:22
created

PropertyMetadata   C

Complexity

Total Complexity 72

Size/Duplication

Total Lines 660
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.46%

Importance

Changes 0
Metric Value
wmc 72
lcom 1
cbo 1
dl 0
loc 660
ccs 218
cts 226
cp 0.9646
rs 5.2432
c 0
b 0
f 0

58 Methods

Rating   Name   Duplication   Size   Complexity  
A getMaxDepth() 0 4 1
A hasGroups() 0 4 1
A addGroup() 0 6 2
A removeGroup() 0 4 1
A isXmlAttribute() 0 4 1
A __construct() 0 5 1
A getName() 0 4 1
A setName() 0 4 1
A getClass() 0 4 1
A setClass() 0 4 1
A hasAlias() 0 4 1
A getAlias() 0 4 1
A setAlias() 0 4 1
A hasType() 0 4 1
A getType() 0 4 1
A setType() 0 4 1
A isReadable() 0 4 1
A setReadable() 0 4 1
A isWritable() 0 4 1
A setWritable() 0 4 1
A hasAccessor() 0 4 1
A getAccessor() 0 4 1
A setAccessor() 0 4 1
A hasMutator() 0 4 1
A getMutator() 0 4 1
A setMutator() 0 4 1
A hasSinceVersion() 0 4 1
A getSinceVersion() 0 4 1
A setSinceVersion() 0 4 1
A hasUntilVersion() 0 4 1
A getUntilVersion() 0 4 1
A setUntilVersion() 0 4 1
A hasMaxDepth() 0 4 1
A setMaxDepth() 0 4 1
A getGroups() 0 4 1
A setGroups() 0 5 1
A addGroups() 0 6 2
A hasGroup() 0 4 1
A setXmlAttribute() 0 4 1
A isXmlValue() 0 4 1
A setXmlValue() 0 4 1
A isXmlInline() 0 4 1
A setXmlInline() 0 4 1
A hasXmlEntry() 0 4 1
A getXmlEntry() 0 4 1
A setXmlEntry() 0 4 1
A hasXmlEntryAttribute() 0 4 1
A getXmlEntryAttribute() 0 4 1
A setXmlEntryAttribute() 0 4 1
A hasXmlKeyAsAttribute() 0 4 1
A useXmlKeyAsAttribute() 0 4 1
A setXmlKeyAsAttribute() 0 4 1
A hasXmlKeyAsNode() 0 4 1
A useXmlKeyAsNode() 0 4 1
A setXmlKeyAsNode() 0 4 1
F merge() 0 56 13
B serialize() 0 24 1
B unserialize() 0 24 1

How to fix   Complexity   

Complex Class

Complex classes like PropertyMetadata often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PropertyMetadata, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
class PropertyMetadata implements PropertyMetadataInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var string
26
     */
27
    private $class;
28
29
    /**
30
     * @var string
31
     */
32
    private $alias;
33
34
    /**
35
     * @var TypeMetadataInterface|null
36
     */
37
    private $type;
38
39
    /**
40
     * @var bool
41
     */
42
    private $readable = true;
43
44
    /**
45
     * @var bool
46
     */
47
    private $writable = true;
48
49
    /**
50
     * @var string|null
51
     */
52
    private $accessor;
53
54
    /**
55
     * @var string|null
56
     */
57
    private $mutator;
58
59
    /**
60
     * @var string|null
61
     */
62
    private $since;
63
64
    /**
65
     * @var string|null
66
     */
67
    private $until;
68
69
    /**
70
     * @var int|null
71
     */
72
    private $maxDepth;
73
74
    /**
75
     * @var string[]
76
     */
77
    private $groups = [];
78
79
    /**
80
     * @var bool
81
     */
82
    private $xmlAttribute = false;
83
84
    /**
85
     * @var bool
86
     */
87
    private $xmlValue = false;
88
89
    /**
90
     * @var bool
91
     */
92
    private $xmlInline = false;
93
94
    /**
95
     * @var string|null
96
     */
97
    private $xmlEntry;
98
99
    /**
100
     * @var string|null
101
     */
102
    private $xmlEntryAttribute;
103
104
    /**
105
     * @var bool|null
106
     */
107
    private $xmlKeyAsAttribute;
108
109
    /**
110
     * @var bool|null
111
     */
112
    private $xmlKeyAsNode;
113
114
    /**
115
     * @param string $name
116
     * @param string $class
117
     */
118 1784
    public function __construct($name, $class)
119
    {
120 1784
        $this->setName($name);
121 1784
        $this->setClass($class);
122 1784
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 1524
    public function getName()
128
    {
129 1524
        return $this->name;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 1784
    public function setName($name)
136
    {
137 1784
        $this->name = $name;
138 1784
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 8
    public function getClass()
144
    {
145 8
        return $this->class;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 1784
    public function setClass($class)
152
    {
153 1784
        $this->class = $class;
154 1784
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 1468
    public function hasAlias()
160
    {
161 1468
        return $this->alias !== null;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 508
    public function getAlias()
168
    {
169 508
        return $this->alias;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 152
    public function setAlias($alias)
176
    {
177 152
        $this->alias = $alias;
178 152
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 380
    public function hasType()
184
    {
185 380
        return $this->type !== null;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1188
    public function getType()
192
    {
193 1188
        return $this->type;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1216
    public function setType(TypeMetadataInterface $type = null)
200
    {
201 1216
        $this->type = $type;
202 1216
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 1052
    public function isReadable()
208
    {
209 1052
        return $this->readable;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215 1708
    public function setReadable($readable)
216
    {
217 1708
        $this->readable = $readable;
218 1708
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 768
    public function isWritable()
224
    {
225 768
        return $this->writable;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 1704
    public function setWritable($writable)
232
    {
233 1704
        $this->writable = $writable;
234 1704
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239 1052
    public function hasAccessor()
240
    {
241 1052
        return $this->accessor !== null;
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247 396
    public function getAccessor()
248
    {
249 396
        return $this->accessor;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 56
    public function setAccessor($accessor)
256
    {
257 56
        $this->accessor = $accessor;
258 56
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263 708
    public function hasMutator()
264
    {
265 708
        return $this->mutator !== null;
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271 396
    public function getMutator()
272
    {
273 396
        return $this->mutator;
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279 56
    public function setMutator($mutator)
280
    {
281 56
        $this->mutator = $mutator;
282 56
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287 444
    public function hasSinceVersion()
288
    {
289 444
        return $this->since !== null;
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295 444
    public function getSinceVersion()
296
    {
297 444
        return $this->since;
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 120
    public function setSinceVersion($since)
304
    {
305 120
        $this->since = $since;
306 120
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311 444
    public function hasUntilVersion()
312
    {
313 444
        return $this->until !== null;
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319 444
    public function getUntilVersion()
320
    {
321 444
        return $this->until;
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327 120
    public function setUntilVersion($until)
328
    {
329 120
        $this->until = $until;
330 120
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335 412
    public function hasMaxDepth()
336
    {
337 412
        return $this->maxDepth !== null;
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343 412
    public function getMaxDepth()
344
    {
345 412
        return $this->maxDepth;
346
    }
347
348
    /**
349
     * {@inheritdoc}
350
     */
351 88
    public function setMaxDepth($maxDepth)
352
    {
353 88
        $this->maxDepth = $maxDepth;
354 88
    }
355
356
    /**
357
     * {@inheritdoc}
358
     */
359 388
    public function hasGroups()
360
    {
361 388
        return !empty($this->groups);
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367 392
    public function getGroups()
368
    {
369 392
        return array_keys($this->groups);
370
    }
371
372
    /**
373
     * {@inheritdoc}
374
     */
375 12
    public function setGroups(array $groups)
376
    {
377 12
        $this->groups = [];
378 12
        $this->addGroups($groups);
379 12
    }
380
381
    /**
382
     * {@inheritdoc}
383
     */
384 12
    public function addGroups(array $groups)
385
    {
386 12
        foreach ($groups as $group) {
387 12
            $this->addGroup($group);
388 6
        }
389 12
    }
390
391
    /**
392
     * {@inheritdoc}
393
     */
394 148
    public function hasGroup($group)
395
    {
396 148
        return isset($this->groups[$group]);
397
    }
398
399
    /**
400
     * {@inheritdoc}
401
     */
402 148
    public function addGroup($group)
403
    {
404 148
        if (!$this->hasGroup($group)) {
405 148
            $this->groups[$group] = true;
406 74
        }
407 148
    }
408
409
    /**
410
     * {@inheritdoc}
411
     */
412 4
    public function removeGroup($group)
413
    {
414 4
        unset($this->groups[$group]);
415 4
    }
416
417
    /**
418
     * {@inheritdoc}
419
     */
420 640
    public function isXmlAttribute()
421
    {
422 640
        return $this->xmlAttribute;
423
    }
424
425
    /**
426
     * {@inheritdoc}
427
     */
428 100
    public function setXmlAttribute($xmlAttribute)
429
    {
430 100
        $this->xmlAttribute = $xmlAttribute;
431 100
    }
432
433
    /**
434
     * {@inheritdoc}
435
     */
436 640
    public function isXmlValue()
437
    {
438 640
        return $this->xmlValue;
439
    }
440
441
    /**
442
     * {@inheritdoc}
443
     */
444 52
    public function setXmlValue($xmlValue)
445
    {
446 52
        $this->xmlValue = $xmlValue;
447 52
    }
448
449
    /**
450
     * {@inheritdoc}
451
     */
452 636
    public function isXmlInline()
453
    {
454 636
        return $this->xmlInline;
455
    }
456
457
    /**
458
     * {@inheritdoc}
459
     */
460 52
    public function setXmlInline($xmlInline)
461
    {
462 52
        $this->xmlInline = $xmlInline;
463 52
    }
464
465
    /**
466
     * {@inheritdoc}
467
     */
468 40
    public function hasXmlEntry()
469
    {
470 40
        return $this->xmlEntry !== null;
471
    }
472
473
    /**
474
     * {@inheritdoc}
475
     */
476 376
    public function getXmlEntry()
477
    {
478 376
        return $this->xmlEntry;
479
    }
480
481
    /**
482
     * {@inheritdoc}
483
     */
484 48
    public function setXmlEntry($xmlEntry)
485
    {
486 48
        $this->xmlEntry = $xmlEntry;
487 48
    }
488
489
    /**
490
     * {@inheritdoc}
491
     */
492 40
    public function hasXmlEntryAttribute()
493
    {
494 40
        return $this->xmlEntryAttribute !== null;
495
    }
496
497
    /**
498
     * {@inheritdoc}
499
     */
500 376
    public function getXmlEntryAttribute()
501
    {
502 376
        return $this->xmlEntryAttribute;
503
    }
504
505
    /**
506
     * {@inheritdoc}
507
     */
508 48
    public function setXmlEntryAttribute($xmlEntryAttribute)
509
    {
510 48
        $this->xmlEntryAttribute = $xmlEntryAttribute;
511 48
    }
512
513
    /**
514
     * {@inheritdoc}
515
     */
516 40
    public function hasXmlKeyAsAttribute()
517
    {
518 40
        return $this->xmlKeyAsAttribute !== null;
519
    }
520
521
    /**
522
     * {@inheritdoc}
523
     */
524 376
    public function useXmlKeyAsAttribute()
525
    {
526 376
        return $this->xmlKeyAsAttribute;
527
    }
528
529
    /**
530
     * {@inheritdoc}
531
     */
532 48
    public function setXmlKeyAsAttribute($xmlKeyAsAttribute)
533
    {
534 48
        $this->xmlKeyAsAttribute = $xmlKeyAsAttribute;
535 48
    }
536
537
    /**
538
     * {@inheritdoc}
539
     */
540 32
    public function hasXmlKeyAsNode()
541
    {
542 32
        return $this->xmlKeyAsNode !== null;
543
    }
544
545
    /**
546
     * {@inheritdoc}
547
     */
548 376
    public function useXmlKeyAsNode()
549
    {
550 376
        return $this->xmlKeyAsNode;
551
    }
552
553
    /**
554
     * {@inheritdoc}
555
     */
556 48
    public function setXmlKeyAsNode($xmlKeyAsNode)
557
    {
558 48
        $this->xmlKeyAsNode = $xmlKeyAsNode;
559 48
    }
560
561
    /**
562
     * {@inheritdoc}
563
     */
564 4
    public function merge(PropertyMetadataInterface $propertyMetadata)
565
    {
566 4
        $this->setReadable($propertyMetadata->isReadable());
567 4
        $this->setWritable($propertyMetadata->isWritable());
568 4
        $this->setXmlAttribute($propertyMetadata->isXmlAttribute());
569 4
        $this->setXmlValue($propertyMetadata->isXmlValue());
570 4
        $this->setXmlInline($propertyMetadata->isXmlInline());
571
572 4
        if ($propertyMetadata->hasAlias()) {
573 4
            $this->setAlias($propertyMetadata->getAlias());
574 2
        }
575
576 4
        if ($propertyMetadata->hasType()) {
577 4
            $this->setType($propertyMetadata->getType());
578 2
        }
579
580 4
        if ($propertyMetadata->hasAccessor()) {
581 4
            $this->setAccessor($propertyMetadata->getAccessor());
582 2
        }
583
584 4
        if ($propertyMetadata->hasMutator()) {
585 4
            $this->setMutator($propertyMetadata->getMutator());
586 2
        }
587
588 4
        if ($propertyMetadata->hasSinceVersion()) {
589 4
            $this->setSinceVersion($propertyMetadata->getSinceVersion());
590 2
        }
591
592 4
        if ($propertyMetadata->hasUntilVersion()) {
593 4
            $this->setUntilVersion($propertyMetadata->getUntilVersion());
594 2
        }
595
596 4
        if ($propertyMetadata->hasMaxDepth()) {
597 4
            $this->setMaxDepth($propertyMetadata->getMaxDepth());
598 2
        }
599
600 4
        foreach ($propertyMetadata->getGroups() as $group) {
601 4
            $this->addGroup($group);
602 2
        }
603
604 4
        if ($propertyMetadata->hasXmlEntry()) {
605
            $this->setXmlEntry($propertyMetadata->getXmlEntry());
606
        }
607
608 4
        if ($propertyMetadata->hasXmlEntryAttribute()) {
609
            $this->setXmlEntryAttribute($propertyMetadata->getXmlEntryAttribute());
610
        }
611
612 4
        if ($propertyMetadata->hasXmlKeyAsAttribute()) {
613
            $this->setXmlKeyAsAttribute($propertyMetadata->useXmlKeyAsAttribute());
614
        }
615
616 4
        if ($propertyMetadata->hasXmlKeyAsNode()) {
617
            $this->setXmlKeyAsNode($propertyMetadata->useXmlKeyAsNode());
618
        }
619 4
    }
620
621
    /**
622
     * {@inheritdoc}
623
     */
624 4
    public function serialize()
625
    {
626 4
        return serialize([
627 4
            $this->name,
628 4
            $this->class,
629 4
            $this->alias,
630 4
            $this->type,
631 4
            $this->readable,
632 4
            $this->writable,
633 4
            $this->accessor,
634 4
            $this->mutator,
635 4
            $this->since,
636 4
            $this->until,
637 4
            $this->maxDepth,
638 4
            $this->groups,
639 4
            $this->xmlAttribute,
640 4
            $this->xmlValue,
641 4
            $this->xmlInline,
642 4
            $this->xmlEntry,
643 4
            $this->xmlEntryAttribute,
644 4
            $this->xmlKeyAsAttribute,
645 4
            $this->xmlKeyAsNode,
646 2
        ]);
647
    }
648
649
    /**
650
     * {@inheritdoc}
651
     */
652 4
    public function unserialize($serialized)
653
    {
654
        list(
655 4
            $this->name,
656 4
            $this->class,
657 4
            $this->alias,
658 4
            $this->type,
659 4
            $this->readable,
660 4
            $this->writable,
661 4
            $this->accessor,
662 4
            $this->mutator,
663 4
            $this->since,
664 4
            $this->until,
665 4
            $this->maxDepth,
666 4
            $this->groups,
667 4
            $this->xmlAttribute,
668 4
            $this->xmlValue,
669 4
            $this->xmlInline,
670 4
            $this->xmlEntry,
671 4
            $this->xmlEntryAttribute,
672 4
            $this->xmlKeyAsAttribute,
673 4
            $this->xmlKeyAsNode
674 4
        ) = unserialize($serialized);
675 4
    }
676
}
677