Passed
Push — master ( 3896ee...e50616 )
by Raffael
05:45
created

QueryTransformer   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 115
Duplicated Lines 38.26 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 93.65%

Importance

Changes 0
Metric Value
wmc 26
lcom 0
cbo 0
dl 44
loc 115
ccs 59
cts 63
cp 0.9365
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F transform() 44 109 26

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.io
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 8
    public static function transform($query): string
20
    {
21 8
        $result = '';
22
23 8
        if (!is_array($query)) {
24
            return $query;
25
        }
26
27 8
        $simple = '';
28 8
        foreach ($query as $key => $value) {
29 8 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 8
            $key = (string) $key;
36 8
            switch ($key) {
37 8 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 2
                    $part = '(';
39 2
                    foreach ($value as $sub) {
40 2
                        if ($part !== '(') {
41 2
                            $part .= ' and ';
42
                        }
43
44 2
                        $part .= self::transform($sub);
45
                    }
46
47 2
                    $result .= $part.')';
48
49 2
                break;
50 8 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 8
                    $part = '';
65
66 8
                    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 2
                                break;
97
                            }
98
                        }
99
100 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...
101 1
                            $result .= '('.$part.')';
102
                        } else {
103 2
                            $result .= $part;
104
                        }
105
                    } else {
106 6
                        if ($simple !== '') {
107 1
                            $simple .= ' and ';
108
                        }
109
110 6
                        $simple .= "($key='$value')";
111
                    }
112
113 8
                break;
114
            }
115
        }
116
117 8 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...
118 6
            if (count($query) > 1) {
119 1
                $simple = '('.$simple.')';
120 1
                $result .= $simple;
121
            } else {
122 5
                $result .= $simple;
123
            }
124
        }
125
126 8
        return $result;
127
    }
128
}
129