Passed
Push — master ( 4cc6c2...176af1 )
by Alexander
04:16
created

Str   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 35.82 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 82.05%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 24
loc 67
ccs 32
cts 39
cp 0.8205
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateConfig() 15 15 3
A initConfig() 0 7 1
D normalize() 9 30 10

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 string value
7
 */
8
class Str extends Property
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13 186 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
        /** @noinspection DegradedSwitchInspection */
16
        switch ($name) {
17 186
            case 'maxlength':
18 185
                if ($value !== null) {
19 2
                    $value = max((int)$value, 1);
20 2
                }
21 185
                break;
22 186
            default:
23 186
                return parent::validateConfig($name, $value);
24
                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...
25 186
        }
26 185
        return true;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function initConfig()
33
    {
34
        parent::initConfig();
35
        $this->mergeConfig([
36
            'maxlength' => null, // String length limitation
37
        ]);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     * @throws \RuntimeException
43
     */
44 186
    protected function normalize(&$value)
45
    {
46 186
        if (!parent::normalize($value)) {
47
            return false;
48
        }
49 186
        if ($value === null) {
50 125
            return true;
51
        }
52 185
        if (!is_scalar($value)) {
53 4
            if (is_array($value)) {
54 1
                return false;
55
            }
56
57 3 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...
58 3
                if (method_exists($value, 'toString')) {
59 1
                    $value = $value->toString();
60 3
                } elseif (method_exists($value, '__toString')) {
61 1
                    $value = (string)$value;
62 1
                } else {
63 1
                    return false;
64
                }
65 2
            }
66 2
        }
67 185
        $value = (string)$value;
68 185
        $maxlength = $this->getConfig('maxlength');
69 185
        if (($maxlength !== null) && (strlen($value) > $maxlength)) {
70 2
            $value = substr($value, 0, $maxlength);
71 2
        }
72 185
        return true;
73
    }
74
}
75