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 — 1.1 (#1169)
by Henrique
02:59
created

PlIdentityCard::validate()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.0231

Importance

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