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
Push — master ( 478e24...410658 )
by Henrique
02:57
created

Luhn::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use function mb_strlen;
17
use function mb_substr;
18
19
/**
20
 * Validate whether a given input is a Luhn number.
21
 *
22
 * @see https://en.wikipedia.org/wiki/Luhn_algorithm
23
 *
24
 * @author Alexander Gorshkov <[email protected]>
25
 * @author Danilo Correa <[email protected]>
26
 * @author Henrique Moody <[email protected]>
27
 */
28
final class Luhn extends AbstractRule
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 14
    public function validate($input): bool
34
    {
35 14
        if (!(new Digit())->validate($input)) {
36 5
            return false;
37
        }
38
39 9
        return $this->isValid((string) $input);
40
    }
41
42 9
    private function isValid(string $input): bool
43
    {
44 9
        $sum = 0;
45 9
        $numDigits = mb_strlen($input);
46 9
        $parity = $numDigits % 2;
47 9
        for ($i = 0; $i < $numDigits; ++$i) {
48 9
            $digit = mb_substr($input, $i, 1);
49 9
            if ($parity == ($i % 2)) {
50 8
                $digit <<= 1;
51 8
                if (9 < $digit) {
52 6
                    $digit = $digit - 9;
53
                }
54
            }
55 9
            $sum += $digit;
56
        }
57
58 9
        return 0 == ($sum % 10);
59
    }
60
}
61