Completed
Push — master ( e54ccb...52bc20 )
by Konstantin
03:38
created

PassportHelper::explodePassport()   D

Complexity

Conditions 10
Paths 42

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 28
c 2
b 2
f 0
ccs 17
cts 17
cp 1
rs 4.8196
nc 42
cc 10
eloc 17
nop 0
crap 10

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 FormHelper;
4
5
/**
6
 * Class PassportHelper
7
 * @package FormHelper
8
 */
9
class PassportHelper extends AbstractFormHelper
10
{
11
    use ValidationTrait;
12
13
    public $passport;
14
    public $language;
15
16
    public $pass_srs;
17
    public $pass_num;
18
19
    /**
20
     * @param $passport
21
     * @return $this
22
     */
23 4
    public function set($passport)
24
    {
25 4
        $this->passport = $passport;
26
27 4
        return $this;
28
    }
29
30
    /**
31
     * @param $language
32
     * @return $this
33
     */
34 4
    public function setLanguage($language)
35
    {
36 4
        if (in_array($language, ['ru', 'ua'])) {
37 4
            $this->language = $language;
38
        } else {
39
            $this->language = false;
40
        }
41
42 4
        return $this;
43
    }
44
45
    /**
46
     * @return $this
47
     */
48 4
    public function explodePassport()
49
    {
50 4
        mb_internal_encoding("UTF-8");
51
52 4
        $pass_length = mb_strlen($this->passport);
53 4
        $is_valid_ru = $this->language == 'ru' && $pass_length == 10 && $this->isInteger($this->passport);
54 4
        $is_valid_ua = $this->language == 'ua' && $pass_length == 8;
55
56 4
        if (!($is_valid_ru || $is_valid_ua)) {
57 1
            $this->passport = $this->pass_num = $this->pass_srs = false;
58 1
            return $this;
59
        }
60
61 3
        if ($is_valid_ru) {
62 1
            $this->pass_srs = mb_substr($this->passport, 0, 4);
63 1
            $this->pass_num = mb_substr($this->passport, 4, 6);
64
        }
65
66 3
        if ($is_valid_ua) {
67 2
            $this->pass_srs = mb_substr($this->passport, 0, 2);
68 2
            $this->pass_num = mb_substr($this->passport, 2, 6);
69 2
            if (!($this->isText($this->pass_srs) && $this->isInteger($this->pass_num))) {
70 1
                $this->passport = $this->pass_num = $this->pass_srs = false;
71
            }
72
        }
73
74 3
        return $this;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80 4
    public function getPassSrc()
81
    {
82 4
        return $this->pass_srs;
83
    }
84
85
    /**
86
     * @return mixed
87
     */
88 2
    public function getPassNum()
89
    {
90 2
        return $this->pass_num;
91
    }
92
93
    /**
94
     * @return array
95
     */
96 2
    public function get()
97
    {
98 2
        return ['pass_src' => $this->pass_srs, 'pass_num' => $this->pass_num];
99
    }
100
}