Passed
Pull Request — master (#42)
by Antonio Oertel
02:24
created

Voter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 108
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createFromString() 0 4 1
A getSection() 0 4 1
A getZone() 0 4 1
A format() 0 4 1
A calculateDigit() 0 7 1
A calculateFirstDigit() 0 9 1
A calculateSecondDigit() 0 9 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
use Brazanation\Documents\Exception\InvalidDocument;
6
7
final class Voter extends AbstractDocument
8
{
9
    const LENGTH = 12;
10
11
    const LABEL = 'Voter';
12
13
    const NUMBER_OF_DIGITS = 2;
14
15
    private $section;
16
17
    private $zone;
18
19
    /**
20
     * Voter constructor.
21
     *
22
     * @param string $number
23
     * @param string $section [optional]
24
     * @param string $zone    [optional]
25
     */
26 26
    public function __construct($number, $section = null, $zone = null)
27
    {
28 26
        $number = preg_replace('/\D/', '', $number);
29 26
        parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
30
31 14
        $this->section = str_pad($section, 4, '0', STR_PAD_LEFT);
32 14
        $this->zone = str_pad($zone, 3, '0', STR_PAD_LEFT);
33 14
    }
34
35 13
    public static function createFromString($number, $section = null, $zone = null)
36
    {
37 13
        return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (tryCreateFromString() instead of createFromString()). Are you sure this is correct? If so, you might want to change this to $this->tryCreateFromString().

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...
38
    }
39
40
    /**
41
     * Gets section.
42
     *
43
     * @return string Returns section.
44
     */
45
    public function getSection()
46
    {
47
        return $this->section;
48
    }
49
50
    /**
51
     * Gets zone.
52
     *
53
     * @return string Returns zone.
54
     */
55
    public function getZone()
56
    {
57
        return $this->zone;
58
    }
59
60
    /**
61
     * Voter does not has a specific format.
62
     *
63
     * @return string Returns only numbers.
64
     */
65 2
    public function format()
66
    {
67 2
        return "{$this}";
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 18
    public function calculateDigit($baseNumber)
74
    {
75 18
        $firstDigit = $this->calculateFirstDigit($baseNumber);
76 18
        $secondDigit = $this->calculateSecondDigit("{$baseNumber}{$firstDigit}");
77
78 18
        return "{$firstDigit}{$secondDigit}";
79
    }
80
81
    /**
82
     * Calculate check digit from base number.
83
     *
84
     * @param string $baseNumber Base numeric section to be calculate your digit.
85
     *
86
     * @return string Returns the checker digit.
87
     */
88 18
    private function calculateFirstDigit($baseNumber)
89
    {
90 18
        $calculator = new DigitCalculator(substr($baseNumber, 0, -2));
91 18
        $calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2]);
92 18
        $calculator->withModule(DigitCalculator::MODULE_11);
93 18
        $digit = $calculator->calculate();
94
95 18
        return "{$digit}";
96
    }
97
98
    /**
99
     * Calculate check digit from base number.
100
     *
101
     * @param string $baseNumber Base numeric section to be calculate your digit.
102
     *
103
     * @return string Returns the checker digit.
104
     */
105 18
    private function calculateSecondDigit($baseNumber)
106
    {
107 18
        $calculator = new DigitCalculator(substr($baseNumber, -3));
108 18
        $calculator->withMultipliers([9, 8, 7]);
109 18
        $calculator->withModule(DigitCalculator::MODULE_11);
110 18
        $digit = $calculator->calculate();
111
112 18
        return "{$digit}";
113
    }
114
}
115