Enum::normalize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3.0416
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