Completed
Push — master ( cb44f9...1fdaf4 )
by Gabriel
07:02
created

Variant   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 74.5%

Importance

Changes 0
Metric Value
wmc 52
lcom 1
cbo 4
dl 0
loc 270
ccs 111
cts 149
cp 0.745
rs 7.9487
c 0
b 0
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __clone() 0 6 1
A getImages() 0 4 1
A getAnnotations() 0 4 1
A getPrices() 0 4 1
A getCodes() 0 4 1
A getName() 0 4 1
A getDescription() 0 4 1
A getNotes() 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 59 19
A deleteImage() 0 4 1
A setImage() 0 4 1
A setName() 0 4 1
A setDescription() 0 4 1
A setNotes() 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
A getId() 0 4 1
B jsonSerialize() 0 26 4

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