Completed
Push — master ( b2620d...cacd0e )
by Gabriel
02:27
created

Variant::reset()   C

Complexity

Conditions 18
Paths 18

Size

Total Lines 56
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 18

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 51
cts 51
cp 1
rs 6.5661
c 0
b 0
f 0
cc 18
eloc 51
nc 18
nop 1
crap 18

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
namespace Waredesk\Models\Product;
4
5
use DateTime;
6
use JsonSerializable;
7
use Waredesk\Collections\Products\Variants\Codes;
8
use Waredesk\Collections\Products\Variants\Options;
9
use Waredesk\Collections\Products\Variants\Prices;
10
use Waredesk\Image;
11
12
class Variant implements JsonSerializable
13
{
14
    private $id;
15
    private $images;
16
    private $options;
17
    private $codes;
18
    private $prices;
19
    private $description;
20
    private $notes;
21
    private $weight_unit;
22
    private $length_unit;
23
    private $weight;
24
    private $width;
25
    private $height;
26
    private $depth;
27
    private $creation_datetime;
28
    private $modification_datetime;
29
30
    /**
31
     * @var Image
32
     */
33
    private $pendingImage;
34
35
    /**
36
     * @var Image
37
     */
38
    private $deleteImage;
39
40 3
    public function __construct()
41
    {
42 3
        $this->options = new Options();
43 3
        $this->codes = new Codes();
44 3
        $this->prices = new Prices();
45 3
    }
46
47
    public function getId(): ?int
48
    {
49
        return $this->id;
50
    }
51
52 1
    public function getImages(): ?array
53
    {
54 1
        return $this->images;
55
    }
56
57 2
    public function getOptions(): ?Options
58
    {
59 2
        return $this->options;
60
    }
61
62 2
    public function getCodes(): ?Codes
63
    {
64 2
        return $this->codes;
65
    }
66
67 2
    public function getPrices(): ?Prices
68
    {
69 2
        return $this->prices;
70
    }
71
72 2
    public function getDescription(): ?string
73
    {
74 2
        return $this->description;
75
    }
76
77 2
    public function getNotes(): ?string
78
    {
79 2
        return $this->notes;
80
    }
81
82 2
    public function getWeightUnit(): ?string
83
    {
84 2
        return $this->weight_unit;
85
    }
86
87 2
    public function getLengthUnit(): ?string
88
    {
89 2
        return $this->length_unit;
90
    }
91
92 2
    public function getWeight(): ?float
93
    {
94 2
        return $this->weight;
95
    }
96
97 2
    public function getWidth(): ?float
98
    {
99 2
        return $this->width;
100
    }
101
102 2
    public function getHeight(): ?float
103
    {
104 2
        return $this->height;
105
    }
106
107 2
    public function getDepth(): ?float
108
    {
109 2
        return $this->depth;
110
    }
111
112
    public function getCreationDatetime(): ?DateTime
113
    {
114
        return $this->creation_datetime;
115
    }
116
117
    public function getModificationDatetime(): ?DateTime
118
    {
119
        return $this->modification_datetime;
120
    }
121
122 3
    public function reset(array $data = null)
123
    {
124 3
        if ($data) {
125 3
            foreach ($data as $key => $value) {
126
                switch ($key) {
127 3
                    case 'id':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
128 3
                        $this->id = $value;
129 3
                        break;
130 3
                    case 'images':
131 3
                        $this->deleteImage = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<Waredesk\Image> of property $deleteImage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
132 3
                        $this->pendingImage = null;
133 3
                        $this->images = $value;
134 3
                        break;
135 3
                    case 'options':
136 3
                        $this->options = $value;
137 3
                        break;
138 3
                    case 'codes':
139 3
                        $this->codes = $value;
140 3
                        break;
141 3
                    case 'prices':
142 3
                        $this->prices = $value;
143 3
                        break;
144 3
                    case 'description':
145 3
                        $this->description = $value;
146 3
                        break;
147 3
                    case 'notes':
148 3
                        $this->notes = $value;
149 3
                        break;
150 3
                    case 'weight_unit':
151 3
                        $this->weight_unit = $value;
152 3
                        break;
153 3
                    case 'length_unit':
154 3
                        $this->length_unit = $value;
155 3
                        break;
156 3
                    case 'weight':
157 3
                        $this->weight = $value;
158 3
                        break;
159 3
                    case 'width':
160 3
                        $this->width = $value;
161 3
                        break;
162 3
                    case 'height':
163 3
                        $this->height = $value;
164 3
                        break;
165 3
                    case 'depth':
166 3
                        $this->depth = $value;
167 3
                        break;
168 3
                    case 'creation_datetime':
169 3
                        $this->creation_datetime = $value;
170 3
                        break;
171 3
                    case 'modification_datetime':
172 3
                        $this->modification_datetime = $value;
173 3
                        break;
174
                }
175
            }
176
        }
177 3
    }
178
179
    public function deleteImage()
180
    {
181
        $this->deleteImage = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type boolean is incompatible with the declared type object<Waredesk\Image> of property $deleteImage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
182
    }
183
184 1
    public function setImage(Image $image = null)
185
    {
186 1
        $this->pendingImage = $image;
187 1
    }
188
189 2
    public function setDescription(string $description = null)
190
    {
191 2
        $this->description = $description;
192 2
    }
193
194
    public function setNotes(string $notes = null)
195
    {
196
        $this->notes = $notes;
197
    }
198
199
    public function setWeightUnit(string $weight_unit = null)
200
    {
201
        $this->weight_unit = $weight_unit;
202
    }
203
204
    public function setLengthUnit(string $length_unit = null)
205
    {
206
        $this->length_unit = $length_unit;
207
    }
208
209
    public function setWeight(float $weight = null)
210
    {
211
        $this->weight = $weight;
212
    }
213
214
    public function setWidth(float $width = null)
215
    {
216
        $this->width = $width;
217
    }
218
219
    public function setHeight(float $height = null)
220
    {
221
        $this->height = $height;
222
    }
223
224
    public function setDepth(float $depth = null)
225
    {
226
        $this->depth = $depth;
227
    }
228
229 2
    public function jsonSerialize()
230
    {
231
        $returnValue = [
232 2
            'description' => $this->getDescription(),
233 2
            'notes' => $this->getNotes(),
234 2
            'options' => $this->getOptions()->jsonSerialize(),
235 2
            'codes' => $this->getCodes()->jsonSerialize(),
236 2
            'prices' => $this->getPrices()->jsonSerialize(),
237 2
            'weight_unit' => $this->getWeightUnit(),
238 2
            'length_unit' => $this->getLengthUnit(),
239 2
            'weight' => $this->getWeight(),
240 2
            'width' => $this->getWidth(),
241 2
            'height' => $this->getHeight(),
242 2
            'depth' => $this->getDepth(),
243
        ];
244 2
        if ($this->pendingImage) {
245 1
            $returnValue['image'] = $this->pendingImage->toBase64();
246 1
        } else if ($this->deleteImage) {
247
            $returnValue['image'] = null;
248
        }
249 2
        return $returnValue;
250
    }
251
}
252