Passed
Push — master ( 248c35...f550ad )
by Reyo
04:07
created

ExpenseNormalizer::denormalize()   F

Complexity

Conditions 19
Paths > 20000

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 59
ccs 0
cts 58
cp 0
rs 0.3499
cc 19
nc 131073
nop 4
crap 380

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\Client\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 ExpenseNormalizer 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\\Client\\Model\\Expense';
28
    }
29
30
    public function supportsNormalization($data, $format = null)
31
    {
32
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Client\\Model\\Expense';
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\Client\Model\Expense();
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, 'categoryName')) {
57
            $object->setCategoryName($data->{'categoryName'});
58
        }
59
        if (property_exists($data, 'categoryId')) {
60
            $object->setCategoryId($data->{'categoryId'});
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\Expense::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, 'notes')) {
72
            $object->setNotes($data->{'notes'});
73
        }
74
        if (property_exists($data, 'attachment')) {
75
            $object->setAttachment($data->{'attachment'});
76
        }
77
        if (property_exists($data, 'quantity')) {
78
            $object->setQuantity($data->{'quantity'});
79
        }
80
        if (property_exists($data, 'rate')) {
81
            $object->setRate($data->{'rate'});
82
        }
83
        if (property_exists($data, 'tax')) {
84
            $object->setTax($data->{'tax'});
85
        }
86
        if (property_exists($data, 'billable')) {
87
            $object->setBillable($data->{'billable'});
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->getCategoryName()) {
115
            $data->{'categoryName'} = $object->getCategoryName();
116
        }
117
        if (null !== $object->getCategoryId()) {
118
            $data->{'categoryId'} = $object->getCategoryId();
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->getNotes()) {
130
            $data->{'notes'} = $object->getNotes();
131
        }
132
        if (null !== $object->getAttachment()) {
133
            $data->{'attachment'} = $object->getAttachment();
134
        }
135
        if (null !== $object->getQuantity()) {
136
            $data->{'quantity'} = $object->getQuantity();
137
        }
138
        if (null !== $object->getRate()) {
139
            $data->{'rate'} = $object->getRate();
140
        }
141
        if (null !== $object->getTax()) {
142
            $data->{'tax'} = $object->getTax();
143
        }
144
        if (null !== $object->getBillable()) {
145
            $data->{'billable'} = $object->getBillable();
146
        }
147
        if (null !== $object->getStatus()) {
148
            $data->{'status'} = $object->getStatus();
149
        }
150
151
        return $data;
152
    }
153
}
154