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::validate()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
c 1
b 0
f 0
nc 12
nop 5
dl 0
loc 37
ccs 19
cts 19
cp 1
crap 7
rs 8.8333
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