Total Complexity | 47 |
Total Lines | 224 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 3 | Features | 0 |
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 |
||
16 | class WhereQueryBuilder extends QueryBuilder |
||
17 | { |
||
18 | private $qb; |
||
19 | |||
20 | public function __construct(){ |
||
22 | } |
||
23 | |||
24 | private function buildWhereInClauseQuery($terms = array()) |
||
25 | { |
||
26 | if (is_int($terms[0])) { |
||
27 | $dataStr = join(',', $terms); |
||
28 | } elseif (is_string($terms[0])) { |
||
29 | $dataStr = join("', '", $terms); |
||
30 | $dataStr = "'".$dataStr."'"; |
||
31 | } else { |
||
32 | return null; |
||
33 | } |
||
34 | |||
35 | return $dataStr; |
||
36 | } |
||
37 | |||
38 | public function buildWhereQuery($conditions = array()) |
||
39 | { |
||
40 | $whereQuery = array(); |
||
41 | if (count($conditions)<=0) { |
||
42 | return array(); |
||
43 | } |
||
44 | |||
45 | $firstTime = true; |
||
46 | if ($this->whereAdded === false && count($conditions)) { |
||
47 | $whereQuery[] = 'WHERE'; |
||
48 | $this->whereAdded = true; |
||
49 | } |
||
50 | |||
51 | foreach ($conditions as $where) { |
||
52 | $sign = '='; |
||
53 | if(count($where)==3) { |
||
54 | $sign = $where[1]; |
||
55 | } |
||
56 | if ($firstTime) { |
||
57 | $whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
58 | $firstTime = false; |
||
59 | } else { |
||
60 | $whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if (count($whereQuery)) { |
||
65 | return $whereQuery; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function buildWhereRawQuery($conditions = array(), $query = array()) |
||
70 | { |
||
71 | $whereRawQuery = array(); |
||
72 | |||
73 | if (count($conditions)<=0) { |
||
74 | return $query; |
||
75 | } |
||
76 | |||
77 | $firstTime = true; |
||
78 | if ($this->whereAdded === false && count($conditions)) { |
||
79 | $whereRawQuery[] = 'WHERE'; |
||
80 | $this->whereAdded = true; |
||
81 | } |
||
82 | |||
83 | foreach ($conditions as $whereRaw) { |
||
84 | if ($firstTime === true) { |
||
85 | $whereRawQuery[] = $whereRaw; |
||
86 | $firstTime = false; |
||
87 | } else { |
||
88 | $whereRawQuery[] = 'AND '.$whereRaw; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if (count($whereRawQuery)) { |
||
93 | $query = array_merge($query, $whereRawQuery); |
||
94 | } |
||
95 | return $query; |
||
96 | } |
||
97 | |||
98 | public function buildWhereInQuery($conditions = array()) |
||
128 | } |
||
129 | |||
130 | public function buildWhereNotInQuery($conditions = array()) |
||
131 | { |
||
132 | $whereNotInQuery = array(); |
||
133 | |||
134 | if (count($conditions)<=0) { |
||
135 | return array(); |
||
136 | } |
||
137 | |||
138 | $firstTime = false; |
||
139 | if ($this->whereAdded === false) { |
||
140 | $whereNotInQuery[] = 'WHERE'; |
||
141 | $firstTime = true; |
||
142 | $this->whereAdded = true; |
||
143 | } |
||
144 | |||
145 | foreach ($conditions as $whereNotIn) { |
||
146 | $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
||
147 | if ($dataStr === null) { |
||
148 | continue; |
||
149 | } |
||
150 | |||
151 | if ($firstTime) { |
||
152 | $whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
153 | $firstTime = false; |
||
154 | } else { |
||
155 | $whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return $whereNotInQuery; |
||
160 | } |
||
161 | |||
162 | public function buildWhereNullQuery($conditions = array(), $query = array()) |
||
189 | } |
||
190 | |||
191 | public function buildWhereNotNullQuery($conditions = array(), $query = array()) |
||
192 | { |
||
193 | $whereNotNullQuery = array(); |
||
194 | |||
195 | if (count($conditions)<=0) { |
||
196 | return $query; |
||
197 | } |
||
198 | |||
199 | $firstTime = false; |
||
200 | if ($this->whereAdded === false) { |
||
201 | $whereNotNullQuery[] = 'WHERE'; |
||
202 | $firstTime = true; |
||
203 | $this->whereAdded = true; |
||
204 | } |
||
205 | |||
206 | foreach ($conditions as $whereNotNull) { |
||
207 | if ($firstTime) { |
||
208 | $whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL'; |
||
209 | $firstTime = false; |
||
210 | } else { |
||
211 | $whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
||
212 | } |
||
213 | } |
||
214 | if (count($whereNotNullQuery)) { |
||
215 | $query = array_merge($query, $whereNotNullQuery); |
||
216 | } |
||
217 | return $query; |
||
218 | } |
||
219 | |||
220 | public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = array()) |
||
240 | } |
||
241 | } |
||
242 |