Passed
Push — master ( 4cc6c2...176af1 )
by Alexander
04:16
created

DateTime   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 83
Duplicated Lines 10.84 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.92%

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 1
dl 9
loc 83
ccs 47
cts 49
cp 0.9592
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
D validateConfig() 0 25 9
D normalize() 9 35 9
A initConfig() 0 7 1

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
namespace Flying\Struct\Property;
4
5
/**
6
 * Structure property with DateTime value
7
 */
8
class DateTime extends Property
9
{
10
    /**
11
     * {@inheritdoc}
12
     * @throws \RuntimeException
13
     */
14 21
    public function validateConfig($name, &$value)
15
    {
16
        switch ($name) {
17 21
            case 'format':
18 11
                if ((!is_string($value)) || ($value === '')) {
19 10
                    $value = null;
20 10
                }
21 11
                break;
22 21
            case 'default':
23 21
                if ($value === null) {
24 3
                    if (!$this->getConfig('nullable')) {
25 1
                        $value = new \DateTime();
26 1
                    }
27 21
                } elseif (is_string($value)) {
28 15
                    $value = new \DateTime($value);
29 18
                } elseif (!($value instanceof \DateTime)) {
30
                    return false;
31
                }
32 21
                break;
33 21
            default:
34 21
                return parent::validateConfig($name, $value);
35
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
36 21
        }
37 21
        return true;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     * @throws \RuntimeException
43
     */
44 21
    protected function normalize(&$value)
45
    {
46 21
        if (!parent::normalize($value)) {
47
            return false;
48
        }
49 21
        if ($value === null) {
50 2
            return true;
51
        }
52
53 19
        if ($value instanceof \DateTime) {
54 12
            return true;
55
        }
56
57 14 View Code Duplication
        if (is_object($value)) {
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...
58 3
            if (method_exists($value, 'toString')) {
59 1
                $value = $value->toString();
60 3
            } elseif (method_exists($value, '__toString')) {
61 1
                $value = (string)$value;
62 1
            } else {
63 1
                return false;
64
            }
65 2
        }
66
67 13
        if (!is_string($value)) {
68 4
            return false;
69
        }
70
71 9
        $format = $this->getConfig('format');
72 9
        if ($format) {
73 1
            $value = \DateTime::createFromFormat($format, $value);
74 1
        } else {
75 8
            $value = new \DateTime($value);
76
        }
77 8
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    protected function initConfig()
84
    {
85 1
        parent::initConfig();
86 1
        $this->mergeConfig([
87 1
            'format' => null, // Default format of date/time information to use for constructing DateTime object from string
88 1
        ]);
89 1
    }
90
}
91