Completed
Push — master ( b64fc1...5651bd )
by Raffael
16:20 queued 08:39
created

QueryTransformer   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 121
Duplicated Lines 36.36 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 89.55%

Importance

Changes 0
Metric Value
wmc 28
lcom 0
cbo 0
dl 44
loc 121
ccs 60
cts 67
cp 0.8955
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F transform() 44 115 28

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
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Endpoint\Xml;
13
14
class QueryTransformer
15
{
16
    /**
17
     * Convert mongodb like query to ldap query.
18
     */
19 14
    public static function transform($query): string
20
    {
21 14
        $result = '';
22
23 14
        if (!is_array($query)) {
24
            return $query;
25
        }
26
27 14
        $simple = '';
28 14
        foreach ($query as $key => $value) {
29 14 View Code Duplication
            if (is_array($value) && isset($value['$and']) || isset($value['$or'])) {
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...
30
                $result .= self::transform($value);
31
32
                continue;
33
            }
34
35 14
            $key = (string) $key;
36 14
            switch ($key) {
37 14 View Code Duplication
                case '$and':
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...
38 4
                    $part = '(';
39 4
                    foreach ($value as $sub) {
40 4
                        if ($part !== '(') {
41 4
                            $part .= ' and ';
42
                        }
43
44 4
                        $part .= self::transform($sub);
45
                    }
46
47 4
                    $result .= $part.')';
48
49 4
                break;
50 14 View Code Duplication
                case '$or':
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...
51 3
                    $part = '(';
52 3
                    foreach ($value as $sub) {
53 3
                        if ($part !== '(') {
54 3
                            $part .= ' or ';
55
                        }
56
57 3
                        $part .= self::transform($sub);
58
                    }
59
60 3
                    $result .= $part.')';
61
62 3
                break;
63
                default:
64 14
                    $part = '';
65
66 14
                    if (is_array($value)) {
67 2
                        foreach ($value as $t => $a) {
68 2
                            if ($part !== '') {
69 1
                                $part .= ' and ';
70
                            }
71
72 2
                            if (!is_array($a) && $t[0] !== '$') {
73
                                $part .= "($t='$a')";
74
                            }
75
76 2
                            switch ($t) {
77 2
                                case '$gt':
78 2
                                    $part .= "($key>'$a')";
79
80 2
                                break;
81 2
                                case '$lt':
82 2
                                    $part .= "($key<'$a')";
83
84 2
                                break;
85 1
                                case '$lte':
86 1
                                    $part .= "($key<='$a')";
87
88 1
                                break;
89 1
                                case '$gte':
90 1
                                    $part .= "($key>='$a')";
91
92 1
                                break;
93 1
                                case '$ne':
94 1
                                    $part .= "($key!='$a')";
95
96 1
                                break;
97
                                case '$exists':
98
                                    if ($a) {
99
                                        $part .= "($key=*)";
100
                                    }
101
102 2
                                break;
103
                            }
104
                        }
105
106 2 View Code Duplication
                        if (count($value) > 1) {
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...
107 1
                            $result .= '('.$part.')';
108
                        } else {
109 2
                            $result .= $part;
110
                        }
111
                    } else {
112 12
                        if ($simple !== '') {
113 1
                            $simple .= ' and ';
114
                        }
115
116 12
                        $simple .= "($key='$value')";
117
                    }
118
119 14
                break;
120
            }
121
        }
122
123 14 View Code Duplication
        if (!empty($simple)) {
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...
124 12
            if (count($query) > 1) {
125 1
                $simple = '('.$simple.')';
126 1
                $result .= $simple;
127
            } else {
128 11
                $result .= $simple;
129
            }
130
        }
131
132 14
        return $result;
133
    }
134
}
135