SqlToArray::getResponse()   F
last analyzed

Complexity

Conditions 29
Paths 166

Size

Total Lines 78
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
cc 29
eloc 57
c 6
b 1
f 1
nc 166
nop 0
dl 0
loc 78
rs 3.6166

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
namespace Manticoresearch\Response;
4
5
use Manticoresearch\Response;
6
7
class SqlToArray extends Response
8
{
9
    public function getResponse()
10
    {
11
        $response = parent::getResponse();
12
        // workaround for the change in Manticore Search made in 4.2.1, after
13
        // which any query in mode=raw returns an array
14
        if (is_array($response)
15
        and count($response) === 1
16
        and isset($response[0]['total'], $response[0]['error'], $response[0]['warning'])) {
17
            foreach ($response[0] as $k => $v) {
18
                $response[$k] = $v;
19
            }
20
            unset($response[0]);
21
        }
22
23
        if (isset($response['columns'], $response['data'])) {
24
            $data = [];
25
            array_walk($response['columns'], static function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

25
            array_walk($response['columns'], static function (&$value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
                $value = array_keys($value)[0];
27
            });
28
            $id = -1;
29
            foreach ($response['data'] as $property) {
30
                if (isset($property['id'])) {
31
                    $id = $property['id'];
32
                    if (count($property) > 1) {
33
                        unset($property['id']);
34
                    }
35
                } elseif (isset($this->params['customMapping']) and $this->params['customMapping']) {
36
                    if (isset($property['Field'])) {
37
                        $id = $property['Field'];
38
                        if (count($property) > 1) {
39
                            unset($property['Field']);
40
                        }
41
                    } elseif (isset($property['Variable_name'])) {
42
                        $id = $property['Variable_name'];
43
                        if (count($property) > 1) {
44
                            unset($property['Variable_name']);
45
                        }
46
                    } elseif (isset($property['Index'])) {
47
                        $id = $property['Index'];
48
                        if (count($property) > 1) {
49
                            unset($property['Index']);
50
                        }
51
                    } elseif (isset($property['Counter'])) {
52
                        $id = $property['Counter'];
53
                        if (count($property) > 1) {
54
                            unset($property['Counter']);
55
                        }
56
                    } elseif (isset($property['Key'])) {
57
                        $id = $property['Key'];
58
                        if (count($property) > 1) {
59
                            unset($property['Key']);
60
                        }
61
                    } elseif (isset($property['command'])) {
62
                        $id = $property['command'];
63
                        if (count($property) > 1) {
64
                            unset($property['command']);
65
                        }
66
                    } elseif (isset($property['suggest'])) {
67
                        $id = $property['suggest'];
68
                        if (count($property) > 1) {
69
                            unset($property['suggest']);
70
                        }
71
                    } elseif (isset($property['Variable'])) {
72
                        $id = $property['Variable'];
73
                        if (count($property) > 1) {
74
                            unset($property['Variable']);
75
                        }
76
                    } else {
77
                        $id++;
78
                    }
79
                } else {
80
                    $id++;
81
                }
82
                $data[$id] = (count($property) == 1)?array_shift($property):$property;
83
            }
84
            return (count($data) > 0) ? $data : [];
85
        }
86
        return $response;
87
    }
88
}
89