Imei::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2
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