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 (#923)
by lee
03:57
created

PlIdentityCard::validate()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 6.1403
c 0
b 0
f 0
cc 8
eloc 13
nc 6
nop 1
crap 8
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\Locale;
15
16
use Respect\Validation\Rules\AbstractRule;
17
18
/**
19
 * Validator for Polish identity card.
20
 *
21
 * @see https://en.wikipedia.org/wiki/Polish_identity_card
22
 */
23
class PlIdentityCard extends AbstractRule
24
{
25 8
    public function validate($input): bool
26
    {
27 8
        if (!preg_match('/^[A-Z0-9]{9}$/', $input)) {
28 1
            return false;
29
        }
30
31 7
        $weights = [7, 3, 1, 0, 7, 3, 1, 7, 3];
32 7
        $weightedSum = 0;
33 7
        for ($i = 0; $i < 9; ++$i) {
34 7
            $code = ord($input[$i]);
35 7
            if ($i < 3 && $code <= 57) { // 57 is "9"
36 1
                return false;
37
            }
38
39 7
            if ($i > 2 && $code >= 65) { // 65 is "A"
40 1
                return false;
41
            }
42
43 7
            $difference = $code <= 57 ? 48 : 55; // 48 is "0"
44 7
            $weightedSum += ($code - $difference) * $weights[$i];
45
        }
46
47 5
        return $weightedSum % 10 == $input[3];
48
    }
49
}
50