Passed
Push — master ( bd41e0...b80a23 )
by Gabriel
02:52
created

Item::reset()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 39
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 5.1234
c 0
b 0
f 0
ccs 0
cts 34
cp 0
cc 13
eloc 34
nc 13
nop 1
crap 182

How to fix   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\Inventory;
4
5
use DateTime;
6
use JsonSerializable;
7
use Waredesk\Collections\Inventory\Items\Activities;
8
use Waredesk\Collections\Inventory\Items\Codes;
9
use Waredesk\Entity;
10
use Waredesk\ReplaceableEntity;
11
12
class Item implements Entity, ReplaceableEntity, JsonSerializable
13
{
14
    private $id;
15
    private $warehouse;
16
    private $product;
17
    private $variant;
18
    private $activities;
19
    private $codes;
20
    private $in_stock;
21
    private $note;
22
    private $creation;
23
    private $modification;
24
25
    public function __construct(array $data = null)
26
    {
27
        $this->activities = new Activities();
28
        $this->codes = new Codes();
29
        $this->reset($data);
30
    }
31
32
    public function __clone()
33
    {
34
        $this->activities = clone $this->activities;
35
        $this->codes = clone $this->codes;
36
    }
37
38
    public function getId(): ? string
39
    {
40
        return $this->id;
41
    }
42
43
    public function getWarehouse(): ? string
44
    {
45
        return $this->warehouse;
46
    }
47
48
    public function getProduct(): ? string
49
    {
50
        return $this->product;
51
    }
52
53
    public function getVariant(): ? string
54
    {
55
        return $this->variant;
56
    }
57
58
    public function getActivities(): Activities
59
    {
60
        return $this->activities;
61
    }
62
63
    public function getCodes(): Codes
64
    {
65
        return $this->codes;
66
    }
67
68
    public function isInStock(): ? bool
69
    {
70
        return $this->in_stock;
71
    }
72
73
    public function getNote(): ? string
74
    {
75
        return $this->note;
76
    }
77
78
    public function getCreation(): ? DateTime
79
    {
80
        return $this->creation;
81
    }
82
83
    public function getModification(): ? DateTime
84
    {
85
        return $this->modification;
86
    }
87
88
    public function reset(array $data = null)
89
    {
90
        if ($data) {
91
            foreach ($data as $key => $value) {
92
                switch ($key) {
93
                    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...
94
                        $this->id = $value;
95
                        break;
96
                    case 'warehouse':
97
                        $this->warehouse = $value;
98
                        break;
99
                    case 'product':
100
                        $this->product = $value;
101
                        break;
102
                    case 'variant':
103
                        $this->variant = $value;
104
                        break;
105
                    case 'activities':
106
                        $this->activities = $value;
107
                        break;
108
                    case 'codes':
109
                        $this->codes = $value;
110
                        break;
111
                    case 'in_stock':
112
                        $this->in_stock = $value;
113
                        break;
114
                    case 'note':
115
                        $this->note = $value;
116
                        break;
117
                    case 'creation':
118
                        $this->creation = $value;
119
                        break;
120
                    case 'modification':
121
                        $this->modification = $value;
122
                        break;
123
                }
124
            }
125
        }
126
    }
127
128
    public function setWarehouse(string $warehouse = null)
129
    {
130
        $this->warehouse = $warehouse;
131
    }
132
133
    public function setVariant(string $variant)
134
    {
135
        $this->variant = $variant;
136
    }
137
138
    public function setIsInStock(string $in_stock)
139
    {
140
        $this->in_stock = $in_stock;
141
    }
142
143
    public function setNote(string $note = null)
144
    {
145
        $this->note = $note;
146
    }
147
148
    public function jsonSerialize(): array
149
    {
150
        $returnValue = [
151
            'warehouse' => $this->getWarehouse(),
152
            'variant' => $this->getVariant(),
153
            'activities' => $this->getActivities()->jsonSerialize(),
154
            'codes' => $this->getCodes()->jsonSerialize(),
155
            'in_stock' => $this->isInStock(),
156
            'note' => $this->getNote(),
157
        ];
158
        if ($this->getId()) {
159
            $returnValue = array_merge(['id' => $this->getId()], $returnValue);
160
        }
161
        return $returnValue;
162
    }
163
}
164