Completed
Pull Request — master (#21)
by Antonio Oertel
02:53 queued 22s
created

Cns::defineCalculator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Brazanation\Documents;
4
5
use Brazanation\Documents\Cns\CnsCalculator;
6
use Brazanation\Documents\Cns\Temporary;
7
8
final class Cns extends AbstractDocument
9
{
10
    const LENGTH = 15;
11
12
    const LABEL = 'CNS';
13
14
    const REGEX = '/^([\d]{3})([\d]{4})([\d]{4})([\d]{4})$/';
15
16
    const FORMAT = '$1 $2 $3 $4';
17
18
    const DIGIT_COUNT = 1;
19
20
    /**
21
     * @var DigitCalculable
22
     */
23
    private $calculator;
24
25
    /**
26
     * CNS constructor.
27
     *
28
     * @param string $number
29
     */
30 15
    public function __construct($number)
31
    {
32 15
        $number = preg_replace('/\D/', '', $number);
33 15
        $this->defineCalculator($number);
34 15
        parent::__construct($number, self::LENGTH, self::DIGIT_COUNT, self::LABEL);
35 9
    }
36
37 15
    public function defineCalculator($number)
38
    {
39 15
        $this->calculator = new CnsCalculator();
40 15
        if (in_array(substr($number, 0, 1), [7, 8, 9])) {
41 1
            $this->calculator = new Temporary($number);
0 ignored issues
show
Unused Code introduced by
The call to Temporary::__construct() has too many arguments starting with $number.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
        }
43 15
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function format()
49
    {
50 1
        return preg_replace(self::REGEX, self::FORMAT, "{$this}");
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 11
    public function calculateDigit($baseNumber)
57
    {
58 11
        $digit = $this->calculator->calculateDigit($baseNumber);
59
60 11
        return "{$digit}";
61
    }
62
}
63