DateTime   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 82
Duplicated Lines 10.98 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.92%

Importance

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

3 Methods

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