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

Date::ensureValidValue()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 12
Ratio 50 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 24
rs 8.5125
c 1
b 0
f 0
ccs 11
cts 12
cp 0.9167
cc 6
eloc 12
nc 6
nop 1
crap 6.0208
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