Completed
Push — master ( 9de2ae...02afd8 )
by Ronan
06:45
created

Iban::validate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 97
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 97
rs 8.2428
cc 4
eloc 82
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Iban.
7
 */
8
class Iban implements IsoCodeInterface
9
{
10
    /**
11
     * Iban validator.
12
     *
13
     * @author  petitchevalroux
14
     * @licence originale http://creativecommons.org/licenses/by-sa/2.0/fr/
15
     *
16
     * @link    http://dev.petitchevalroux.net/php/validation-iban-php.356.html + comments & links
17
     *
18
     * @param string $iban
19
     *
20
     * @return bool
21
     */
22
    public static function validate($iban)
23
    {
24
        // Per country validation rules
25
        static $rules = array(
26
            'AL' => '[0-9]{8}[0-9A-Z]{16}',
27
            'AD' => '[0-9]{8}[0-9A-Z]{12}',
28
            'AT' => '[0-9]{16}',
29
            'BE' => '[0-9]{12}',
30
            'BA' => '[0-9]{16}',
31
            'BG' => '[A-Z]{4}[0-9]{6}[0-9A-Z]{8}',
32
            'HR' => '[0-9]{17}',
33
            'CY' => '[0-9]{8}[0-9A-Z]{16}',
34
            'CZ' => '[0-9]{20}',
35
            'DK' => '[0-9]{14}',
36
            'EE' => '[0-9]{16}',
37
            'FO' => '[0-9]{14}',
38
            'FI' => '[0-9]{14}',
39
            'FR' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}',
40
            'PF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Polynesia
41
            'TF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Southern Territories
42
            'GP' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Guadeloupe
43
            'MQ' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Martinique
44
            'YT' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Mayotte
45
            'NC' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // New Caledonia
46
            'RE' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Reunion
47
            'BL' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Saint Barthelemy
48
            'MF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // French Saint Martin
49
            'PM' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // Saint Pierre et Miquelon
50
            'WF' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}', // Wallis and Futuna Islands
51
            'GE' => '[0-9A-Z]{2}[0-9]{16}',
52
            'DE' => '[0-9]{18}',
53
            'GI' => '[A-Z]{4}[0-9A-Z]{15}',
54
            'GR' => '[0-9]{7}[0-9A-Z]{16}',
55
            'GL' => '[0-9]{14}',
56
            'HU' => '[0-9]{24}',
57
            'IS' => '[0-9]{22}',
58
            'IE' => '[0-9A-Z]{4}[0-9]{14}',
59
            'IL' => '[0-9]{19}',
60
            'IT' => '[A-Z][0-9]{10}[0-9A-Z]{12}',
61
            'KZ' => '[0-9]{3}[0-9A-Z]{3}[0-9]{10}',
62
            'KW' => '[A-Z]{4}[0-9]{22}',
63
            'LV' => '[A-Z]{4}[0-9A-Z]{13}',
64
            'LB' => '[0-9]{4}[0-9A-Z]{20}',
65
            'LI' => '[0-9]{5}[0-9A-Z]{12}',
66
            'LT' => '[0-9]{16}',
67
            'LU' => '[0-9]{3}[0-9A-Z]{13}',
68
            'MK' => '[0-9]{3}[0-9A-Z]{10}[0-9]{2}',
69
            'MT' => '[A-Z]{4}[0-9]{5}[0-9A-Z]{18}',
70
            'MR' => '[0-9]{23}',
71
            'MU' => '[A-Z]{4}[0-9]{19}[A-Z]{3}',
72
            'MC' => '[0-9]{10}[0-9A-Z]{11}[0-9]{2}',
73
            'ME' => '[0-9]{18}',
74
            'NL' => '[A-Z]{4}[0-9]{10}',
75
            'NO' => '[0-9]{11}',
76
            'PL' => '[0-9]{24}',
77
            'PT' => '[0-9]{21}',
78
            'RO' => '[A-Z]{4}[0-9A-Z]{16}',
79
            'SM' => '[A-Z][0-9]{10}[0-9A-Z]{12}',
80
            'SA' => '[0-9]{2}[0-9A-Z]{18}',
81
            'RS' => '[0-9]{18}',
82
            'SK' => '[0-9]{20}',
83
            'SI' => '[0-9]{15}',
84
            'ES' => '[0-9]{20}',
85
            'SE' => '[0-9]{20}',
86
            'CH' => '[0-9]{5}[0-9A-Z]{12}',
87
            'TN' => '[0-9]{20}',
88
            'TR' => '[0-9]{5}[0-9A-Z]{17}',
89
            'AE' => '[0-9]{19}',
90
            'GB' => '[A-Z]{4}[0-9]{14}',
91
            'CI' => '[0-9A-Z]{2}[0-9]{22}',
92
        );
93
        // Min length check
94
        if (mb_strlen($iban) < 15) {
95
            return false;
96
        }
97
        // Fetch country code from IBAN
98
        $ctr = substr($iban, 0, 2);
99
        if (isset($rules[$ctr]) === false) {
100
            return false;
101
        }
102
        // Fetch country validation rule
103
        $check = substr($iban, 4);
104
        if (preg_match('~^'.$rules[$ctr].'$~', $check) !== 1) {
105
            return false;
106
        }
107
        // Fetch needed string for validation
108
        $check = $check.substr($iban, 0, 4);
109
        // Replace characters by decimal values
110
        $check = str_replace(
111
            array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
112
            array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
113
            $check
114
        );
115
116
        // Final check
117
        return bcmod($check, 97) === '1';
118
    }
119
}
120