Passed
Push — master ( d07708...58b099 )
by RN
01:49
created

WhereQueryBuilder   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 6
Bugs 4 Features 0
Metric Value
eloc 128
c 6
b 4
f 0
dl 0
loc 225
rs 8.72
wmc 46

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A buildWhereInQuery() 0 30 6
A buildWhereQuery() 0 27 6
A buildWhereNullQuery() 0 27 6
A buildWhereRawQuery() 0 27 6
A buildWhereInClauseQuery() 0 12 3
A buildAllWhereQuery() 0 19 4
A buildWhereNotNullQuery() 0 27 6
A buildWhereNotInQuery() 0 30 6
A whereAddedCondition() 0 2 2

How to fix   Complexity   

Complex Class

Complex classes like WhereQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WhereQueryBuilder, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * The Query builder API.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 * @since v0.0.1 <Date: 16th April, 2019>
7
 */
8
9
namespace Dolphin\Builders;
10
11
use Dolphin\Parsers\WhereQueryParser;
12
13
/**
14
 * This class provides the mechanism to build the Where Queries.
15
 */
16
class WhereQueryBuilder extends QueryBuilder
17
{
18
    private $qb;
19
20
    public function __construct(){
21
        $this->qb = new QueryBuilder();
22
    }
23
24
    private function buildWhereInClauseQuery($terms = array())
25
    {
26
        if (is_int($terms[0])) {
27
            $dataStr = join(',', $terms);
28
            return $dataStr;
29
        } elseif (is_string($terms[0])) {
30
            $dataStr = join("', '", $terms);
31
            $dataStr = "'".$dataStr."'";
32
            return $dataStr;
33
        }
34
35
        return null;
36
    }
37
38
    private function whereAddedCondition($conditions = array()){
39
      return $this->whereAdded === false && count($conditions);
40
    }
41
42
    public function buildWhereQuery($conditions = array())
43
    {
44
        $whereQuery = array();
45
        if (count($conditions)<=0) {
46
            return array();
47
        }
48
49
        $firstTime = true;
50
        if ($this->whereAddedCondition($conditions)) {
51
          $whereQuery[] = 'WHERE';
52
          $this->whereAdded = true;
53
        }
54
55
        foreach ($conditions as $where) {
56
            $sign = '=';
57
            if(count($where)==3) {
58
                $sign = $where[1];
59
            }
60
            if ($firstTime) {
61
                $whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where));
62
                $firstTime = false;
63
            } else {
64
                $whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where));
65
            }
66
        }
67
68
        return $whereQuery;
69
    }
70
71
    public function buildWhereRawQuery($conditions = array(), $query = array())
72
    {
73
        $whereRawQuery = array();
74
75
        if (count($conditions)<=0) {
76
            return $query;
77
        }
78
79
        $firstTime = true;
80
        if ($this->whereAddedCondition($conditions)) {
81
            $whereRawQuery[] = 'WHERE';
82
            $this->whereAdded = true;
83
        }
84
85
        foreach ($conditions as $whereRaw) {
86
            if ($firstTime === true) {
87
                $whereRawQuery[] = $whereRaw;
88
                $firstTime = false;
89
            } else {
90
                $whereRawQuery[] = 'AND '.$whereRaw;
91
            }
92
        }
93
94
        if (count($whereRawQuery)) {
95
            $query = array_merge($query, $whereRawQuery);
96
        }
97
        return $query;
98
    }
99
100
    public function buildWhereInQuery($conditions = array())
