| Conditions | 21 |
| Paths | 28 |
| Total Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 48 |
| CRAP Score | 21.2005 |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | 7 | public static function transform($query): string |
|
| 20 | { |
||
| 21 | 7 | $result = ''; |
|
| 22 | |||
| 23 | 7 | if (!is_array($query)) { |
|
| 24 | return $query; |
||
| 25 | } |
||
| 26 | |||
| 27 | 7 | $simple = ''; |
|
| 28 | 7 | foreach ($query as $key => $value) { |
|
| 29 | 7 | if (is_array($value) && isset($value['$and']) || isset($value['$or'])) { |
|
| 30 | $result .= self::transform($value); |
||
| 31 | |||
| 32 | continue; |
||
| 33 | } |
||
| 34 | |||
| 35 | 7 | $key = (string) $key; |
|
| 36 | 7 | switch ($key) { |
|
| 37 | 7 | 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 | 7 | 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 | 7 | $part = ''; |
|
| 57 | |||
| 58 | 7 | 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 | if (count($value) > 1) { |
|
| 85 | 1 | $result .= '(&'.$part.')'; |
|
| 86 | } else { |
||
| 87 | 2 | $result .= $part; |
|
| 88 | } |
||
| 89 | } else { |
||
| 90 | 5 | $simple .= '('.$key.'='.$value.')'; |
|
| 91 | } |
||
| 92 | |||
| 93 | 7 | break; |
|
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | 7 | if (!empty($simple)) { |
|
| 98 | 5 | if (count($query) > 1) { |
|
| 99 | 2 | $simple = '(&'.$simple.')'; |
|
| 100 | 2 | $result .= $simple; |
|
| 101 | } else { |
||
| 102 | 3 | $result .= $simple; |
|
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | 7 | return $result; |
|
| 107 | } |
||
| 108 | |||
| 114 |