Imei::validateLuhnAlgorithm()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 6
nop 1
crap 4
1
<?php
2
3
namespace ValueObjects\Identity;
4
5
use ValueObjects\Exception\InvalidNativeArgumentException;
6
use ValueObjects\Number\Natural;
7
8
class Imei extends Natural
9
{
10
    /**
11
     * Returns an Imei number
12
     *
13
     * @param int $value
14
     */
15 4
    public function __construct($value)
16
    {
17
        $options = array(
18
            'options' => array(
19 4
                'min_range' => 100000000000000,
20
                'max_range' => 999999999999999
21 4
            )
22 4
        );
23
24 4
        $value = filter_var($value, FILTER_VALIDATE_INT, $options);
25
26 4
        if (false === $value) {
27 2
            throw new InvalidNativeArgumentException($value, array('int (15 digits)'));
28
        }
29
30 2
        $value = $this->validateLuhnAlgorithm($value);
31
32 1
        $this->value = $value;
33 1
    }
34
35
    /**
36
     * Checks the validity of the Imei using the Luhn algorithm
37
     *
38
     * @param int $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     * @return int
40
     * @throws \ValueObjects\Exception\InvalidNativeArgumentException
41
     */
42 2
    private function validateLuhnAlgorithm($imei)
43
    {
44 2
        $str = '';
45
46 2
        foreach (str_split(strrev((string) $imei)) as $i => $d) {
47 2
            $str .= $i %2 !== 0 ? $d * 2 : $d;
48 2
        }
49
50 2
        if (array_sum(str_split($str)) % 10 !== 0) {
51 1
            throw new InvalidNativeArgumentException($imei, array('int (valid Imei code)'));
52
        }
53
54 1
        return $imei;
55
    }
56
}
57