LuhnAlgorithm::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
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 GusDeCooL\Cryptography\Algorithm;
4
5
/**
6
 * Luhn Algorithm
7
 *
8
 * @package GusDeCooL\Cryptography\Algorithm\Algorithm
9
 * @author Budi Arsana <[email protected]>
10
 */
11
class LuhnAlgorithm
12
{
13
    /**
14
     * @param string $number
15
     * @return bool
16
     */
17 1
    public function isValid($number)
18
    {
19 1
        settype($number, 'string');
20
        $sumTable = [
21 1
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
22
            [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
23
        ];
24 1
        $sum = 0;
25 1
        $flip = 0;
26
27 1
        for ($i = strlen($number) - 1; $i >= 0; $i--) {
28 1
            $sum += $sumTable[$flip++ % 2][$number[$i]];
29
        }
30
31 1
        return $sum % 10 === 0;
32
    }
33
}
34