IniTreeSerializer::deserialize()   C
last analyzed

Complexity

Conditions 11
Paths 13

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 11.004

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 30
cts 31
cp 0.9677
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 27
nc 13
nop 1
crap 11.004

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\TreeSerializer\Types;
4
5
use ConfigToken\TreeSerializer\Exception\TreeSerializerSyntaxException;
6
7
8
class IniTreeSerializer extends AbstractTreeSerializer
9
{
10
    /**
11
     * @codeCoverageIgnore
12
     * @return string
13
     */
14
    public static function getContentType()
15
    {
16
        return 'zz-application/zz-winassoc-ini';
17
    }
18
19
    /**
20
     * @codeCoverageIgnore
21
     * @return string
22
     */
23
    public static function getFileExtension()
24
    {
25
        return 'ini';
26
    }
27
28 1
    protected static function recursiveSerialize($data, $namespace = null)
29
    {
30 1
        $result = array();
31 1
        $resultValues = array();
32 1
        foreach ($data as $key => $value) {
33 1
            if (!is_array($value)) {
34 1
                $resultValues[] = sprintf('%s=%s', $key, $value);
35 1
            }
36 1
        }
37 1
        if (!empty($namespace)) {
38 1
            $result[] = sprintf('[%s]', implode(':', $namespace));
39 1
        }
40 1
        if (!empty($resultValues)) {
41 1
            $result[] = implode("\n", $resultValues);
42 1
        } else if (!empty($namespace)) {
43 1
            $result[] = '';
44 1
        }
45 1
        unset($resultValues);
46 1
        foreach ($data as $key => $value) {
47 1
            if (!is_array($value)) {
48 1
                continue;
49
            }
50 1
            if (!isset($namespace)) {
51 1
                $namespace = array();
52 1
            }
53 1
            $namespace[] = $key;
54 1
            if (count($result) > 0) {
55 1
                $result[] = '';
56 1
            }
57 1
            $result[] = static::recursiveSerialize($value, $namespace);
58 1
            array_pop($namespace);
59 1
        }
60 1
        return implode("\n", $result);
61
    }
62
63
    /**
64
     * @covers IniTreeSerializer::recursiveSerialize
65
     * @param $data
66
     * @return string
67
     */
68 1
    public static function serialize($data)
69
    {
70 1
        return static::recursiveSerialize($data);
71
    }
72
73 4
    public static function deserialize($string)
74
    {
75 4
        $result = array();
76
        try {
77 4
            $ini = parse_ini_string($string . "\n", true, INI_SCANNER_RAW);
78 4
        } catch (\Exception $e) {
79 2
            throw new TreeSerializerSyntaxException(sprintf('Unable to parse INI string (json: %s): %s.', json_encode($string), $e->getMessage()));
80
        }
81 2
        if (($ini === false) || (empty($ini) && (strlen(trim($string)) > 0))) {
82
            throw new TreeSerializerSyntaxException(sprintf('Unable to parse INI string (json: %s).', json_encode($string)));
83
        }
84 2
        foreach ($ini as $key => $value) {
85 2
            if (is_array($value)) {
86 2
                $namespace = explode(':', $key);
87 2
                $resultPtr = &$result;
88 2
                foreach ($namespace as $namespaceLevel) {
89 2
                    if (!isset($resultPtr[$namespaceLevel])) {
90 2
                        $resultPtr[$namespaceLevel] = array();
91 2
                    }
92 2
                    $resultPtr = &$resultPtr[$namespaceLevel];
93 2
                }
94 2
                if (!is_array($resultPtr)) {
95 1
                    throw new TreeSerializerSyntaxException(
96 1
                        sprintf(
97 1
                            'Namespace [%s] overlaps with value.',
98
                            $key
99 1
                        )
100 1
                    );
101
                }
102 2
                foreach ($value as $nsKey => $nsValue) {
103 2
                    $resultPtr[$nsKey] = $nsValue;
104 2
                }
105 2
                unset($resultPtr);
106 2
                continue;
107
            }
108 1
            $result[$key] = $value;
109 1
        }
110 1
        return $result;
111
    }
112
}