Total Complexity | 51 |
Total Lines | 448 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 3 | Features | 4 |
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 |
||
53 | class Dolphin |
||
54 | { |
||
55 | protected $fields = array(); |
||
56 | public $table; |
||
57 | public $className; |
||
58 | protected $groupBy; |
||
59 | protected $orderBy; |
||
60 | protected $having; |
||
61 | protected $join = array(); |
||
62 | protected $leftJoin = array(); |
||
63 | protected $rightJoin = array(); |
||
64 | protected $crossJoin = array(); |
||
65 | protected $where = array(); |
||
66 | protected $whereRaw = array(); |
||
67 | protected $whereIn = array(); |
||
68 | protected $whereNotIn = array(); |
||
69 | protected $whereNull = array(); |
||
70 | protected $whereNotNull = array(); |
||
71 | protected $limit; |
||
72 | protected $offset; |
||
73 | protected $results; |
||
74 | |||
75 | private function getFields(array $args, bool $quote = true){ |
||
76 | $fldAr = array(); |
||
77 | $qb = new QueryBuilder(); |
||
78 | |||
79 | foreach ($args as $arg) { |
||
80 | foreach (explode(',', $arg) as $ar) { |
||
81 | $fldAr[] = ($quote === true) ? $qb->quote(trim($ar)) : trim($ar); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return $fldAr; |
||
86 | } |
||
87 | |||
88 | public function select() |
||
89 | { |
||
90 | $args = func_get_args(); |
||
91 | $fldAr = $this->getFields($args, true); |
||
92 | $this->fields = array_merge($this->fields, $fldAr); |
||
93 | |||
94 | return $this; |
||
95 | } |
||
96 | |||
97 | public function selectRaw() |
||
98 | { |
||
99 | $args = func_get_args(); |
||
100 | $fldAr = $this->getFields($args, false); |
||
101 | $this->fields = array_merge($this->fields, $fldAr); |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | public function join($join, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
107 | { |
||
108 | $this->join = array_merge($this->join, [[$join, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | public function leftJoin($leftJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
114 | { |
||
115 | $this->leftJoin = array_merge($this->leftJoin, [[$leftJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | public function rightJoin($rightJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
121 | { |
||
122 | $this->rightJoin = array_merge($this->rightJoin, [[$rightJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | public function crossJoin($crossJoin, $params = null) |
||
128 | { |
||
129 | $this->crossJoin = array_merge($this->crossJoin, [[$crossJoin, $params]]); |
||
130 | |||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @throws Exception |
||
136 | */ |
||
137 | public function where() |
||
138 | { |
||
139 | $args = func_get_args(); |
||
140 | if(func_num_args()===2){ |
||
141 | $this->where = array_merge($this->where, [[$args[0], '=', $args[1]]]); |
||
142 | return $this; |
||
143 | } elseif(func_num_args()===3){ |
||
144 | $this->where = array_merge($this->where, [[$args[0], $args[1], $args[2]]]); |
||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | throw new Exception('Where parameter contains invalid number of parameters', 1); |
||
149 | } |
||
150 | |||
151 | public function whereIn($whereIn, $params = array()) |
||
152 | { |
||
153 | $this->whereIn = array_merge($this->whereIn, [[$whereIn, $params]]); |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | public function whereNotIn($whereNotIn, $params = array()) |
||
159 | { |
||
160 | $this->whereNotIn = array_merge($this->whereNotIn, [[$whereNotIn, $params]]); |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | public function whereNull($whereNull) |
||
166 | { |
||
167 | $this->whereNull = array_merge($this->whereNull, [$whereNull]); |
||
168 | |||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | public function whereNotNull($whereNotNull) |
||
177 | } |
||
178 | |||
179 | public function whereRaw($whereConditions) |
||
180 | { |
||
181 | $this->whereRaw = array_merge($this->whereRaw, [$whereConditions]); |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | public function offset($offset) |
||
187 | { |
||
188 | $this->offset = $offset; |
||
189 | |||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | public function limit($limit) |
||
194 | { |
||
195 | $this->limit = $limit; |
||
196 | |||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | public function orderBy($orderBy) |
||
201 | { |
||
202 | $this->orderBy = $orderBy; |
||
203 | |||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | public function groupBy($groupBy) |
||
208 | { |
||
209 | $this->groupBy = $groupBy; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | public function having($having) |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Builds Query added by method chaining. |
||
223 | * It has the main logic of ORM |
||
224 | */ |
||
225 | protected function buildQuery() |
||
250 | } |
||
251 | |||
252 | protected function reset() |
||
272 | } |
||
273 | |||
274 | public function prepare($query, $fetchRows = 'all') |
||
275 | { |
||
276 | $qb = new QueryBuilder(); |
||
277 | $wqp = new WhereQueryParser(); |
||
278 | $util = new Utils(); |
||
279 | $rows = null; |
||
280 | |||
281 | try { |
||
282 | $ar = $wqp->parseWhereQuery($this->where); |
||
283 | $stmt = Connection::get()->prepare($qb->queryPrefix($query)); |
||
284 | $stmt->execute($ar); |
||
285 | |||
286 | if ($fetchRows == 'first') { |
||
287 | $this->results = $stmt->fetch(\PDO::FETCH_OBJ); |
||
288 | } else{ |
||
289 | $this->results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
||
290 | } |
||
291 | |||
292 | if(count($this->results) ){ |
||
293 | // now turn this stdClass object to the object type of calling model |
||
294 | $rows = $util->turnObjects($this->className, $this->results); |
||
295 | } |
||
296 | // Reset class variables |
||
297 | $this->reset(); |
||
298 | |||
299 | return $rows; |
||
300 | } catch (\PDOException $ex) { |
||
301 | throw new \PDOException($ex->getMessage(), 1); |
||
302 | } catch (Exception $e) { |
||
303 | throw new Exception($e->getMessage(), 1); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | public function query($query, $fetchRows = 'all') |
||
326 | } |
||
327 | } |
||
328 | |||
329 | public function get() |
||
330 | { |
||
331 | return $this->prepare($this->buildQuery()); |
||
332 | } |
||
333 | |||
334 | public function first() |
||
335 | { |
||
336 | $query = $this->buildQuery(); |
||
337 | |||
338 | if (!strripos($query, 'LIMIT 1')) { |
||
339 | $query .= ' LIMIT 1'; |
||
340 | } |
||
341 | |||
342 | return $this->prepare($query, 'first'); |
||
343 | } |
||
344 | |||
345 | public function all() |
||
346 | { |
||
347 | $query = $this->buildQuery(); |
||
348 | |||
349 | return $this->prepare($query); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * It fetches the row by primary key |
||
354 | * |
||
355 | * @since v0.0.5 |
||
356 | */ |
||
357 | public function find($id) |
||
358 | { |
||
359 | $this->where('id', $id); |
||
360 | |||
361 | return $this->first(); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * It fetches the row by primary key |
||
366 | * |
||
367 | * @param int $id |
||
368 | * @return object $row |
||
369 | * @throws Exception |
||
370 | * @since v0.0.5 |
||
371 | */ |
||
372 | public function findOrFail($id) |
||
373 | { |
||
374 | $this->where('id', $id); |
||
375 | |||
376 | $row = $this->first(); |
||
377 | |||
378 | if($row == null ){ |
||
379 | throw new Exception("The record does not exists!"); |
||
380 | } |
||
381 | |||
382 | return $row; |
||
383 | } |
||
384 | |||
385 | public function count() |
||
386 | { |
||
387 | $this->fields = null; |
||
388 | $query = $this->buildQuery(); |
||
389 | $query = str_replace('SELECT * ', 'SELECT COUNT(*) as count ', $query); |
||
390 | |||
391 | return $this->query($query, 'count'); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * It truncates the table |
||
396 | * |
||
397 | * @return boolean |
||
398 | * @throws Exception |
||
399 | * @since v0.0.5 |
||
400 | */ |
||
401 | public function truncate() |
||
402 | { |
||
403 | $qb = new QueryBuilder(); |
||
404 | $query = "TRUNCATE ".$this->table; |
||
405 | |||
406 | try{ |
||
407 | Connection::get()->query($qb->queryPrefix($query)); |
||
408 | } catch(Exception $e){ |
||
409 | throw new Exception($e->getMessage()); |
||
410 | } |
||
411 | |||
412 | return true; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * It inserts the new rows |
||
417 | * |
||
418 | * @param array $rows |
||
419 | * @return integer $lastInsertedId |
||
420 | * @throws Exception |
||
421 | * @since v0.0.5 |
||
422 | */ |
||
423 | public function insert($rows) |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * It updates the rows |
||
431 | * |
||
432 | * @param array $row |
||
433 | * @return boolean |
||
434 | * @throws Exception |
||
435 | * @since v0.0.5 |
||
436 | */ |
||
437 | public function update($row) |
||
438 | { |
||
439 | $qb = new QueryBuilder(); |
||
440 | $wqb = new WhereQueryBuilder(); |
||
441 | $query = "UPDATE ".$this->table." SET "; |
||
442 | $ar = array(); |
||
443 | |||
444 | foreach($row as $key => $val){ |
||
445 | $ar[':'.$key] = $val; |
||
446 | $query.= $qb->quote($key)." =:".$key.","; |
||
447 | } |
||
448 | |||
449 | $query = rtrim($query, ","); |
||
450 | |||
451 | try{ |
||
452 | $whereQuery = $wqb->buildAllWhereQuery( |
||
453 | $this->where, |
||
454 | $this->whereRaw, |
||
455 | $this->whereIn, |
||
456 | $this->whereNotIn, |
||
457 | $this->whereNull, |
||
458 | $this->whereNotNull |
||
459 | ); |
||
460 | $query.= " ".join(" ", $whereQuery); |
||
461 | $stmt = Connection::get()->prepare($qb->queryPrefix($query)); |
||
462 | $stmt->execute($ar); |
||
463 | $this->reset(); |
||
464 | } catch(Exception $e){ |
||
465 | throw new Exception($e->getMessage()); |
||
466 | } |
||
467 | |||
468 | return true; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * It deleted the rows matched by where clause |
||
473 | * |
||
474 | * @return boolean |
||
475 | * @throws Exception |
||
476 | * @since v0.0.5 |
||
477 | */ |
||
478 | public function delete() |
||
501 | } |
||
502 | |||
503 | } |
||
504 |