Completed
Push — master ( d5c9e6...77b81f )
by Konstantin
02:19
created

PassportHelper::explodePassport()   D

Complexity

Conditions 10
Paths 40

Size

Total Lines 32
Code Lines 20

Duplication

Lines 15
Ratio 46.88 %

Code Coverage

Tests 18
CRAP Score 10.0145

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 15
loc 32
ccs 18
cts 19
cp 0.9474
rs 4.8196
c 1
b 1
f 0
nc 40
cc 10
eloc 20
nop 0
crap 10.0145

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 extends AbstractFormHelper
7
{
8
    public $passport;
9
    public $language;
10
11
    public $pass_srs;
12
    public $pass_num;
13
14
    /**
15
     * @param $passport
16
     * @return $this
17
     */
18 4
    public function set($passport)
19
    {
20 4
        $this->passport = $passport;
21
22 4
        return $this;
23
    }
24
25
    /**
26
     * @param $language
27
     * @return $this
28
     */
29 4
    public function setLanguage($language)
30
    {
31 4
        if (in_array($language, ['ru', 'ua'])) {
32 4
            $this->language = $language;
33
        } else {
34
            $this->language = false;
35
        }
36
37 4
        return $this;
38
    }
39
40
    /**
41
     * @return $this
42
     */
43 4
    public function explodePassport()
44
    {
45 4
        mb_internal_encoding("UTF-8");
46
47 4
        $pass_length = mb_strlen($this->passport);
48 4
        $is_valid_ru = $this->language == 'ru' && $pass_length == 10;
49 4
        $is_valid_ua = $this->language == 'ua' && $pass_length == 8;
50
51 4
        if (!($is_valid_ru || $is_valid_ua)) {
52 1
            $this->passport = $this->pass_num = $this->pass_srs = false;
53 1
            return $this;
54
        }
55
56 3 View Code Duplication
        if ($is_valid_ru) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57 1
            if (parent::isInteger($this->passport)) {
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (isInteger() instead of explodePassport()). Are you sure this is correct? If so, you might want to change this to $this->isInteger().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
58 1
                $this->pass_srs = mb_substr($this->passport, 0, 4);
59 1
                $this->pass_num = mb_substr($this->passport, 4, 6);
60
            } else {
61
                $this->passport = $this->pass_num = $this->pass_srs = false;
62
            }
63
        }
64
65 3 View Code Duplication
        if ($is_valid_ua) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66 2
            $this->pass_srs = mb_substr($this->passport, 0, 2);
67 2
            $this->pass_num = mb_substr($this->passport, 2, 6);
68 2
            if (!(parent::isText($this->pass_srs) && parent::isInteger($this->pass_num))) {
2 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (isText() instead of explodePassport()). Are you sure this is correct? If so, you might want to change this to $this->isText().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (isInteger() instead of explodePassport()). Are you sure this is correct? If so, you might want to change this to $this->isInteger().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
69 1
                $this->passport = $this->pass_num = $this->pass_srs = false;
70
            }
71
        }
72
73 3
        return $this;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79 4
    public function getPassSrc()
80
    {
81 4
        return $this->pass_srs;
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87 2
    public function getPassNum()
88
    {
89 2
        return $this->pass_num;
90
    }
91
92
    /**
93
     * @return array
94
     */
95 2
    public function get()
96
    {
97 2
        return ['pass_src' => $this->pass_srs, 'pass_num' => $this->pass_num];
98
    }
99
}