Test Failed
Pull Request — master (#31)
by
unknown
02:31
created

Interval::validate()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 18
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 8
nop 1
crap 110

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 CommerceGuys\AuthNet\DataTypes;
4
5
6
class Interval extends BaseDataType
7
{
8
    protected $propertyMap = [
9
        'length',
10
        'unit',
11
    ];
12
13
    /**
14
     * Allows child classes to validate incoming values.
15
     *
16
     * @param array $values
17
     */
18
    protected function validate(array $values)
19
    {
20
        if (!isset($values['length'])) {
21
            throw new \InvalidArgumentException('Interval must have a length.');
22
        }
23
        if (!isset($values['unit'])) {
24
            throw new \InvalidArgumentException('Interval must have a unit.');
25
        }
26
        if (array_intersect(['days', 'months'], $values['unit'])) {
27
            throw new \InvalidArgumentException('Interval unit must be days or months.');
28
        }
29
        switch ($values['unit']) {
30
            case 'days':
31
                if ($values['length'] < 7 || $values['length'] > 365) {
32
                    throw new \InvalidArgumentException('Interval length for days must be between 7 and 365, inclusive.');
33
                }
34
                break;
35
36
            case 'months':
37
                if ($values['length'] < 1 || $values['length'] > 12) {
38
                    throw new \InvalidArgumentException('Interval length for months must be between 1 and 12, inclusive.');
39
                }
40
                break;
41
        }
42
    }
43
}
44