Total Complexity | 43 |
Total Lines | 219 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 2 | 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 | public function buildWhereQuery($conditions = array(), $query = array()) |
||
25 | { |
||
26 | $whereQuery = array(); |
||
27 | if (!count($conditions)) { |
||
28 | return $whereQuery; |
||
29 | } |
||
30 | |||
31 | $firstTime = true; |
||
32 | $whereQuery[] = 'WHERE'; |
||
33 | $this->whereAdded = true; |
||
34 | |||
35 | foreach ($conditions as $where) { |
||
36 | $sign = '='; |
||
37 | if(count($where)==3) { |
||
38 | $sign = $where[1]; |
||
39 | } |
||
40 | if ($firstTime) { |
||
41 | $whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
42 | $firstTime = false; |
||
43 | } else { |
||
44 | $whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | if (count($whereQuery)) { |
||
49 | $query = array_merge($query, $whereQuery); |
||
50 | } |
||
51 | return $query; |
||
52 | } |
||
53 | |||
54 | public function buildWhereInClauseQuery($terms = array()) |
||
66 | } |
||
67 | |||
68 | public function buildWhereRawQuery($conditions = array(), $query = array()) |
||
69 | { |
||
70 | $whereRawQuery = array(); |
||
71 | |||
72 | if (!count($conditions)) { |
||
73 | return $whereRawQuery; |
||
74 | } |
||
75 | |||
76 | $firstTime = false; |
||
77 | if ($this->whereAdded === false) { |
||
78 | $whereRawQuery[] = 'WHERE'; |
||
79 | $firstTime = true; |
||
80 | $this->whereAdded = true; |
||
81 | } |
||
82 | |||
83 | foreach ($conditions as $whereRaw) { |
||
84 | if ($firstTime) { |
||
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(), $query = array()) |
||
130 | } |
||
131 | |||
132 | public function buildWhereNotInQuery($conditions = array(), $query = array()) |
||
133 | { |
||
134 | $whereNotInQuery = array(); |
||
135 | |||
136 | if (!count($conditions)) { |
||
137 | return $whereNotInQuery; |
||
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 | if (count($whereNotInQuery)) { |
||
161 | $query = array_merge($query, $whereNotInQuery); |
||
162 | } |
||
163 | return $query; |
||
164 | } |
||
165 | |||
166 | public function buildWhereNullQuery($conditions = array(), $query = array()) |
||
193 | } |
||
194 | |||
195 | public function buildWhereNotNullQuery($conditions = array(), $query = array()) |
||
196 | { |
||
197 | $whereNotNullQuery = array(); |
||
198 | |||
199 | if (!count($conditions)) { |
||
200 | return $whereNotNullQuery; |
||
201 | } |
||
202 | |||
203 | $firstTime = false; |
||
204 | if ($this->whereAdded === false) { |
||
205 | $whereNotNullQuery[] = 'WHERE'; |
||
206 | $firstTime = true; |
||
207 | $this->whereAdded = true; |
||
208 | } |
||
209 | |||
210 | foreach ($conditions as $whereNotNull) { |
||
211 | if ($firstTime) { |
||
212 | $whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL'; |
||
213 | $firstTime = false; |
||
214 | } else { |
||
215 | $whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
||
216 | } |
||
217 | } |
||
218 | if (count($whereNotNullQuery)) { |
||
219 | $query = array_merge($query, $whereNotNullQuery); |
||
220 | } |
||
221 | return $query; |
||
222 | } |
||
223 | |||
224 | public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull) |
||
235 | } |
||
236 | } |
||
237 |