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

QueryTransformer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 95
Duplicated Lines 18.95 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 0
dl 18
loc 95
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D transform() 18 89 21

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\Ldap;
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
                case '$and':
38 2
                    $part = '(&';
39 2
                    foreach ($value as $sub) {
40 2
                        $part .= self::transform($sub);
41
                    }
42
43 2
                    $result .= $part.')';
44
45 2
                break;
46 8
                case '$or':
47 3
                    $part = '(|';
48 3
                    foreach ($value as $sub) {
49 3
                        $part .= self::transform($sub);
50
                    }
51
52 3
                    $result .= $part.')';
53
54 3
                break;
55
                default:
56 8
                    $part = '';
57
58 8
                    if (is_array($value)) {
59 2
                        foreach ($value as $t => $a) {
60 2
                            if (!is_array($a) && $t[0] !== '$') {
61
                                $part .= '('.$t.'='.$a.')';
62
                            }
63
64 2
                            switch ($t) {
65 2
                                case '$gt':
66 2
                                    $part .= '('.$key.'>'.$a.')';
67
68 2
                                break;
69 2
                                case '$lt':
70 2
                                    $part .= '('.$key.'<'.$a.')';
71
72 2
                                break;
73 1
                                case '$lte':
74 1
                                    $part .= '('.$key.'<='.$a.')';
75
76 1
                                break;
77 1
                                case '$gte':
78 1
                                    $part .= '('.$key.'>='.$a.')';
79
80 2
                                break;
81
                            }
82
                        }
83
84 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...
85 1
                            $result .= '(&'.$part.')';
86
                        } else {
87 2
                            $result .= $part;
88
                        }
89
                    } else {
90 6
                        $simple .= '('.$key.'='.$value.')';
91
                    }
92
93 8
                break;
94
            }
95
        }
96
97 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...
98 6
            if (count($query) > 1) {
99 3
                $simple = '(&'.$simple.')';
100 3
                $result .= $simple;
101
            } else {
102 3
                $result .= $simple;
103
            }
104
        }
105
106 8
        return $result;
107
    }
108
}
109