GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 098477...997a41 )
by Yunus Emre
18:44 queued 15:46
created

TCKimlikNo   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 97.67%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 40
c 3
b 1
f 0
dl 0
loc 113
ccs 42
cts 43
cp 0.9767
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 37 7
A verify() 0 23 5
A generateChecksumDigits() 0 9 1
A toUppercaseTr() 0 7 1
1
<?php
2
3
namespace Deligoez\TCKimlikNo;
4
5
use SoapClient;
6
7
class TCKimlikNo
8
{
9
    /**
10
     * Validates Citizenship Number using nvi.gov.tr.
11
     *
12
     * @param  string  $tcKimlikNo
13
     * @param  string  $name
14
     * @param  string  $surname
15
     * @param  string  $birthYear
16
     * @param  bool  $autoUppercase
17
     * @return bool
18
     * @throws \SoapFault
19
     */
20 5
    public static function validate(
21
        $tcKimlikNo,
22
        string $name,
23
        string $surname,
24
        $birthYear,
25
        bool $autoUppercase = true
26
    ): bool {
27 5
        if ($autoUppercase) {
28 5
            $name = self::toUppercaseTr($name);
29 5
            $surname = self::toUppercaseTr($surname);
30
        }
31
32 5
        if (! preg_match('/^[A-Z ÇĞÖŞÜİ]{1,}$/', self::toUppercaseTr($name))) {
33 1
            return false;
34
        }
35
36 5
        if (! preg_match('/^[A-Z ÇĞÖŞÜİ]{1,}$/', self::toUppercaseTr($surname))) {
37 1
            return false;
38
        }
39
40 5
        if (! preg_match('/^[0-9]{4}$/', $birthYear)) {
41 1
            return false;
42
        }
43
44 4
        if (! self::verify($tcKimlikNo)) {
45 1
            return false;
46
        }
47
48 3
        $response = (new SoapClient('https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx?WSDL'))
49 3
            ->TCKimlikNoDogrula([
50 3
                'TCKimlikNo' => intval($tcKimlikNo),
51 3
                'Ad'         => trim($name),
52 3
                'Soyad'      => trim($surname),
53 3
                'DogumYili'  => intval($birthYear),
54
            ]);
55
56 3
        return $response->TCKimlikNoDogrulaResult ? true : false;
57
    }
58
59
    /**
60
     * Verify Citizenship Number According to it's Algorithm.
61
     *
62
     * @param  string  $tcKimlikNo
63
     * @return bool
64
     */
65 13
    public static function verify($tcKimlikNo): bool
66
    {
67 13
        $tcKimlikNo = strval($tcKimlikNo);
68
69 13
        if (strlen(strval($tcKimlikNo)) != 11) {
70 3
            return false;
71
        }
72
73 10
        if (! preg_match('/^[1-9]{1}[0-9]{9}[0,2,4,6,8]{1}$/', $tcKimlikNo)) {
74 2
            return false;
75
        }
76
77 8
        $checksumDigits = static::generateChecksumDigits($tcKimlikNo);
78
79 8
        if ($checksumDigits[0] != $tcKimlikNo[9]) {
80 3
            return false;
81
        }
82
83 7
        if ($checksumDigits[1] != $tcKimlikNo[10]) {
84
            return false;
85
        }
86
87 7
        return true;
88
    }
89
90
    /**
91
     * Generates Checksum Digits from the first 9 Digits.
92
     *
93
     * @param $tcKimlikNo
94
     * @return string
95
     */
96 9
    public static function generateChecksumDigits($tcKimlikNo): string
97
    {
98 9
        $oddDigitsSum = $tcKimlikNo[0] + $tcKimlikNo[2] + $tcKimlikNo[4] + $tcKimlikNo[6] + $tcKimlikNo[8];
99 9
        $evenDigitsSum = $tcKimlikNo[1] + $tcKimlikNo[3] + $tcKimlikNo[5] + $tcKimlikNo[7];
100
101 9
        $digit10 = ($oddDigitsSum * 7 - $evenDigitsSum) % 10;
102 9
        $digit11 = ($oddDigitsSum + $evenDigitsSum + $digit10) % 10;
103
104 9
        return $digit10.$digit11;
105
    }
106
107
    /**
108
     * Prepares, trims, and makes uppercase names.
109
     *
110
     * @param $name
111
     * @return bool|false|mixed|string|string[]|null
112
     */
113 8
    private static function toUppercaseTr(string $name): string
114
    {
115 8
        return mb_strtoupper(
116 8
            str_replace(
117 8
                ['ç', 'ğ', 'ı', 'ö', 'ş', 'ü', 'i'],
118 8
                ['Ç', 'Ğ', 'I', 'Ö', 'Ş', 'Ü', 'İ'],
119 8
                $name
120
            )
121
        );
122
    }
123
}
124