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 (#868)
by Henrique
03:06
created

Luhn::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
/**
15
 * @link https://en.wikipedia.org/wiki/Luhn_algorithm
16
 */
17
class Luhn extends AbstractRule
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 8
    public function validate($input)
23
    {
24 8
        if (!(new Digit())->validate($input)) {
25
            return false;
26
        }
27
28 8
        return $this->isValid($input);
29
    }
30
31 8
    private function isValid($input): bool
32
    {
33 8
        $sum = 0;
34 8
        $numDigits = strlen($input);
35 8
        $parity = $numDigits % 2;
36 8
        for ($i = 0; $i < $numDigits; ++$i) {
37 8
            $digit = substr($input, $i, 1);
38 8
            if ($parity == ($i % 2)) {
39 8
                $digit <<= 1;
40 8
                if (9 < $digit) {
41 4
                    $digit = $digit - 9;
42
                }
43
            }
44 8
            $sum += $digit;
45
        }
46
47 8
        return 0 == ($sum % 10);
48
    }
49
}
50