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::verify()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
crap 5.0073
rs 9.4888
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