Integer::validateConfig()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 14
ccs 11
cts 11
cp 1
rs 9.2
c 1
b 0
f 0
cc 4
eloc 10
nc 5
nop 2
crap 4
1
<?php
2
3
namespace Flying\Struct\Property;
4
5
/**
6
 * Structure property with integer value
7
 */
8
class Integer extends Property
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13 198 View Code Duplication
    public function validateConfig($name, &$value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
14
    {
15
        switch ($name) {
16 198
            case 'min':
17 198
            case 'max':
18 197
                if ($value !== null) {
19 139
                    $value = (int)$value;
20 139
                }
21 197
                break;
22 198
            default:
23 198
                return parent::validateConfig($name, $value);
24 198
        }
25 197
        return true;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function initConfig()
32
    {
33
        parent::initConfig();
34
        $this->mergeConfig([
35
            'min' => null, // Minimum allowed value
36
            'max' => null, // Maximum allowed value
37
        ]);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     * @throws \RuntimeException
43
     */
44 198
    protected function normalize(&$value)
45
    {
46 198
        if (!parent::normalize($value)) {
47 4
            return false;
48
        }
49 198
        if ($value === null) {
50 35
            return true;
51
        }
52 197
        if (!is_scalar($value)) {
53 2
            return false;
54
        }
55 197
        $value = (int)$value;
56 197
        $min = $this->getConfig('min');
57 197
        if ($min !== null) {
58 139
            $value = max($value, $min);
59 139
        }
60 197
        $max = $this->getConfig('max');
61 197
        if ($max !== null) {
62 139
            $value = min($value, $max);
63 139
        }
64 197
        return true;
65
    }
66
}
67