Passed
Branch master (0e9587)
by Antonio Oertel
02:20
created

Cnh::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Cnh extends AbstractDocument
6
{
7
    const LENGTH = 11;
8
9
    const LABEL = 'CNH';
10
11
    const REGEX = '/^([\d]{3})([\d]{3})([\d]{4})([\d]{1})$/';
12
13
    const NUMBER_OF_DIGITS = 2;
14
15
    /**
16
     * Cnh constructor.
17
     *
18
     * @param string $cnh Only accept numbers
19
     */
20 18
    public function __construct($cnh)
21
    {
22 18
        $cnh = preg_replace('/\D/', '', $cnh);
23 18
        parent::__construct($cnh, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
24 6
    }
25
26 9
    public static function createFromString($number)
27
    {
28 9
        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...
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 8
    public function calculateDigit($baseNumber)
35
    {
36 8
        $firstDigit = $this->calculateFirstDigit($baseNumber);
37 8
        $secondDigit = $this->calculateSecondDigit($baseNumber);
38
39 8
        return "{$firstDigit}{$secondDigit}";
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function format()
46
    {
47 2
        return "{$this}";
48
    }
49
50
    /**
51
     * Calculate check digit from base number.
52
     *
53
     * @param string $baseNumber Base numeric section to be calculate your digit.
54
     *
55
     * @return string Returns a calculated checker digit.
56
     */
57 8
    private function calculateFirstDigit($baseNumber)
58
    {
59 8
        $calculator = new DigitCalculator($baseNumber);
60 8
        $calculator->withMultipliersInterval(1, 9);
61 8
        $calculator->replaceWhen('0', 10, 11);
62 8
        $calculator->withModule(DigitCalculator::MODULE_11);
63 8
        $firstDigit = $calculator->calculate();
64
65 8
        return "{$firstDigit}";
66
    }
67
68
    /**
69
     * Calculate check digit from base number.
70
     *
71
     * @param string $baseNumber Base numeric section to be calculate your digit.
72
     *
73
     * @return string Returns a calculated checker digit.
74
     */
75 8
    private function calculateSecondDigit($baseNumber)
76
    {
77 8
        $calculator = new DigitCalculator($baseNumber);
78 8
        $calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2, 1]);
79 8
        $calculator->replaceWhen('0', 10, 11);
80 8
        $calculator->withModule(DigitCalculator::MODULE_11);
81 8
        $secondDigit = $calculator->calculate();
82
83 8
        return "{$secondDigit}";
84
    }
85
}
86