Passed
Pull Request — master (#31)
by
unknown
02:26
created

Interval::validate()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 15.8818

Importance

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

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