Completed
Push — master ( f0c93a...888950 )
by Andrii
05:26
created

Date   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 12
loc 42
rs 10
c 1
b 0
f 0
ccs 16
cts 17
cp 0.9412

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 4 1
B ensureValidValue() 12 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers\addons;
12
13
use DateTimeImmutable;
14
15
/**
16
 * Date addon.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
abstract class Date
21
{
22
    /**
23
     * @var DateTimeImmutable
24
     */
25
    protected $value;
26
27 5
    public function __construct($value)
28
    {
29 5
        $this->value = static::ensureValidValue($value);
30 5
    }
31
32 5
    public function getValue()
33
    {
34 5
        return $this->value;
35
    }
36
37 6
    public static function ensureValidValue($value)
38
    {
39 6
        if ($value instanceof DateTimeImmutable) {
40 4
            return $value;
41
        }
42
43 2 View Code Duplication
        if (preg_match('/^(\d{4})-(\d{2})$/', $value, $ms)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44 1
            return new DateTimeImmutable("$ms[1]-$ms[2]-01");
45
        }
46
47 2 View Code Duplication
        if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value, $ms)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48 2
            return new DateTimeImmutable("$ms[1]-$ms[2]-$ms[3]");
49
        }
50
51 1 View Code Duplication
        if (preg_match('/^(\d{2})\.(\d{4})$/', $value, $ms)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 1
            return new DateTimeImmutable("$ms[2]-$ms[1]-01");
53
        }
54
55 1 View Code Duplication
        if (preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/', $value, $ms)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56 1
            return new DateTimeImmutable("$ms[3]-$ms[2]-$ms[1]");
57
        }
58
59
        throw new \Exception('invalid date given');
60
    }
61
}
62