Total Complexity | 46 |
Total Lines | 225 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 4 | 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 | 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()) |
||
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()) |
||
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()) |
||
241 | } |
||
242 | } |
||
243 |