TrTaxNumberValidation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 1
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 23 7
1
<?php
2
3
namespace XAdam;
4
5
class TrTaxNumberValidation
6
{
7
    protected static array $cache = [];
8
9
    public static function validate(string $tax_number): bool
10
    {
11
        if (isset(static::$cache[$tax_number])) {
12
            return static::$cache[$tax_number];
13
        }
14
15
        if (strlen($tax_number) !== 10) {
16
            return static::$cache[$tax_number] = false;
17
        }
18
19
        $array = array_map(function ($value) {
20
            return intval($value);
21
        }, str_split($tax_number));
22
23
        $sum = 0;
24
        for ($i = 0; $i < 9; $i++) {
25
            $mod = ($array[$i] + (9 - $i)) % 10;
26
            $pow = $mod * pow(2, (9 - $i)) % 9;
27
            $sum += ($mod !== 0 && $pow === 0) ? 9 : $pow;
28
        }
29
        $checksum = ($sum % 10 === 0) ? 0 : (10 - ($sum % 10));
30
31
        return static::$cache[$tax_number] = $checksum === $array[9];
32
    }
33
}
34