Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#850)
by
unknown
06:21
created

Luhn::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Respect\Validation\Rules;
4
5
class Luhn extends AbstractRule
6
{
7
    public function validate($input)
8
    {
9
        $input = preg_replace('([^0-9])', '', $input);
10
11
        if (empty($input)) {
12
            return false;
13
        }
14
15
        return $this->verifyMod10($input);
16
    }
17
18
    /**
19
     * Returns whether the input matches the Luhn algorithm or not.
20
     *
21
     * @param string $input
22
     *
23
     * @return bool
24
     */
25
    private function verifyMod10($input)
26
    {
27
        $sum = 0;
28
        $input = strrev($input);
29
        for ($i = 0, $inputLength = mb_strlen($input); $i < $inputLength; ++$i) {
30
            $current = mb_substr($input, $i, 1);
31
            if ($i % 2 === 1) {
32
                $current *= 2;
33
                if ($current > 9) {
34
                    $firstDigit = $current % 10;
35
                    $secondDigit = ($current - $firstDigit) / 10;
36
                    $current = $firstDigit + $secondDigit;
37
                }
38
            }
39
            $sum += $current;
40
        }
41
42
        return $sum % 10 === 0;
43
    }
44
}
45