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

Cns   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 57
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createFromString() 0 4 1
A format() 0 4 1
A calculateDigit() 0 12 2
1
<?php
2
3
namespace Brazanation\Documents;
4
5
use Brazanation\Documents\Cns\CnsCalculator;
6
use Brazanation\Documents\Cns\TemporaryCalculator;
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 NUMBER_OF_DIGITS = 1;
19
20
    /**
21
     * CNS constructor.
22
     *
23
     * @param string $number
24
     */
25 30
    public function __construct($number)
26
    {
27 30
        $number = preg_replace('/\D/', '', $number);
28 30
        parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
29 18
    }
30
31 15
    public static function createFromString($number)
32
    {
33 15
        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...
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function format()
40
    {
41 2
        return preg_replace(self::REGEX, self::FORMAT, "{$this}");
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * Based on given number, it will decide what kind of calculator will use.
48
     *
49
     * For numbers starting with 7, 8 or 9 will use TemporaryCalculator,
50
     * otherwise CnsCalculator.
51
     */
52 22
    public function calculateDigit($baseNumber)
53
    {
54 22
        $calculator = new CnsCalculator();
55
56 22
        if (in_array(substr($baseNumber, 0, 1), [7, 8, 9])) {
57 2
            $calculator = new TemporaryCalculator();
58
        }
59
60 22
        $digit = $calculator->calculateDigit($baseNumber);
61
62 22
        return "{$digit}";
63
    }
64
}
65