Passed
Branch master (0e9587)
by Antonio Oertel
02:20
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
final class Voter extends AbstractDocument
6
{
7
    const LENGTH = 12;
8
9
    const LABEL = 'Voter';
10
11
    const NUMBER_OF_DIGITS = 2;
12
13
    private $section;
14
15
    private $zone;
16
17
    /**
18
     * Voter constructor.
19
     *
20
     * @param string $number
21
     * @param string $section [optional]
22
     * @param string $zone    [optional]
23
     */
24 26
    public function __construct($number, $section = null, $zone = null)
25
    {
26 26
        $number = preg_replace('/\D/', '', $number);
27 26
        parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
28
29 14
        $this->section = str_pad($section, 4, '0', STR_PAD_LEFT);
30 14
        $this->zone = str_pad($zone, 3, '0', STR_PAD_LEFT);
31 14
    }
32
33 13
    public static function createFromString($number, $section = null, $zone = null)
34
    {
35 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...
36
    }
37
38
    /**
39
     * Gets section.
40
     *
41
     * @return string Returns section.
42
     */
43
    public function getSection()
44
    {
45
        return $this->section;
46
    }
47
48
    /**
49
     * Gets zone.
50
     *
51
     * @return string Returns zone.
52
     */
53
    public function getZone()
54
    {
55
        return $this->zone;
56
    }
57
58
    /**
59
     * Voter does not has a specific format.
60
     *
61
     * @return string Returns only numbers.
62
     */
63 2
    public function format()
64
    {
65 2
        return "{$this}";
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 18
    public function calculateDigit($baseNumber)
72
    {
73 18
        $firstDigit = $this->calculateFirstDigit($baseNumber);
74 18
        $secondDigit = $this->calculateSecondDigit("{$baseNumber}{$firstDigit}");
75
76 18
        return "{$firstDigit}{$secondDigit}";
77
    }
78
79
    /**
80
     * Calculate check digit from base number.
81
     *
82
     * @param string $baseNumber Base numeric section to be calculate your digit.
83
     *
84
     * @return string Returns the checker digit.
85
     */
86 18
    private function calculateFirstDigit($baseNumber)
87
    {
88 18
        $calculator = new DigitCalculator(substr($baseNumber, 0, -2));
89 18
        $calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2]);
90 18
        $calculator->withModule(DigitCalculator::MODULE_11);
91 18
        $digit = $calculator->calculate();
92
93 18
        return "{$digit}";
94
    }
95
96
    /**
97
     * Calculate check digit from base number.
98
     *
99
     * @param string $baseNumber Base numeric section to be calculate your digit.
100
     *
101
     * @return string Returns the checker digit.
102
     */
103 18
    private function calculateSecondDigit($baseNumber)
104
    {
105 18
        $calculator = new DigitCalculator(substr($baseNumber, -3));
106 18
        $calculator->withMultipliers([9, 8, 7]);
107 18
        $calculator->withModule(DigitCalculator::MODULE_11);
108 18
        $digit = $calculator->calculate();
109
110 18
        return "{$digit}";
111
    }
112
}
113