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

QueryTransformer::transform()   D

Complexity

Conditions 21
Paths 28

Size

Total Lines 89

Duplication

Lines 18
Ratio 20.22 %

Code Coverage

Tests 48
CRAP Score 21.2005

Importance

Changes 0
Metric Value
dl 18
loc 89
ccs 48
cts 52
cp 0.9231
rs 4.1666
c 0
b 0
f 0
cc 21
nc 28
nop 1
crap 21.2005

How to fix   Long Method    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
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