Total Complexity | 41 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 16 | ||
Bugs | 4 | Features | 5 |
Complex classes like Dolphin 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 Dolphin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class Dolphin |
||
55 | { |
||
56 | protected $fields = []; |
||
57 | public $table; |
||
58 | public $className; |
||
59 | protected $groupBy; |
||
60 | protected $orderBy; |
||
61 | protected $having; |
||
62 | protected $join = []; |
||
63 | protected $leftJoin = []; |
||
64 | protected $rightJoin = []; |
||
65 | protected $crossJoin = []; |
||
66 | protected $where = []; |
||
67 | protected $whereRaw = []; |
||
68 | protected $whereIn = []; |
||
69 | protected $whereNotIn = []; |
||
70 | protected $whereNull = []; |
||
71 | protected $whereNotNull = []; |
||
72 | protected $limit; |
||
73 | protected $offset; |
||
74 | protected $results; |
||
75 | |||
76 | private function getFields(array $args, bool $quote = true){ |
||
77 | return (new QueryBuilder())->getFields($args, $quote); |
||
78 | } |
||
79 | |||
80 | private function validateArgsCount($noOfArgs){ |
||
81 | if($noOfArgs<2 || $noOfArgs >3){ |
||
82 | throw new Exception('Where parameter contains invalid number of parameters', 1); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | public function select() |
||
87 | { |
||
88 | $args = func_get_args(); |
||
89 | $fldAr = $this->getFields($args, true); |
||
90 | $this->fields = array_merge($this->fields, $fldAr); |
||
91 | |||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | public function selectRaw() |
||
96 | { |
||
97 | $args = func_get_args(); |
||
98 | $fldAr = $this->getFields($args, false); |
||
99 | $this->fields = array_merge($this->fields, $fldAr); |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | public function join($join, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
105 | { |
||
106 | $this->join = array_merge($this->join, [[$join, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | public function leftJoin($leftJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
112 | { |
||
113 | $this->leftJoin = array_merge($this->leftJoin, [[$leftJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | public function rightJoin($rightJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
119 | { |
||
120 | $this->rightJoin = array_merge($this->rightJoin, [[$rightJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | public function crossJoin($crossJoin, $params = null) |
||
126 | { |
||
127 | $this->crossJoin = array_merge($this->crossJoin, [[$crossJoin, $params]]); |
||
128 | |||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @throws Exception |
||
134 | */ |
||
135 | public function where() |
||
136 | { |
||
137 | $args = func_get_args(); |
||
138 | $noOfArgs = func_num_args(); |
||
139 | $this->validateArgsCount($noOfArgs); |
||
140 | |||
141 | if($noOfArgs===2){ |
||
142 | $this->where = array_merge($this->where, [[$args[0], '=', $args[1]]]); |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | $this->where = array_merge($this->where, [[$args[0], $args[1], $args[2]]]); |
||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | public function whereIn($whereIn, $params = []) |
||
151 | { |
||
152 | $this->whereIn = array_merge($this->whereIn, [[$whereIn, $params]]); |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | public function whereNotIn($whereNotIn, $params = []) |
||
158 | { |
||
159 | $this->whereNotIn = array_merge($this->whereNotIn, [[$whereNotIn, $params]]); |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | public function whereNull($whereNull) |
||
165 | { |
||
166 | $this->whereNull = array_merge($this->whereNull, [$whereNull]); |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | public function whereNotNull($whereNotNull) |
||
176 | } |
||
177 | |||
178 | public function whereRaw($whereConditions) |
||
179 | { |
||
180 | $this->whereRaw = array_merge($this->whereRaw, [$whereConditions]); |
||
181 | |||
182 | return $this; |
||
183 | } |
||
184 | |||
185 | public function offset($offset) |
||
186 | { |
||
187 | $this->offset = $offset; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | public function limit($limit) |
||
193 | { |
||
194 | $this->limit = $limit; |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | public function orderBy($orderBy) |
||
200 | { |
||
201 | $this->orderBy = $orderBy; |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | public function groupBy($groupBy) |
||
207 | { |
||
208 | $this->groupBy = $groupBy; |
||
209 | |||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | public function having($having) |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Builds Query added by method chaining. |
||
222 | * It has the main logic of ORM |
||
223 | */ |
||
224 | protected function buildQuery() |
||
225 | { |
||
226 | $query = (new QueryBuilder())->buildQuery([ |
||
227 | 'table' => $this->table, |
||
247 | } |
||
248 | |||
249 | protected function reset() |
||
250 | { |
||
251 | $this->fields = []; |
||
252 | $this->table = null; |
||
253 | $this->className = null; |
||
254 | $this->groupBy = null; |
||
255 | $this->orderBy = null; |
||
256 | $this->having = null; |
||
257 | $this->join = []; |
||
258 | $this->leftJoin = []; |
||
259 | $this->rightJoin = []; |
||
260 | $this->crossJoin = []; |
||
261 | $this->where = []; |
||
262 | $this->whereRaw = []; |
||
263 | $this->whereIn = []; |
||
264 | $this->whereNotIn = []; |
||
265 | $this->whereNull = []; |
||
266 | $this->whereNotNull = []; |
||
267 | $this->limit = null; |
||
268 | $this->offset = null; |
||
269 | } |
||
270 | |||
271 | public function prepare($query, $fetchRows = 'all') |
||
272 | { |
||
273 | $results = (new PrepareQueryBuilder())->prepare($this->where, $this->className, $query, $fetchRows); |
||
274 | |||
275 | $this->results = $results; |
||
276 | $this->reset(); |
||
277 | |||
278 | return $results; |
||
279 | } |
||
280 | |||
281 | public function query($query, $fetchRows = 'all') |
||
287 | } |
||
288 | |||
289 | public function get() |
||
290 | { |
||
291 | return $this->prepare($this->buildQuery()); |
||
292 | } |
||
293 | |||
294 | public function first() |
||
295 | { |
||
296 | $query = $this->buildQuery(); |
||
297 | |||
298 | if (!strripos($query, 'LIMIT 1')) { |
||
299 | $query .= ' LIMIT 1'; |
||
300 | } |
||
301 | |||
302 | $result = $this->prepare($query, 'first'); |
||
303 | |||
304 | return isset($result->data) ? $result->data : ''; |
||
|
|||
305 | } |
||
306 | |||
307 | public function all() |
||
308 | { |
||
309 | $query = $this->buildQuery(); |
||
310 | $result = $this->prepare($query); |
||
311 | |||
312 | return isset($result->data) ? $result->data : ''; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * It fetches the row by primary key |
||
317 | * |
||
318 | * @since v0.0.5 |
||
319 | */ |
||
320 | public function find($id) |
||
321 | { |
||
322 | $this->where('id', $id); |
||
323 | |||
324 | return $this->first(); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * It fetches the row by primary key |
||
329 | * |
||
330 | * @param int $id |
||
331 | * @return object $row |
||
332 | * @throws Exception |
||
333 | * @since v0.0.5 |
||
334 | */ |
||
335 | public function findOrFail($id) |
||
336 | { |
||
337 | $this->where('id', $id); |
||
338 | $row = $this->first(); |
||
339 | |||
340 | if($row == null ){ |
||
341 | throw new Exception("The record does not exists!"); |
||
342 | } |
||
343 | |||
344 | return $row; |
||
345 | } |
||
346 | |||
347 | public function count() |
||
348 | { |
||
349 | $this->fields = null; |
||
350 | $query = $this->buildQuery(); |
||
351 | $query = str_replace('SELECT * ', 'SELECT COUNT(*) as count ', $query); |
||
352 | |||
353 | return $this->query($query, 'count'); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * It inserts the new rows |
||
358 | * |
||
359 | * @param array $rows |
||
360 | * @return integer $lastInsertedId |
||
361 | * @throws Exception |
||
362 | * @since v0.0.5 |
||
363 | */ |
||
364 | public function insert($rows) |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * It updates the rows |
||
371 | * |
||
372 | * @param array $row |
||
373 | * @return boolean |
||
374 | * @throws Exception |
||
375 | * @since v0.0.5 |
||
376 | */ |
||
377 | public function update($row) |
||
378 | { |
||
379 | $result = (new UpdateQueryBuilder())->update( |
||
380 | $row, |
||
381 | $this->table, |
||
382 | $this->where, |
||
383 | $this->whereRaw, |
||
384 | $this->whereIn, |
||
385 | $this->whereNotIn, |
||
386 | $this->whereNull, |
||
387 | $this->whereNotNull |
||
388 | ); |
||
389 | |||
390 | $this->reset(); |
||
391 | |||
392 | return $result; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * It truncates the table |
||
397 | * |
||
398 | * @return boolean |
||
399 | * @throws Exception |
||
400 | * @since v0.0.5 |
||
401 | */ |
||
402 | public function truncate() |
||
403 | { |
||
404 | $qb = new QueryBuilder(); |
||
405 | $query = "TRUNCATE ".$this->table; |
||
406 | |||
407 | try{ |
||
408 | Connection::get()->query($qb->queryPrefix($query)); |
||
409 | } catch(Exception $e){ |
||
410 | throw new Exception($e->getMessage()); |
||
411 | } |
||
412 | |||
413 | return true; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * It deleted the rows matched by where clause |
||
418 | * |
||
419 | * @return boolean |
||
420 | * @throws Exception |
||
421 | * @since v0.0.5 |
||
422 | */ |
||
423 | public function delete() |
||
438 | } |
||
439 | |||
440 | } |
||
441 |