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.
Passed
Push — master ( 9239bb...931dc0 )
by Yunus Emre
03:44
created

TCKimlikNo   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 97.5%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 38
c 3
b 1
f 0
dl 0
loc 99
ccs 39
cts 40
cp 0.975
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 26 5
A toUppercaseTr() 0 7 1
B validate() 0 37 7
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
        $oddDigitsSum = $tcKimlikNo[0] + $tcKimlikNo[2] + $tcKimlikNo[4] + $tcKimlikNo[6] + $tcKimlikNo[8];
78 8
        $evenDigitsSum = $tcKimlikNo[1] + $tcKimlikNo[3] + $tcKimlikNo[5] + $tcKimlikNo[7];
79 8
        $digit10 = ($oddDigitsSum * 7 - $evenDigitsSum) % 10;
80 8
        $digit11 = ($oddDigitsSum + $evenDigitsSum + $tcKimlikNo[9]) % 10;
81
82 8
        if ($digit10 != $tcKimlikNo[9]) {
83 3
            return false;
84
        }
85
86 7
        if ($digit11 != $tcKimlikNo[10]) {
87
            return false;
88
        }
89
90 7
        return true;
91
    }
92
93
    /**
94
     * Prepares, trims, and makes uppercase names.
95
     *
96
     * @param $name
97
     * @return bool|false|mixed|string|string[]|null
98
     */
99 8
    private static function toUppercaseTr($name)
100
    {
101 8
        return mb_strtoupper(
102 8
            str_replace(
103 8
                ['ç', 'ğ', 'ı', 'ö', 'ş', 'ü', 'i'],
104 8
                ['Ç', 'Ğ', 'I', 'Ö', 'Ş', 'Ü', 'İ'],
105 8
                $name
106
            )
107
        );
108
    }
109
}
110