Str::normalize()   D
last analyzed

Complexity

Conditions 10
Paths 12

Size

Total Lines 30
Code Lines 20

Duplication

Lines 9
Ratio 30 %

Code Coverage

Tests 22
CRAP Score 10.0082

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 30
ccs 22
cts 23
cp 0.9565
rs 4.8196
cc 10
eloc 20
nc 12
nop 1
crap 10.0082

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 186
        }
25 185
        return true;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function initConfig()
32
    {
33
        parent::initConfig();
34
        $this->mergeConfig([
35
            'maxlength' => null, // String length limitation
36
        ]);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     * @throws \RuntimeException
42
     */
43 186
    protected function normalize(&$value)
44
    {
45 186
        if (!parent::normalize($value)) {
46
            return false;
47
        }
48 186
        if ($value === null) {
49 125
            return true;
50
        }
51 185
        if (!is_scalar($value)) {
52 4
            if (is_array($value)) {
53 1
                return false;
54
            }
55
56 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...
57 3
                if (method_exists($value, 'toString')) {
58 1
                    $value = $value->toString();
59 3
                } elseif (method_exists($value, '__toString')) {
60 1
                    $value = (string)$value;
61 1
                } else {
62 1
                    return false;
63
                }
64 2
            }
65 2
        }
66 185
        $value = (string)$value;
67 185
        $maxlength = $this->getConfig('maxlength');
68 185
        if (($maxlength !== null) && (strlen($value) > $maxlength)) {
69 2
            $value = substr($value, 0, $maxlength);
70 2
        }
71 185
        return true;
72
    }
73
}
74