Parser::parseModifiers()   D
last analyzed

Complexity

Conditions 10
Paths 18

Size

Total Lines 46
Code Lines 33

Duplication

Lines 46
Ratio 100 %

Code Coverage

Tests 38
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 46
loc 46
ccs 38
cts 38
cp 1
rs 4.983
cc 10
eloc 33
nc 18
nop 1
crap 10

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 Macroparts\Vortex\RichParam;
4
5
/**
6
 * Created by PhpStorm.
7
 * User: daniel.jurkovic
8
 * Date: 14.06.17
9
 * Time: 18:02
10
 */
11
class Parser
12
{
13 39
    public function parseRichParam($param)
14
    {
15 39
        $interpretation = [];
16 39
        foreach ($this->parseElements($param) as $element) {
17 21
            list($fieldname, $modifiersStr) = array_pad(explode(':', $element, 2), 2, null);
18
19 21
            if ($modifiersStr === null) {
20 3
                if (!isset($interpretation[$fieldname])) {
21 3
                    $interpretation[$fieldname] = [];
22 2
                }
23 3
                continue;
24
            }
25
26 18
            $modifierArr = $this->parseModifiers($modifiersStr);
27
28 18
            if (isset($interpretation[$fieldname])) {
29 3
                $interpretation[$fieldname] = $interpretation[$fieldname] + $modifierArr;
30 2
            } else {
31 18
                $interpretation[$fieldname] = $modifierArr;
32
            }
33 26
        }
34
35 39
        return $interpretation;
36
    }
37
38
    /**
39
     * creates an array out of string in this format:
40
     *
41
     * modifierName1(modifierParam1|modifierParam2):modifierName2(modifierParam3))
42
     *
43
     * Can also handle modifier params that contain other modifier with params
44
     *
45
     * @param string $modifiersStr
46
     * @return array
47
     */
48 18 View Code Duplication
    private function parseModifiers($modifiersStr)
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...
49
    {
50 18
        $interpretation = [];
51 18
        $modifierName = '';
52 18
        $modifierParamStr = '';
53
54 18
        $depth = 0;
55 18
        for ($i = 0; $i < strlen($modifiersStr); $i++) {
56 18
            switch ($modifiersStr[$i]) {
57 18
                case '(':
58 15
                    if ($depth) {
59 9
                        $modifierParamStr .= $modifiersStr[$i];
60 6
                    }
61 15
                    $depth++;
62 15
                    break;
63
64 18
                case ')':
65 15
                    $depth--;
66 15
                    if ($depth) {
67 9
                        $modifierParamStr .= $modifiersStr[$i];
68 6
                    }
69 15
                    break;
70 18
                case ':':
71 9
                    if ($depth) {
72 9
                        $modifierParamStr .= $modifiersStr[$i];
73 6
                    } else {
74 3
                        $interpretation[$modifierName] = $this->parseArguments($modifierParamStr);
75 3
                        $modifierName = '';
76 3
                        $modifierParamStr = '';
77
                    }
78 9
                    break;
79 12
                default:
80 18
                    if ($depth) {
81 15
                        $modifierParamStr .= $modifiersStr[$i];
82 10
                    } else {
83 18
                        $modifierName .= $modifiersStr[$i];
84
                    }
85 12
            }
86 12
        }
87
88 18
        if ($modifierName) {
89 18
            $interpretation[$modifierName] = $this->parseArguments($modifierParamStr);
90 12
        }
91
92 18
        return $interpretation;
93
    }
94
95
    /**
96
     * Can make an array out of parameter string that looks like this:
97
     *
98
     * param1|param2|param3
99
     *
100
     * Can also handle params that contain other modifiers with params like this:
101
     * param1|modifier(innerParam1|innerParam2)|param3
102
     *
103
     * @param string $argumentsStr
104
     * @return array
105
     */
106 18
    private function parseArguments($argumentsStr)
107
    {
108 18
        $paramArr = [];
109 18
        $tmpStr = '';
110
111 18
        $depth = 0;
112 18
        for ($i = 0; $i < strlen($argumentsStr); $i++) {
113 15
            switch ($argumentsStr[$i]) {
114 15
                case '(':
115 9
                    $tmpStr .= $argumentsStr[$i];
116 9
                    $depth++;
117 9
                    break;
118
119 15
                case ')':
120 9
                    $tmpStr .= $argumentsStr[$i];
121 9
                    $depth--;
122 9
                    break;
123
124 15
                case '|':
125 6
                    if ($depth) {
126 6
                        $tmpStr .= $argumentsStr[$i];
127 4
                    } else {
128 6
                        $paramArr[] = $tmpStr;
129 6
                        $tmpStr = '';
130
                    }
131 6
                    break;
132
133 10
                default:
134 15
                    $tmpStr .= $argumentsStr[$i];
135 10
            }
136 10
        }
137
138 18
        if (strlen($tmpStr)) {
139 15
            $paramArr[] = $tmpStr;
140 10
        }
141
142 18
        return $paramArr;
143
    }
144
145 39
    private function parseElements($richParam)
146
    {
147 39
        if ($richParam === '' || !is_string($richParam)) {
148 18
            return [];
149
        }
150
151 21
        $fields = explode(',', $richParam);
152
153 21
        return is_array($fields) ? $fields : [];
154
    }
155
}
156