Integer::normalize()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 8.6737
cc 6
eloc 15
nc 7
nop 1
crap 6
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