Passed
Push — master ( 17481c...d9f781 )
by Reyo
02:57
created

TimeNormalizer::normalize()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 85
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 756

Importance

Changes 0
Metric Value
cc 27
eloc 56
nc 16777216
nop 3
dl 0
loc 85
ccs 0
cts 84
cp 0
crap 756
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exception\InvalidArgumentException;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
15
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
17
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
20
class TimeNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
21
{
22
    use DenormalizerAwareTrait;
23
    use NormalizerAwareTrait;
24
25
    public function supportsDenormalization($data, $type, $format = null)
26
    {
27
        return $type === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\Time';
28
    }
29
30
    public function supportsNormalization($data, $format = null)
31
    {
32
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\Time';
33
    }
34
35
    public function denormalize($data, $class, $format = null, array $context = [])
36
    {
37
        if (!is_object($data)) {
38
            throw new InvalidArgumentException(sprintf('Given $data is not an object (%s given). We need an object in order to continue denormalize method.', gettype($data)));
39
        }
40
        $object = new \ConnectHolland\TimechimpBundle\Api\Model\Time();
41
        if (property_exists($data, 'id')) {
42
            $object->setId($data->{'id'});
43
        }
44
        if (property_exists($data, 'customerId')) {
45
            $object->setCustomerId($data->{'customerId'});
46
        }
47
        if (property_exists($data, 'customerName')) {
48
            $object->setCustomerName($data->{'customerName'});
49
        }
50
        if (property_exists($data, 'projectId')) {
51
            $object->setProjectId($data->{'projectId'});
52
        }
53
        if (property_exists($data, 'projectName')) {
54
            $object->setProjectName($data->{'projectName'});
55
        }
56
        if (property_exists($data, 'projectTaskId')) {
57
            $object->setProjectTaskId($data->{'projectTaskId'});
58
        }
59
        if (property_exists($data, 'taskId')) {
60
            $object->setTaskId($data->{'taskId'});
61
        }
62
        if (property_exists($data, 'taskName')) {
63
            $object->setTaskName($data->{'taskName'});
64
        }
65
        if (property_exists($data, 'userId')) {
66
            $object->setUserId($data->{'userId'});
67
        }
68
        if (property_exists($data, 'userDisplayName')) {
69
            $object->setUserDisplayName($data->{'userDisplayName'});
70
        }
71
        if (property_exists($data, 'userTags')) {
72
            $values = [];
73
            foreach ($data->{'userTags'} as $value) {
74
                $values[] = $value;
75
            }
76
            $object->setUserTags($values);
77
        }
78
        if (property_exists($data, 'date')) {
79
            $object->setDate(\DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'date'}));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromForm...H:i:sP', $data->'date') can also be of type false; however, parameter $date of ConnectHolland\Timechimp...i\Model\Time::setDate() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            $object->setDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'date'}));
Loading history...
80
        }
81
        if (property_exists($data, 'hours')) {
82
            $object->setHours($data->{'hours'});
83
        }
84
        if (property_exists($data, 'notes')) {
85
            $object->setNotes($data->{'notes'});
86
        }
87
        if (property_exists($data, 'status')) {
88
            $object->setStatus($data->{'status'});
89
        }
90
        if (property_exists($data, 'startEnd')) {
91
            $object->setStartEnd($data->{'startEnd'});
92
        }
93
        if (property_exists($data, 'start')) {
94
            $object->setStart($data->{'start'});
95
        }
96
        if (property_exists($data, 'end')) {
97
            $object->setEnd($data->{'end'});
98
        }
99
        if (property_exists($data, 'pause')) {
100
            $object->setPause($data->{'pause'});
101
        }
102
        if (property_exists($data, 'externalName')) {
103
            $object->setExternalName($data->{'externalName'});
104
        }
105
        if (property_exists($data, 'externalUrl')) {
106
            $object->setExternalUrl($data->{'externalUrl'});
107
        }
108
        if (property_exists($data, 'statusIntern')) {
109
            $object->setStatusIntern($data->{'statusIntern'});
110
        }
