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

MileageNormalizer::normalize()   F

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 36
nc 131072
nop 3
dl 0
loc 56
ccs 0
cts 55
cp 0
crap 342
rs 0.7
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 MileageNormalizer 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\\Mileage';
28
    }
29
30
    public function supportsNormalization($data, $format = null)
31
    {
32
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\Mileage';
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\Mileage();
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, 'vehicleId')) {
57
            $object->setVehicleId($data->{'vehicleId'});
58
        }
59
        if (property_exists($data, 'vehicleName')) {
60
            $object->setVehicleName($data->{'vehicleName'});
61
        }
62
        if (property_exists($data, 'userId')) {
63
            $object->setUserId($data->{'userId'});
64
        }
65
        if (property_exists($data, 'userDisplayName')) {
66
            $object->setUserDisplayName($data->{'userDisplayName'});
67
        }
68
        if (property_exists($data, 'date')) {
69
            $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...odel\Mileage::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

69
            $object->setDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'date'}));
Loading history...
70
        }
71
        if (property_exists($data, 'fromAddress')) {
72
            $object->setFromAddress($data->{'fromAddress'});
73
        }
74
        if (property_exists($data, 'toAddress')) {
75
            $object->setToAddress($data->{'toAddress'});
76
        }
77
        if (property_exists($data, 'notes')) {
78
            $object->setNotes($data->{'notes'});
79
        }
80
        if (property_exists($data, 'distance')) {
81
            $object->setDistance($data->{'distance'});
82
        }
83
        if (property_exists($data, 'billable')) {
84
            $object->setBillable($data->{'billable'});
85
        }
86
        if (property_exists($data, 'type')) {
87
            $object->setType($data->{'type'});
88
        }
89
        if (property_exists($data, 'status')) {
90
            $object->setStatus($data->{'status'});
91
        }
92
93
        return $object;
94
    }
95
96
    public function normalize($object, $format = null, array $context = [])
97
    {
98
        $data = new \stdClass();
99
        if (null !== $object->getId()) {
100
            $data->{'id'} = $object->getId();
101
        }
102
        if (null !== $object->getCustomerId()) {
103
            $data->{'customerId'} = $object->getCustomerId();
104
        }
105
        if (null !== $object->getCustomerName()) {
106
            $data->{'customerName'} = $object->getCustomerName();
107
        }
108
        if (null !== $object->getProjectId()) {
109
            $data->{'projectId'} = $object->getProjectId();
110
        }
111
        if (null !== $object->getProjectName()) {
112
            $data->{'projectName'} = $object->getProjectName();
113
        }
114
        if (null !== $object->getVehicleId()) {
115
            $data->{'vehicleId'} = $object->getVehicleId();
116
        }
117
        if (null !== $object->getVehicleName()) {
118
            $data->{'vehicleName'} = $object->getVehicleName();
119
        }
120
        if (null !== $object->getUserId()) {
121
            $data->{'userId'} = $object->getUserId();
122
        }
123
        if (null !== $object->getUserDisplayName()) {
124
            $data->{'userDisplayName'} = $object->getUserDisplayName();
125
        }
126
        if (null !== $object->getDate()) {
127
            $data->{'date'} = $object->getDate()->format('Y-m-d\\TH:i:sP');
128
        }
129
        if (null !== $object->getFromAddress()) {
130
            $data->{'fromAddress'} = $object->getFromAddress();
131
        }
132
        if (null !== $object->getToAddress()) {
133
            $data->{'toAddress'} = $object->getToAddress();
134
        }
135
        if (null !== $object->getNotes()) {
136
            $data->{'notes'} = $object->getNotes();
137
        }
138
        if (null !== $object->getDistance()) {
139
            $data->{'distance'} = $object->getDistance();
140
        }
141
        if (null !== $object->getBillable()) {
142
            $data->{'billable'} = $object->getBillable();
143
        }
144
        if (null !== $object->getType()) {
145
            $data->{'type'} = $object->getType();
146
        }
147
        if (null !== $object->getStatus()) {
148
            $data->{'status'} = $object->getStatus();
149
        }
150
151
        return $data;
152
    }
153
}
154