Completed
Push — master ( 743367...2eb0ee )
by Gabriel
06:30
created

Variant   B

Complexity

Total Complexity 54

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 75.64%

Importance

Changes 0
Metric Value
wmc 54
lcom 1
cbo 5
dl 0
loc 281
ccs 118
cts 156
cp 0.7564
rs 7.0642
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __clone() 0 6 1
A getId() 0 4 1
A getImages() 0 4 1
A getAttributes() 0 4 1
A getPrices() 0 4 1
A getCodes() 0 4 1
A getItemsAttributes() 0 4 1
A getName() 0 4 1
A getDescription() 0 4 1
A getWeightUnit() 0 4 1
A getLengthUnit() 0 4 1
A getWeight() 0 4 1
A getWidth() 0 4 1
A getHeight() 0 4 1
A getDepth() 0 4 1
A getCreation() 0 4 1
A getModification() 0 4 1
C reset() 0 62 20
A deleteImage() 0 4 1
A setImage() 0 4 1
A setName() 0 4 1
A setDescription() 0 4 1
A setWeightUnit() 0 4 1
A setLengthUnit() 0 4 1
A setWeight() 0 4 1
A setWidth() 0 4 1
A setHeight() 0 4 1
A setDepth() 0 4 1
B jsonSerialize() 0 27 4
A getNote() 0 4 1
A setNote() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Variant 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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

