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

Renavam::createFromString()   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
nc 1
cc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Renavam extends AbstractDocument
6
{
7
    const LENGTH = 11;
8
9
    const LABEL = 'Renavam';
10
11
    const REGEX = '/^([\d]{4})([\d]{6})([\d]{1})$/';
12
13
    const NUMBER_OF_DIGITS = 1;
14
15
    /**
16
     * Renavam constructor.
17
     *
18
     * @param string $renavam Only accept numbers
19
     */
20 18
    public function __construct($renavam)
21
    {
22 18
        $renavam = preg_replace('/\D/', '', $renavam);
23 18
        $renavam = $this->padNumber($renavam);
24 18
        parent::__construct($renavam, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
25 8
    }
26
27 9
    public static function createFromString($number)
28
    {
29 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...
30
    }
31
32
    /**
33
     * Pad left a number to length(11) with 0(ZERO)
34
     *
35
     * @param string $number
36
     *
37
     * @return string
38
     */
39 18
    private function padNumber($number)
40
    {
41 18
        if (empty($number)) {
42 4
            return '';
43
        }
44
45 14
        return str_pad($number, self::LENGTH, 0, STR_PAD_LEFT);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function format()
52
    {
53 2
        return preg_replace(self::REGEX, '$1.$2-$3', "{$this}");
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 10
    public function calculateDigit($baseNumber)
60
    {
61 10
        $calculator = new DigitCalculator($baseNumber);
62
        //$calculator->withMultipliers([3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63 10
        $calculator->withMultipliers([2, 3, 4, 5, 6, 7, 8, 9, 2, 3]);
64 10
        $calculator->replaceWhen('0', 10);
65 10
        $calculator->withModule(DigitCalculator::MODULE_11);
66 10
        $calculator->multiplySumBy(10);
67 10
        $digit = $calculator->calculate();
68
69 10
        return "{$digit}";
70
    }
71
}
72