Completed
Pull Request — master (#6)
by Nick
15:48
created

Rule   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 342
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 6
dl 36
loc 342
rs 8.2857
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setId() 9 9 2
A getId() 0 4 1
A setLabel() 9 9 2
A getLabel() 0 4 1
A setDescription() 9 9 2
A getDescription() 0 4 1
A setSlotId() 0 9 2
A getSlotId() 0 4 1
A setWeight() 0 9 2
A getWeight() 0 4 1
A setContent() 0 10 2
A getContent() 0 10 2
A setSegment() 9 9 2
A getSegment() 0 4 1
B setStatus() 0 12 5
A getStatus() 0 4 1
A getCreated() 0 7 1
A getUpdated() 0 7 1
B setTestConfig() 0 26 4
B getTestConfig() 0 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Acquia\LiftClient\Entity;
4
5
use Acquia\LiftClient\Exception\LiftSdkException;
6
use DateTime;
7
8
class Rule extends \ArrayObject
9
{
10
    use EntityTrait;
11
12
    /**
13
     * @param array $array
14
     */
15
    public function __construct(array $array = [])
16
    {
17
        parent::__construct($array);
18
    }
19
20
    /**
21
     * Sets the 'id' parameter.
22
     *
23
     * @param string $id
24
     *
25
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
26
     *
27
     * @return \Acquia\LiftClient\Entity\Rule
28
     */
29 View Code Duplication
    public function setId($id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        if (!is_string($id)) {
32
            throw new LiftSdkException('Argument must be an instance of string.');
33
        }
34
        $this['id'] = $id;
35
36
        return $this;
37
    }
38
39
    /**
40
     * Gets the 'id' parameter.
41
     *
42
     * @return string The Identifier of the Rule
43
     */
44
    public function getId()
45
    {
46
        return $this->getEntityValue('id', '');
47
    }
48
49
    /**
50
     * Sets the 'label' parameter.
51
     *
52
     * @param string $label
53
     *
54
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
55
     *
56
     * @return \Acquia\LiftClient\Entity\Rule
57
     */
58 View Code Duplication
    public function setLabel($label)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if (!is_string($label)) {
61
            throw new LiftSdkException('Argument must be an instance of string.');
62
        }
63
        $this['label'] = $label;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Gets the 'label' parameter.
70
     *
71
     * @return string
72
     */
73
    public function getLabel()
74
    {
75
        return $this->getEntityValue('label', '');
76
    }
77
78
    /**
79
     * Sets the 'description' parameter.
80
     *
81
     * @param string $description
82
     *
83
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
84
     *
85
     * @return \Acquia\LiftClient\Entity\Rule
86
     */
87 View Code Duplication
    public function setDescription($description)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        if (!is_string($description)) {
90
            throw new LiftSdkException('Argument must be an instance of string.');
91
        }
92
        $this['description'] = $description;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Gets the 'description' parameter.
99
     *
100
     * @return string The Description of the Rule
101
     */
102
    public function getDescription()
103
    {
104
        return $this->getEntityValue('description', '');
105
    }
106
107
    /**
108
     * Sets the 'slot_id' parameter.
109
     *
110
     * @param string $slotId
111
     *
112
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
113
     *
114
     * @return \Acquia\LiftClient\Entity\Rule
115
     */
116
    public function setSlotId($slotId)
117
    {
118
        if (!is_string($slotId)) {
119
            throw new LiftSdkException('Argument must be an instance of string.');
120
        }
121
        $this['slot_id'] = $slotId;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Gets the 'description' parameter.
128
     *
129
     * @return string The Description of the Rule
130
     */
131
    public function getSlotId()
132
    {
133
        return $this->getEntityValue('slot_id', '');
134
    }
135
136
    /**
137
     * Sets the 'weight' parameter.
138
     *
139
     * @param int $weight
140
     *
141
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
142
     *
143
     * @return \Acquia\LiftClient\Entity\Rule
144
     */
145
    public function setWeight($weight)
146
    {
147
        if (!is_integer($weight)) {
148
            throw new LiftSdkException('Argument must be an instance of integer.');
149
        }
150
        $this['weight'] = $weight;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Gets the 'description' parameter.
157
     *
158
     * @return int The Description of the Rule
159
     */
160
    public function getWeight()
161
    {
162
        return $this->getEntityValue('weight', 0);
163
    }
164
165
    /**
166
     * Sets the 'content' parameter.
167
     *
168
     * @param Content[] $contentList
169
     *
170
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
171
     *
172
     * @return \Acquia\LiftClient\Entity\Rule
173
     */
174
    public function setContent(array $contentList)
175
    {
176
        $this['content'] = [];
177
        foreach ($contentList as $content) {
178
            // We need to 'normalize' the data.
179
            $this['content'][] = $content->getArrayCopy();
180
        }
181
182
        return $this;
183
    }
184
185
    /**
186
     * Gets the 'content' parameter.
187
     *
188
     * @return Content[] The list of content this rule applies to
189
     */
190
    public function getContent()
191
    {
192
        $contentList = $this->getEntityValue('content', '');
193
        $ret = [];
194
        foreach ($contentList as $content) {
195
            $ret[] = new Content($content);
196
        }
197
198
        return $ret;
199
    }
200
201
    /**
202
     * Sets the 'segment' parameter.
203
     *
204
     * @param string $segment
205
     *
206
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
207
     *
208
     * @return \Acquia\LiftClient\Entity\Rule
209
     */
210 View Code Duplication
    public function setSegment($segment)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212
        if (!is_string($segment)) {
213
            throw new LiftSdkException('Argument must be an instance of integer.');
214
        }
215
        $this['segment'] = $segment;
216
217
        return $this;
218
    }
219
220
    /**
221
     * Gets the 'segment' parameter.
222
     *
223
     * @return string
224
     */
225
    public function getSegment()
226
    {
227
        return $this->getEntityValue('segment', '');
228
    }
229
230
    /**
231
     * Sets the 'status' parameter.
232
     *
233
     * @param string $status
234
     *
235
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
236
     *
237
     * @return \Acquia\LiftClient\Entity\Rule
238
     */
239
    public function setStatus($status)
240
    {
241
        if (!is_string($status)) {
242
            throw new LiftSdkException('Argument must be an instance of integer.');
243
        }
244
        if ($status !== 'published' && $status !== 'unpublished' && $status !== 'archived') {
245
            throw new LiftSdkException('Status much be either published, unpublished or archived');
246
        }
247
        $this['status'] = $status;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Gets the 'status' parameter.
254
     *
255
     * @return string
256
     */
257
    public function getStatus()
258
    {
259
        return $this->getEntityValue('status', '');
260
    }
261
262
    /**
263
     * Gets the 'created' parameter.
264
     *
265
     * @return DateTime
266
     */
267
    public function getCreated()
268
    {
269
        $date = $this->getEntityValue('created', '');
270
        $datetime = DateTime::createFromFormat(DateTime::ISO8601, $date);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...eTime::ISO8601, $date); of type DateTime|false adds false to the return on line 272 which is incompatible with the return type documented by Acquia\LiftClient\Entity\Rule::getCreated of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
271
272
        return $datetime;
273
    }
274
275
    /**
276
     * Gets the 'updated' parameter.
277
     *
278
     * @return DateTime
279
     */
280
    public function getUpdated()
281
    {
282
        $date = $this->getEntityValue('updated', '');
283
        $datetime = DateTime::createFromFormat(DateTime::ISO8601, $date);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...eTime::ISO8601, $date); of type DateTime|false adds false to the return on line 285 which is incompatible with the return type documented by Acquia\LiftClient\Entity\Rule::getUpdated of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
284
285
        return $datetime;
286
    }
287
288
    /**
289
     * Sets the Rule test_config property.
290
     *
291
     * @param \Acquia\LiftClient\Entity\TestConfigInterface $testConfig
292
     *
293
     * @return \Acquia\LiftClient\Entity\Rule
294
     */
295
    public function setTestConfig(TestConfigInterface $testConfig)
296
    {
297
        // To facilitate TypeHinting in PHPStorm we redefine what $testConfig is
298
        // here. We know it inherits from the TestConfigInterface and is a child
299
        // of TestConfigBase.
300
        /** @var \Acquia\LiftClient\Entity\TestConfigBase $testConfig */
301
302
        // Get class of the testConfig object.
303
        $type = get_class($testConfig);
304
305
        // Only allow one test type at a time.
306
        $this['testconfig'] = [];
307
        switch ($type) {
308
            case 'TestConfigAb':
309
                $this['testconfig']['ab'] = $testConfig->getArrayCopy();
310
                break;
311
            case 'TestConfigMab':
312
                $this['testconfig']['mab'] = $testConfig->getArrayCopy();
313
                break;
314
            case 'TestConfigTarget':
315
                $this['testconfig']['target'] = $testConfig->getArrayCopy();
316
                break;
317
        }
318
319
        return $this;
320
    }
321
322
    /**
323
     * Gets the 'test_config' parameter.
324
     *
325
     * @return \Acquia\LiftClient\Entity\TestConfigInterface|null $testConfig
326
     */
327
    public function getTestConfig()
328
    {
329
        $testConfig = $this->getEntityValue('testconfig', '');
330
        // We know the array only has one possible set of options.
331
        // Get its key and value.
332
        reset($testConfig);
333
        $key = key($testConfig);
334
335
        // Based on the config, we load the different objects.
336
        switch ($key) {
337
            case 'ab':
338
                return new TestConfigAb($testConfig);
339
            case 'mab':
340
                return new TestConfigMab($testConfig);
341
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
342
            case 'target':
343
                return new TestConfigTarget($testConfig);
344
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
345
        }
346
347
        return null;
348
    }
349
}
350