CYAlgorithm::recodeValue()   B
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 25
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LeKoala\Tin\Algo;
4
5
use LeKoala\Tin\Util\StringUtil;
6
7
/**
8
 * Cyprus
9
 */
10
class CYAlgorithm extends TINAlgorithm
11
{
12
    const LENGTH = 9;
13
    const PATTERN = "[0,9]\\d{7}[A-Z]";
14
15
    public function validate(string $tin)
16
    {
17
        if (!$this->isFollowLength($tin)) {
18
            return StatusCode::INVALID_LENGTH;
19
        }
20
        if (!$this->isFollowPattern($tin)) {
21
            return StatusCode::INVALID_PATTERN;
22
        }
23
        if (!$this->isFollowRules($tin)) {
24
            return StatusCode::INVALID_SYNTAX;
25
        }
26
        return StatusCode::VALID;
27
    }
28
29
    public function isFollowLength(string $tin)
30
    {
31
        return StringUtil::isFollowLength($tin, self::LENGTH);
32
    }
33
34
    public function isFollowPattern(string $tin)
35
    {
36
        return StringUtil::isFollowPattern($tin, self::PATTERN);
37
    }
38
39
    public function isFollowRules(string $tin)
40
    {
41
        $c1 = StringUtil::digitAt($tin, 0);
42
        $c2 = StringUtil::digitAt($tin, 1);
43
        $c3 = StringUtil::digitAt($tin, 2);
44
        $c4 = StringUtil::digitAt($tin, 3);
45
        $c5 = StringUtil::digitAt($tin, 4);
46
        $c6 = StringUtil::digitAt($tin, 5);
47
        $c7 = StringUtil::digitAt($tin, 6);
48
        $c8 = StringUtil::digitAt($tin, 7);
49
        $c9 = ord($tin[8]);
50
        $evenPositionNumbersSum = $c2 + $c4 + $c6 + $c8;
51
        $recodedSum = $this->recodeValue($c1) + $this->recodeValue($c3) + $this->recodeValue($c5) + $this->recodeValue($c7);
52
        $remainderBy26 = ($evenPositionNumbersSum + $recodedSum) % 26;
53
        return $remainderBy26 + 65 == $c9;
54
    }
55
56
    /**
57
     * @param integer $x
58
     * @return integer
59
     */
60
    public function recodeValue($x)
61
    {
62
        switch ($x) {
63
            case 0:
64
                return 1;
65
            case 1:
66
                return 0;
67
            case 2:
68
                return 5;
69
            case 3:
70
                return 7;
71
            case 4:
72
                return 9;
73
            case 5:
74
                return 13;
75
            case 6:
76
                return 15;
77
            case 7:
78
                return 17;
79
            case 8:
80
                return 19;
81
            case 9:
82
                return 21;
83
            default:
84
                return -1;
85
        }
86
    }
87
}
88