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

Passed
Pull Request — master (#1087)
by Henrique
03:56 queued 01:10
created

Luhn::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
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
/**
17
 * @see https://en.wikipedia.org/wiki/Luhn_algorithm
18
 */
19
class Luhn extends AbstractRule
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 10
    public function isValid($input): bool
25
    {
26 10
        if (!(new Digit())->isValid($input)) {
27
            return false;
28
        }
29
30 10
        return $this->isLuhn($input);
31
    }
32
33 10
    private function isLuhn($input): bool
34
    {
35 10
        $sum = 0;
36 10
        $numDigits = mb_strlen($input);
37 10
        $parity = $numDigits % 2;
38 10
        for ($i = 0; $i < $numDigits; ++$i) {
39 10
            $digit = mb_substr($input, $i, 1);
40 10
            if ($parity == ($i % 2)) {
41 10
                $digit <<= 1;
42 10
                if (9 < $digit) {
43 6
                    $digit = $digit - 9;
44
                }
45
            }
46 10
            $sum += $digit;
47
        }
48
49 10
        return 0 == ($sum % 10);
50
    }
51
}
52