Enum   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 9
loc 72
ccs 31
cts 33
cp 0.9394
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B validateConfig() 9 19 5
A initConfig() 0 7 1
A normalize() 0 10 3
A onConfigChange() 0 11 2

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 for enumerated set of values
7
 */
8
class Enum extends Property
9
{
10
    /**
11
     * Cached version of "values" configuration property
12
     *
13
     * @var array
14
     */
15
    private $values = [];
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 11
    public function validateConfig($name, &$value)
21
    {
22
        /** @noinspection DegradedSwitchInspection */
23
        switch ($name) {
24 11 View Code Duplication
            case 'values':
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...
25 11
                if (!is_array($value)) {
26 1
                    if (is_object($value) && method_exists($value, 'toArray')) {
27 1
                        $value = $value->toArray();
28 1
                    } else {
29
                        throw new \InvalidArgumentException('Only arrays are accepted as list of values for enum property');
30
                    }
31 1
                }
32 11
                break;
33 11
            default:
34 11
                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 11
        }
37 11
        return true;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    protected function initConfig()
44
    {
45 1
        parent::initConfig();
46 1
        $this->mergeConfig([
47 1
            'values' => [], // List of possible values in enum
48 1
        ]);
49 1
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 11
    protected function normalize(&$value)
55
    {
56 11
        if (!parent::normalize($value)) {
57
            return false;
58
        }
59 11
        if (!in_array($value, $this->values, true)) {
60 4
            return false;
61
        }
62 11
        return true;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 11
    protected function onConfigChange($name, $value)
69
    {
70
        /** @noinspection DegradedSwitchInspection */
71
        switch ($name) {
72 11
            case 'values':
73 11
                $this->values = $value;
74 11
                break;
75 11
            default:
76 11
                parent::onConfigChange($name, $value);
77 11
        }
78 11
    }
79
}
80