1
<?php
2
3
namespace Waredesk\Models\Product;
4
5
use DateTime;
6
use JsonSerializable;
7
use Waredesk\Collections\Products\Variants\Items\Attributes as ItemsAttributes;
8
use Waredesk\Collections\Products\Variants\Attributes;
9
use Waredesk\Collections\Products\Variants\Codes;
10
use Waredesk\Collections\Products\Variants\Prices;
11
use Waredesk\Entity;
12
use Waredesk\Image;
13
use Waredesk\ReplaceableEntity;
14
15
class Variant implements Entity, ReplaceableEntity, JsonSerializable
16
{
17
    public const WEIGHT_UNIT_IMPERIAL = 'imperial';
18
    public const WEIGHT_UNIT_METRIC = 'metric';
19
    public const LENGTH_UNIT_IMPERIAL = 'imperial';
20
    public const LENGTH_UNIT_METRIC = 'metric';
21
22
    private $id;
23
    private $images;
24
    private $attributes;
25
    private $prices;
26
    private $codes;
27
    private $items_attributes;
28
    private $name;
29
    private $description;
30
    private $note;
31
    private $weight_unit = self::WEIGHT_UNIT_METRIC;
32
    private $length_unit = self::LENGTH_UNIT_METRIC;
33
    private $weight;
34
    private $width;
35
    private $height;
36
    private $depth;
37
    private $creation;
38
    private $modification;
39
40
    /**
41
     * @var Image
42
     */
43
    private $pendingImage;
44
45
    /**
46
     * @var bool
47
     */
48
    private $deleteImage = false;
49
50 5
    public function __construct()
51
    {
52 5
        $this->attributes = new Attributes();
53 5
        $this->codes = new Codes();
54 5
        $this->prices = new Prices();
55 5
        $this->items_attributes = new ItemsAttributes();
56 5
    }
57
58
    public function __clone()
59
    {
60
        $this->attributes = clone $this->attributes;
61
        $this->codes = clone $this->codes;
62
        $this->prices = clone $this->prices;
63
    }
64
65 4
    public function getId(): ? string
66
    {
67 4
        return $this->id;
68
    }
69
70 1
    public function getImages(): ? array
71
    {
72 1
        return $this->images;
73
    }
74
75 4
    public function getAttributes(): ? Attributes
76
    {
77 4
        return $this->attributes;
78
    }
79
80 4
    public function getPrices(): ? Prices
81
    {
82 4
        return $this->prices;
83
    }
84
85 4
    public function getCodes(): ? Codes
86
    {
87 4
        return $this->codes;
88
    }
89
90 4
    public function getItemsAttributes(): ? ItemsAttributes
91
    {
92 4
        return $this->items_attributes;
93
    }
94
95 4
    public function getName(): ? string
96
    {
97 4
        return $this->name;
98
    }
99
100 4
    public function getDescription(): ? string
101
    {
102 4
        return $this->description;
103
    }
104
105 4
    public function getNote(): ? string
106
    {
107 4
        return $this->note;
108
    }
109
110 4
    public function getWeightUnit(): ? string
111
    {
112 4
        return $this->weight_unit;
113
    }
114
115 4
    public function getLengthUnit(): ? string
116
    {
117 4
        return $this->length_unit;
118
    }
119
120 4
    public function getWeight(): ? float
121
    {
122 4
        return $this->weight;
123
    }
124
125 4
    public function getWidth(): ? float
126
    {
127 4
        return $this->width;
128
    }
129
130 4
    public function getHeight(): ? float
131
    {
132 4
        return $this->height;
133
    }
134
135 4
    public function getDepth(): ? float
136
    {
137 4
        return $this->depth;
138
    }
139
140
    public function getCreation(): ? DateTime
141
    {
142
        return $this->creation;
143
    }
144
145
    public function getModification(): ? DateTime
146
    {
147
        return $this->modification;
148
    }
149
150 4
    public function reset(array $data = null)
151
    {
152 4
        if ($data) {
153 4
            foreach ($data as $key => $value) {
154
                switch ($key) {
155 4
                    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...
156 4
                        $this->id = $value;
157 4
                        break;
158 4
                    case 'images':
159 4
                        $this->deleteImage = false;
160 4
                        $this->pendingImage = null;
161 4
                        $this->images = $value;
162 4
                        break;
163 4
                    case 'attributes':
164 4
                        $this->attributes = $value;
165 4
                        break;
166 4
                    case 'prices':
167 4
                        $this->prices = $value;
168 4
                        break;
169 4
                    case 'codes':
170 4
                        $this->codes = $value;
171 4
                        break;
172 4
                    case 'items_attributes':
173 4
                        $this->items_attributes = $value;
174 4
                        break;
175 4
                    case 'name':
176 4
                        $this->name = $value;
177 4
                        break;
178 4
                    case 'description':
179 4
                        $this->description = $value;
180 4
                        break;
181 4
                    case 'note':
182 4
                        $this->note = $value;
183 4
                        break;
184 4
                    case 'weight_unit':
185 4
                        $this->weight_unit = $value;
186 4
                        break;
187 4
                    case 'length_unit':
188 4
                        $this->length_unit = $value;
189 4
                        break;
190 4
                    case 'weight':
191 4
                        $this->weight = $value;
192 4
                        break;
193 4
                    case 'width':
194 4
                        $this->width = $value;
195 4
                        break;
196 4
                    case 'height':
197 4
                        $this->height = $value;
198 4
                        break;
199 4
                    case 'depth':
200 4
                        $this->depth = $value;
201 4
                        break;
202 4
                    case 'creation':
203 4
                        $this->creation = $value;
204 4
                        break;
205 4
                    case 'modification':
206 4
                        $this->modification = $value;
207 4
                        break;
208
                }
209
            }
210
        }
211 4
    }
212
213
    public function deleteImage()
214
    {
215
        $this->deleteImage = true;
216
    }
217
218 1
    public function setImage(Image $image = null)
219
    {
220 1
        $this->pendingImage = $image;
221 1
    }
222
223 3
    public function setName(string $name)
224
    {
225 3
        $this->name = $name;
226 3
    }
227
228
    public function setDescription(string $description = null)
229
    {
230
        $this->description = $description;
231
    }
232
233
    public function setNote(string $note = null)
234
    {
235
        $this->note = $note;
236
    }
237
238
    public function setWeightUnit(string $weight_unit = null)
239
    {
240
        $this->weight_unit = $weight_unit;
241
    }
242
243
    public function setLengthUnit(string $length_unit = null)
244
    {
245
        $this->length_unit = $length_unit;
246
    }
247
248
    public function setWeight(float $weight = null)
249
    {
250
        $this->weight = $weight;
251
    }
252
253
    public function setWidth(float $width = null)
254
    {
255
        $this->width = $width;
256
    }
257
258
    public function setHeight(float $height = null)
259
    {
260
        $this->height = $height;
261
    }
262
263
    public function setDepth(float $depth = null)
264
    {
265
        $this->depth = $depth;
266
    }
267
268 3
    public function jsonSerialize(): array
269
    {
270
        $returnValue = [
271 3
            'attributes' => $this->getAttributes()->jsonSerialize(),
272 3
            'prices' => $this->getPrices()->jsonSerialize(),
273 3
            'codes' => $this->getCodes()->jsonSerialize(),
274 3
            'items_attributes' => $this->getItemsAttributes()->jsonSerialize(),
275 3
            'name' => $this->getName(),
276 3
            'description' => $this->getDescription(),
277 3
            'note' => $this->getNote(),
278 3
            'weight_unit' => $this->getWeightUnit(),
279 3
            'length_unit' => $this->getLengthUnit(),
280 3
            'weight' => $this->getWeight(),
281 3
            'width' => $this->getWidth(),
282 3
            'height' => $this->getHeight(),
283 3
            'depth' => $this->getDepth(),
284
        ];
285 3
        if ($this->pendingImage) {
286 1
            $returnValue['image'] = $this->pendingImage->toBase64();
287 2
        } elseif ($this->deleteImage) {
288
            $returnValue['image'] = null;
289
        }
290 3
        if ($this->getId()) {
291
            $returnValue = array_merge(['id' => $this->getId()], $returnValue);
292
        }
293 3
        return $returnValue;
294
    }
295
}
296