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
Push — master ( 3723a1...6d3eb6 )
by Henrique
03:35
created

Iban::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.8666
cc 4
nc 4
nop 1
crap 4
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 bcmod;
17
use function is_string;
18
use function ord;
19
use function preg_match;
20
use function preg_replace_callback;
21
use function str_replace;
22
use function strlen;
23
use function substr;
24
25
/**
26
 * Validates whether the input is a valid IBAN (International Bank Account Number) or not.
27
 *
28
 * @author Mazen Touati <[email protected]>
29
 */
30
final class Iban extends AbstractRule
31
{
32
    private const COUNTRIES_LENGTHS = [
33
        'AL' => 28,
34
        'AD' => 24,
35
        'AT' => 20,
36
        'AZ' => 28,
37
        'BH' => 22,
38
        'BE' => 16,
39
        'BA' => 20,
40
        'BR' => 29,
41
        'BG' => 22,
42
        'CR' => 21,
43
        'HR' => 21,
44
        'CY' => 28,
45
        'CZ' => 24,
46
        'DK' => 18,
47
        'DO' => 28,
48
        'EE' => 20,
49
        'FO' => 18,
50
        'FI' => 18,
51
        'FR' => 27,
52
        'GE' => 22,
53
        'DE' => 22,
54
        'GI' => 23,
55
        'GR' => 27,
56
        'GL' => 18,
57
        'GT' => 28,
58
        'HU' => 28,
59
        'IS' => 26,
60
        'IE' => 22,
61
        'IL' => 23,
62
        'IT' => 27,
63
        'JO' => 30,
64
        'KZ' => 20,
65
        'KW' => 30,
66
        'LV' => 21,
67
        'LB' => 28,
68
        'LI' => 21,
69
        'LT' => 20,
70
        'LU' => 20,
71
        'MK' => 19,
72
        'MT' => 31,
73
        'MR' => 27,
74
        'MU' => 30,
75
        'MD' => 24,
76
        'MC' => 27,
77
        'ME' => 22,
78
        'NL' => 18,
79
        'NO' => 15,
80
        'PK' => 24,
81
        'PL' => 28,
82
        'PS' => 29,
83
        'PT' => 25,
84
        'QA' => 29,
85
        'XK' => 20,
86
        'RO' => 24,
87
        'LC' => 32,
88
        'SM' => 27,
89
        'ST' => 25,
90
        'SA' => 24,
91
        'RS' => 22,
92
        'SC' => 31,
93
        'SK' => 24,
94
        'SI' => 19,
95
        'ES' => 24,
96
        'SE' => 24,
97
        'CH' => 21,
98
        'TL' => 23,
99
        'TN' => 24,
100
        'TR' => 26,
101
        'UA' => 29,
102
        'AE' => 23,
103
        'GB' => 22,
104
        'VG' => 24,
105
    ];
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 23
    public function validate($input): bool
111
    {
112 23
        if (!is_string($input)) {
113 4
            return false;
114
        }
115
116 19
        $iban = str_replace(' ', '', $input);
117 19
        if (!preg_match('/[A-Z0-9]{15,34}/', $iban)) {
118 3
            return false;
119
        }
120
121 17
        $countryCode = substr($iban, 0, 2);
122 17
        if (!$this->hasValidCountryLength($iban, $countryCode)) {
123 6
            return false;
124
        }
125
126 12
        $checkDigits = substr($iban, 2, 2);
127 12
        $bban = substr($iban, 4);
128 12
        $rearranged = $bban.$countryCode.$checkDigits;
129
130 12
        return bcmod($this->convertToInteger($rearranged), '97') === '1';
131
    }
132
133 17
    private function hasValidCountryLength(string $iban, string $countryCode): bool
134
    {
135 17
        if (!isset(self::COUNTRIES_LENGTHS[$countryCode])) {
136 1
            return false;
137
        }
138
139 16
        return strlen($iban) === self::COUNTRIES_LENGTHS[$countryCode];
140
    }
141
142 12
    private function convertToInteger(string $reArrangedIban): string
143
    {
144 12
        return preg_replace_callback(
145 12
            '/[A-Z]/',
146
            static function (array $match): int {
147 12
                return ord($match[0]) - 55;
148 12
            },
149 12
            $reArrangedIban
150
        );
151
    }
152
}
153