getSerializedValue()   D
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10.0512

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 23
cts 25
cp 0.92
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 25
nc 10
nop 2
crap 10.0512

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 ConfigToken\TokenResolver\ScopeValueSerializers;
4
5
use ConfigToken\TokenResolver\Exception\ScopeTokenValueSerializationException;
6
use ConfigToken\TokenResolver\ScopeTokenValueSerializerInterface;
7
8
9
class JsonScopeTokenValueSerializer implements ScopeTokenValueSerializerInterface
10
{
11
    /**
12
     * Serialize the given value from the scope to a string representation.
13
     *
14
     * @param array|string $scopeValue
15
     * @param boolean $escape
16
     * @return string
17
     * @throws ScopeTokenValueSerializationException
18
     */
19 2
    public function getSerializedValue($scopeValue, $escape = False)
20
    {
21 2
        $valueType = gettype($scopeValue);
22
23
        switch ($valueType) {
24 2
            case 'string':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
25 1
                if ($escape) {
26
                    $result = json_encode($scopeValue);
27
                    return substr($result, 1, -1); // eliminate quotes
28
                } else {
29 1
                    return '' . $scopeValue;
30
                }
31 2
            case 'boolean':
32 1
                return ($scopeValue ? 'true' : 'false');
33 2
            case 'integer':
34 1
                return '' . $scopeValue;
35 2
            case 'double':
36 1
                return sprintf('%.5f', $scopeValue);
37 2
            case 'array':
38 2
            case 'object':
39 1
                return json_encode($scopeValue);
40 2
            case 'NULL':
41 1
                return 'null';
42 1
            default:
43 1
                throw new ScopeTokenValueSerializationException(
44 1
                    sprintf(
45 1
                        'Unable to serialize value of type "%s".',
46
                        $valueType
47 1
                    )
48 1
                );
49 1
        }
50
    }
51
}