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

Cns   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 55
ccs 15
cts 15
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A defineCalculator() 0 7 2
A format() 0 4 1
A calculateDigit() 0 6 1
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