111
        if (property_exists($data, 'statusExtern')) {
112
            $object->setStatusExtern($data->{'statusExtern'});
113
        }
114
        if (property_exists($data, 'tags')) {
115
            $values_1 = [];
116
            foreach ($data->{'tags'} as $value_1) {
117
                $values_1[] = $this->denormalizer->denormalize($value_1, 'ConnectHolland\\TimechimpBundle\\Api\\Model\\Tag', 'json', $context);
118
            }
119
            $object->setTags($values_1);
120
        }
121
122
        return $object;
123
    }
124
125
    public function normalize($object, $format = null, array $context = [])
126
    {
127
        $data = new \stdClass();
128
        if (null !== $object->getId()) {
129
            $data->{'id'} = $object->getId();
130
        }
131
        if (null !== $object->getCustomerId()) {
132
            $data->{'customerId'} = $object->getCustomerId();
133
        }
134
        if (null !== $object->getCustomerName()) {
135
            $data->{'customerName'} = $object->getCustomerName();
136
        }
137
        if (null !== $object->getProjectId()) {
138
            $data->{'projectId'} = $object->getProjectId();
139
        }
140
        if (null !== $object->getProjectName()) {
141
            $data->{'projectName'} = $object->getProjectName();
142
        }
143
        if (null !== $object->getProjectTaskId()) {
144
            $data->{'projectTaskId'} = $object->getProjectTaskId();
145
        }
146
        if (null !== $object->getTaskId()) {
147
            $data->{'taskId'} = $object->getTaskId();
148
        }
149
        if (null !== $object->getTaskName()) {
150
            $data->{'taskName'} = $object->getTaskName();
151
        }
152
        if (null !== $object->getUserId()) {
153
            $data->{'userId'} = $object->getUserId();
154
        }
155
        if (null !== $object->getUserDisplayName()) {
156
            $data->{'userDisplayName'} = $object->getUserDisplayName();
157
        }
158
        if (null !== $object->getUserTags()) {
159
            $values = [];
160
            foreach ($object->getUserTags() as $value) {
161
                $values[] = $value;
162
            }
163
            $data->{'userTags'} = $values;
164
        }
165
        if (null !== $object->getDate()) {
166
            $data->{'date'} = $object->getDate()->format('Y-m-d\\TH:i:sP');
167
        }
168
        if (null !== $object->getHours()) {
169
            $data->{'hours'} = $object->getHours();
170
        }
171
        if (null !== $object->getNotes()) {
172
            $data->{'notes'} = $object->getNotes();
173
        }
174
        if (null !== $object->getStatus()) {
175
            $data->{'status'} = $object->getStatus();
176
        }
177
        if (null !== $object->getStartEnd()) {
178
            $data->{'startEnd'} = $object->getStartEnd();
179
        }
180
        if (null !== $object->getStart()) {
181
            $data->{'start'} = $object->getStart();
182
        }
183
        if (null !== $object->getEnd()) {
184
            $data->{'end'} = $object->getEnd();
185
        }
186
        if (null !== $object->getPause()) {
187
            $data->{'pause'} = $object->getPause();
188
        }
189
        if (null !== $object->getExternalName()) {
190
            $data->{'externalName'} = $object->getExternalName();
191
        }
192
        if (null !== $object->getExternalUrl()) {
193
            $data->{'externalUrl'} = $object->getExternalUrl();
194
        }
195
        if (null !== $object->getStatusIntern()) {
196
            $data->{'statusIntern'} = $object->getStatusIntern();
197
        }
198
        if (null !== $object->getStatusExtern()) {
199
            $data->{'statusExtern'} = $object->getStatusExtern();
200
        }
201
        if (null !== $object->getTags()) {
202
            $values_1 = [];
203
            foreach ($object->getTags() as $value_1) {
204
                $values_1[] = $this->normalizer->normalize($value_1, 'json', $context);
205
            }
206
            $data->{'tags'} = $values_1;
207
        }
208
209
        return $data;
210
    }
211
}
212