101
    {
102
        $whereInQuery = array();
103
104
        if (count($conditions)<=0) {
105
            return array();
106
        }
107
108
        $firstTime = false;
109
        if ($this->whereAdded === false) {
110
            $whereInQuery[] = 'WHERE';
111
            $firstTime = true;
112
            $this->whereAdded = true;
113
        }
114
115
        foreach ($conditions as $whereIn) {
116
            $dataStr = $this->buildWhereInClauseQuery($whereIn[1]);
117
            if ($dataStr === null) {
118
                continue;
119
            }
120
121
            if ($firstTime) {
122
                $whereInQuery[] = trim($whereIn[0]).' IN ('.$dataStr.')';
123
                $firstTime = false;
124
            } else {
125
                $whereInQuery[] = 'AND '.trim($whereIn[0]).' IN ('.$dataStr.')';
126
            }
127
        }
128
129
        return $whereInQuery;
130
    }
131
132
    public function buildWhereNotInQuery($conditions = array())
133
    {
134
        $whereNotInQuery = array();
135
136
        if (count($conditions)<=0) {
137
            return array();
138
        }
139
140
        $firstTime = false;
141
        if ($this->whereAdded === false) {
142
            $whereNotInQuery[] = 'WHERE';
143
            $firstTime = true;
144
            $this->whereAdded = true;
145
        }
146
147
        foreach ($conditions as $whereNotIn) {
148
            $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]);
149
            if ($dataStr === null) {
150
                continue;
151
            }
152
153
            if ($firstTime) {
154
                $whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')';
155
                $firstTime = false;
156
            } else {
157
                $whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')';
158
            }
159
        }
160
161
        return $whereNotInQuery;
162
    }
163
164
    public function buildWhereNullQuery($conditions = array(), $query = array())
165
    {
166
        $whereNullQuery = array();
167
168
        if (count($conditions)<=0) {
169
            return $query;
170
        }
171
172
        $firstTime = false;
173
        if ($this->whereAdded === false) {
174
            $whereNullQuery[] = 'WHERE';
175
            $firstTime = true;
176
            $this->whereAdded = true;
177
        }
178
179
        foreach ($conditions as $whereNull) {
180
            if ($firstTime) {
181
                $whereNullQuery[] = trim($whereNull).' IS NULL';
182
                $firstTime = false;
183
            } else {
184
                $whereNullQuery[] = 'AND '.trim($whereNull).' IS NULL';
185
            }
186
        }
187
        if (count($whereNullQuery)) {
188
            $query = array_merge($query, $whereNullQuery);
189
        }
190
        return $query;
191
    }
192
193
    public function buildWhereNotNullQuery($conditions = array(), $query = array())
194
    {
195
        $whereNotNullQuery = array();
196
197
        if (count($conditions)<=0) {
198
            return $query;
199
        }
200
201
        $firstTime = false;
202
        if ($this->whereAdded === false) {
203
            $whereNotNullQuery[] = 'WHERE';
204
            $firstTime = true;
205
            $this->whereAdded = true;
206
        }
207
208
        foreach ($conditions as $whereNotNull) {
209
            if ($firstTime) {
210
                $whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL';
211
                $firstTime = false;
212
            } else {
213
                $whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL';
214
            }
215
        }
216
        if (count($whereNotNullQuery)) {
217
            $query = array_merge($query, $whereNotNullQuery);
218
        }
219
        return $query;
220
    }
221
222
    public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = array())
223
    {
224
        $query = $this->buildWhereQuery($where);
225
        $query = $this->buildWhereRawQuery($whereRaw, $query);
226
        $whereInQuery = $this->buildWhereInQuery($whereIn);
227
        if (count($whereInQuery)) {
228
            $query = array_merge($query, $whereInQuery);
229
        }
230
        $whereNotInQuery = $this->buildWhereNotInQuery($whereNotIn);
231
        if (count($whereNotInQuery)) {
232
            $query = array_merge($query, $whereNotInQuery);
233
        }
234
        $query = $this->buildWhereNullQuery($whereNull, $query);
235
        $query = $this->buildWhereNotNullQuery($whereNotNull, $query);
236
237
        if (count($query)) {
238
            $mainQuery = array_merge($mainQuery, $query);
239
        }
240
        return $mainQuery;
241
    }
242
}
243