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

Variant   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76.69%

Importance

Changes 0
Metric Value
wmc 47
lcom 1
cbo 4
dl 0
loc 240
ccs 102
cts 133
cp 0.7669
rs 8.439
c 0
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
A getImages() 0 4 1
A __construct() 0 6 1
A getId() 0 4 1
A getOptions() 0 4 1
A getCodes() 0 4 1
A getPrices() 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 getCreationDatetime() 0 4 1
A getModificationDatetime() 0 4 1
C reset() 0 56 18
A deleteImage() 0 4 1
A setImage() 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 jsonSerialize() 0 22 3

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\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