Passed
Push — master ( 9db3d0...354d5b )
by Reyo
02:53
created

ProjectTaskNormalizer   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 100
ccs 0
cts 90
cp 0
rs 9.1199
wmc 41

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 2
F denormalize() 0 43 30
A supportsDenormalization() 0 3 1
B normalize() 0 40 8

How to fix   Complexity   

Complex Class

Complex classes like ProjectTaskNormalizer 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.

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 ProjectTaskNormalizer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the timechimp bundle package.
7
 * (c) Connect Holland.
8
 */
9
10
namespace ConnectHolland\TimechimpBundle\Api\Normalizer;
11
12
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
19
class ProjectTaskNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
20
{
21
    use DenormalizerAwareTrait;
22
    use NormalizerAwareTrait;
23
24
    public function supportsDenormalization($data, $type, $format = null)
25
    {
26
        return $type === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectTask';
27
    }
28
29
    public function supportsNormalization($data, $format = null)
30
    {
31
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectTask';
32
    }
33
34
    public function denormalize($data, $class, $format = null, array $context = [])
35
    {
36
        if (!is_object($data)) {
37
            return null;
38
        }
39
        $object = new \ConnectHolland\TimechimpBundle\Api\Model\ProjectTask();
40
        if (property_exists($data, 'id') && $data->{'id'} !== null) {
41
            $object->setId($data->{'id'});
42
        } elseif (property_exists($data, 'id') && $data->{'id'} === null) {
43
            $object->setId(null);
44
        }
45
        if (property_exists($data, 'taskId') && $data->{'taskId'} !== null) {
46
            $object->setTaskId($data->{'taskId'});
47
        } elseif (property_exists($data, 'taskId') && $data->{'taskId'} === null) {
48
            $object->setTaskId(null);
49
        }
50
        if (property_exists($data, 'taskName') && $data->{'taskName'} !== null) {
51
            $object->setTaskName($data->{'taskName'});
52
        } elseif (property_exists($data, 'taskName') && $data->{'taskName'} === null) {
53
            $object->setTaskName(null);
54
        }
55
        if (property_exists($data, 'hourlyRate') && $data->{'hourlyRate'} !== null) {
56
            $object->setHourlyRate($data->{'hourlyRate'});
57
        } elseif (property_exists($data, 'hourlyRate') && $data->{'hourlyRate'} === null) {
58
            $object->setHourlyRate(null);
59
        }
60
        if (property_exists($data, 'billable') && $data->{'billable'} !== null) {
61
            $object->setBillable($data->{'billable'});
62
        } elseif (property_exists($data, 'billable') && $data->{'billable'} === null) {
63
            $object->setBillable(null);
64
        }
65
        if (property_exists($data, 'unspecified') && $data->{'unspecified'} !== null) {
66
            $object->setUnspecified($data->{'unspecified'});
67
        } elseif (property_exists($data, 'unspecified') && $data->{'unspecified'} === null) {
68
            $object->setUnspecified(null);
69
        }
70
        if (property_exists($data, 'budgetHours') && $data->{'budgetHours'} !== null) {
71
            $object->setBudgetHours($data->{'budgetHours'});
72
        } elseif (property_exists($data, 'budgetHours') && $data->{'budgetHours'} === null) {
73
            $object->setBudgetHours(null);
74
        }
75
76
        return $object;
77
    }
78
79
    public function normalize($object, $format = null, array $context = [])
80
    {
81
        $data = new \stdClass();
82
        if (null !== $object->getId()) {
83
            $data->{'id'} = $object->getId();
84
        } else {
85
            $data->{'id'} = null;
86
        }
87
        if (null !== $object->getTaskId()) {
88
            $data->{'taskId'} = $object->getTaskId();
89
        } else {
90
            $data->{'taskId'} = null;
91
        }
92
        if (null !== $object->getTaskName()) {
93
            $data->{'taskName'} = $object->getTaskName();
94
        } else {
95
            $data->{'taskName'} = null;
96
        }
97
        if (null !== $object->getHourlyRate()) {
98
            $data->{'hourlyRate'} = $object->getHourlyRate();
99
        } else {
100
            $data->{'hourlyRate'} = null;
101
        }
102
        if (null !== $object->getBillable()) {
103
            $data->{'billable'} = $object->getBillable();
104
        } else {
105
            $data->{'billable'} = null;
106
        }
107
        if (null !== $object->getUnspecified()) {
108
            $data->{'unspecified'} = $object->getUnspecified();
109
        } else {
110
            $data->{'unspecified'} = null;
111
        }
112
        if (null !== $object->getBudgetHours()) {
113
            $data->{'budgetHours'} = $object->getBudgetHours();
114
        } else {
115
            $data->{'budgetHours'} = null;
116
        }
117
118
        return $data;
119
    }
120
}
121