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 ( f23b3a...c522f6 )
by Henrique
03:19
created

PolishIdCard   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 23 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;
15
16
use function ord;
17
use function preg_match;
18
19
/**
20
 * Validates whether the input is a Polish identity card (Dowód Osobisty).
21
 *
22
 * @see https://en.wikipedia.org/wiki/Polish_identity_card
23
 *
24
 * @author Henrique Moody <[email protected]>
25
 */
26
final class PolishIdCard extends AbstractRule
27
{
28
    private const ASCII_CODE_0 = 48;
29
    private const ASCII_CODE_7 = 55;
30
    private const ASCII_CODE_9 = 57;
31
    private const ASCII_CODE_A = 65;
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function validate($input): bool
37
    {
38
        if (!preg_match('/^[A-Z0-9]{9}$/', $input)) {
39
            return false;
40
        }
41
42
        $weights = [7, 3, 1, 0, 7, 3, 1, 7, 3];
43
        $weightedSum = 0;
44
        for ($i = 0; $i < 9; ++$i) {
45
            $code = ord($input[$i]);
46
            if ($i < 3 && $code <= self::ASCII_CODE_9) {
47
                return false;
48
            }
49
50
            if ($i > 2 && $code >= self::ASCII_CODE_A) {
51
                return false;
52
            }
53
54
            $difference = $code <= self::ASCII_CODE_9 ? self::ASCII_CODE_0 : self::ASCII_CODE_7;
55
            $weightedSum += ($code - $difference) * $weights[$i];
56
        }
57
58
        return $weightedSum % 10 == $input[3];
59
    }
60
}
61