Completed
Push — master ( 028356...03137d )
by
unknown
02:00
created

Parser   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 145
Duplicated Lines 57.93 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 1
cbo 0
dl 84
loc 145
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B parseRichParam() 0 24 5
D parseModifiers() 46 46 10
C parseArguments() 38 38 7
A parseElements() 0 10 4

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 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
    public function parseRichParam($param)
14
    {
15
        $interpretation = [];
16
        foreach ($this->parseElements($param) as $element) {
17
            list($fieldname, $modifiersStr) = array_pad(explode(':', $element, 2), 2, null);
18
19
            if ($modifiersStr === null) {
20
                if (!isset($interpretation[$fieldname])) {
21
                    $interpretation[$fieldname] = [];
22
                }
23
                continue;
24
            }
25
26
            $modifierArr = $this->parseModifiers($modifiersStr);
27
28
            if (isset($interpretation[$fieldname])) {
29
                $interpretation[$fieldname] = $interpretation[$fieldname] + $modifierArr;
30
            } else {
31
                $interpretation[$fieldname] = $modifierArr;
32
            }
33
        }
34
35
        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 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
        $interpretation = [];
51
        $modifierName = '';
52
        $modifierParamStr = '';
53
54
        $depth = 0;
55
        for ($i = 0; $i < strlen($modifiersStr); $i++) {
56
            switch ($modifiersStr[$i]) {
57
                case '(':
58
                    if ($depth) {
59
                        $modifierParamStr .= $modifiersStr[$i];
60
                    }
61
                    $depth++;
62
                    break;
63
64
                case ')':
65
                    $depth--;
66
                    if ($depth) {
67
                        $modifierParamStr .= $modifiersStr[$i];
68
                    }
69
                    break;
70
                case ':':
71
                    if ($depth) {
72
                        $modifierParamStr .= $modifiersStr[$i];
73
                    } else {
74
                        $interpretation[$modifierName] = $this->parseArguments($modifierParamStr);
75
                        $modifierName = '';
76
                        $modifierParamStr = '';
77
                    }
78
                    break;
79
                default:
80
                    if ($depth) {
81
                        $modifierParamStr .= $modifiersStr[$i];
82
                    } else {
83
                        $modifierName .= $modifiersStr[$i];
84
                    }
85
            }
86
        }
87
88
        if ($modifierName) {
89
            $interpretation[$modifierName] = $this->parseArguments($modifierParamStr);
90
        }
91
92
        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 View Code Duplication
    private function parseArguments($argumentsStr)
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...
107
    {
108
        $paramArr = [];
109
        $tmpStr = '';
110
111
        $depth = 0;
112
        for ($i = 0; $i < strlen($argumentsStr); $i++) {
113
            switch ($argumentsStr[$i]) {
114
                case '(':
115
                    $tmpStr .= $argumentsStr[$i];
116
                    $depth++;
117
                    break;
118
119
                case ')':
120
                    $tmpStr .= $argumentsStr[$i];
121
                    $depth--;
122
                    break;
123
124
                case '|':
125
                    if ($depth) {
126
                        $tmpStr .= $argumentsStr[$i];
127
                    } else {
128
                        $paramArr[] = $tmpStr;
129
                        $tmpStr = '';
130
                    }
131
                    break;
132
133
                default:
134
                    $tmpStr .= $argumentsStr[$i];
135
            }
136
        }
137
138
        if (strlen($tmpStr)) {
139
            $paramArr[] = $tmpStr;
140
        }
141
142
        return $paramArr;
143
    }
144
145
    private function parseElements($richParam)
146
    {
147
        if ($richParam === '' || !is_string($richParam)) {
148
            return [];
149
        }
150
151
        $fields = explode(',', $richParam);
152
153
        return is_array($fields) ? $fields : [];
154
    }
155
}