Complex classes like Db 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Db, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Db |
||
27 | { |
||
28 | use Options; |
||
29 | |||
30 | /** |
||
31 | * PDO connection settings |
||
32 | * |
||
33 | * @var array |
||
34 | * @link http://php.net/manual/en/pdo.construct.php |
||
35 | */ |
||
36 | protected $connect = [ |
||
37 | 'type' => 'mysql', |
||
38 | 'host' => 'localhost', |
||
39 | 'name' => '', |
||
40 | 'user' => 'root', |
||
41 | 'pass' => '', |
||
42 | 'options' => [] |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * PDO connection flags |
||
47 | * |
||
48 | * @var array |
||
49 | * @link http://php.net/manual/en/pdo.setattribute.php |
||
50 | */ |
||
51 | protected $attributes = [ |
||
52 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @var \PDO PDO instance |
||
57 | */ |
||
58 | protected $handler; |
||
59 | |||
60 | /** |
||
61 | * Setup connection |
||
62 | * |
||
63 | * Just save connection settings |
||
64 | * <code> |
||
65 | * $db->setConnect([ |
||
66 | * 'type' => 'mysql', |
||
67 | * 'host' => 'localhost', |
||
68 | * 'name' => 'db name', |
||
69 | * 'user' => 'root', |
||
70 | * 'pass' => '' |
||
71 | * ]); |
||
72 | * </code> |
||
73 | * |
||
74 | * @param array $connect options |
||
75 | * |
||
76 | * @throws ConfigurationException |
||
77 | * @return void |
||
78 | * @throws DbException |
||
79 | */ |
||
80 | 47 | public function setConnect(array $connect) |
|
81 | { |
||
82 | 47 | $this->connect = array_merge($this->connect, $connect); |
|
83 | 47 | $this->checkConnect(); |
|
84 | 47 | } |
|
85 | |||
86 | /** |
||
87 | * Check connection options |
||
88 | * |
||
89 | * @return void |
||
90 | * @throws ConfigurationException |
||
91 | */ |
||
92 | 47 | private function checkConnect() |
|
105 | |||
106 | /** |
||
107 | * Setup attributes for PDO connect |
||
108 | * |
||
109 | * @param array $attributes |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | public function setAttributes(array $attributes) |
||
117 | |||
118 | /** |
||
119 | * Connect to Db |
||
120 | * |
||
121 | * @return bool |
||
122 | * @throws DbException |
||
123 | */ |
||
124 | 39 | public function connect() : bool |
|
146 | |||
147 | /** |
||
148 | * Disconnect PDO and clean default adapter |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | 22 | public function disconnect() |
|
158 | |||
159 | /** |
||
160 | * Return PDO handler |
||
161 | * |
||
162 | * @return \PDO |
||
163 | * @throws DbException |
||
164 | */ |
||
165 | 39 | public function handler() : \PDO |
|
172 | |||
173 | /** |
||
174 | * Prepare SQL query and return PDO Statement |
||
175 | * |
||
176 | * @param string $sql SQL query with placeholders |
||
177 | * @param array $params params for query placeholders |
||
178 | * |
||
179 | * @todo Switch to PDO::activeQueryString() when it will be possible |
||
180 | * @link https://wiki.php.net/rfc/debugging_pdo_prepared_statement_emulation |
||
181 | * |
||
182 | * @return \PDOStatement |
||
183 | * @throws DbException |
||
184 | */ |
||
185 | 26 | protected function prepare($sql, $params) : \PDOStatement |
|
194 | |||
195 | /** |
||
196 | * Quotes a string for use in a query |
||
197 | * |
||
198 | * Example of usage |
||
199 | * <code> |
||
200 | * $db->quote($_GET['id']) |
||
201 | * </code> |
||
202 | * |
||
203 | * @param string $value |
||
204 | * @param int $type |
||
205 | * |
||
206 | * @return string |
||
207 | * @throws DbException |
||
208 | */ |
||
209 | 1 | public function quote($value, $type = \PDO::PARAM_STR) : string |
|
213 | |||
214 | /** |
||
215 | * Quote a string so it can be safely used as a table or column name |
||
216 | * |
||
217 | * @param string $identifier |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 16 | public function quoteIdentifier($identifier) : string |
|
233 | |||
234 | /** |
||
235 | * Execute SQL query |
||
236 | * |
||
237 | * Example of usage |
||
238 | * <code> |
||
239 | * $db->query("SET NAMES 'utf8'"); |
||
240 | * </code> |
||
241 | * |
||
242 | * @param string $sql SQL query with placeholders |
||
243 | * "UPDATE users SET name = :name WHERE id = :id" |
||
244 | * @param array $params params for query placeholders (optional) |
||
245 | * array (':name' => 'John', ':id' => '123') |
||
246 | * @param array $types Types of params (optional) |
||
247 | * array (':name' => \PDO::PARAM_STR, ':id' => \PDO::PARAM_INT) |
||
248 | * |
||
249 | * @return integer the number of rows |
||
250 | * @throws DbException |
||
251 | */ |
||
252 | 22 | public function query($sql, $params = [], $types = []) |
|
267 | |||
268 | /** |
||
269 | * Create new query select builder |
||
270 | * |
||
271 | * @param array|string ...$select The selection expressions |
||
272 | * |
||
273 | * @return Query\Select |
||
274 | */ |
||
275 | 1 | public function select(...$select) : Query\Select |
|
281 | |||
282 | /** |
||
283 | * Create new query insert builder |
||
284 | * |
||
285 | * @param string $table |
||
286 | * |
||
287 | * @return Query\Insert |
||
288 | */ |
||
289 | 1 | public function insert($table) : Query\Insert |
|
295 | |||
296 | /** |
||
297 | * Create new query update builder |
||
298 | * |
||
299 | * @param string $table |
||
300 | * |
||
301 | * @return Query\Update |
||
302 | */ |
||
303 | 1 | public function update($table) : Query\Update |
|
309 | |||
310 | /** |
||
311 | * Create new query update builder |
||
312 | * |
||
313 | * @param string $table |
||
314 | * |
||
315 | * @return Query\Delete |
||
316 | */ |
||
317 | 9 | public function delete($table) : Query\Delete |
|
323 | |||
324 | /** |
||
325 | * Return first field from first element from the result set |
||
326 | * |
||
327 | * Example of usage |
||
328 | * <code> |
||
329 | * $db->fetchOne("SELECT COUNT(*) FROM users"); |
||
330 | * </code> |
||
331 | * |
||
332 | * @param string $sql SQL query with placeholders |
||
333 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
334 | * @param array $params params for query placeholders (optional) |
||
335 | * array (':name' => 'John', ':pass' => '123456') |
||
336 | * |
||
337 | * @return string |
||
338 | * @throws DbException |
||
339 | */ |
||
340 | 5 | public function fetchOne($sql, $params = []) |
|
348 | |||
349 | /** |
||
350 | * Returns an array containing first row from the result set |
||
351 | * |
||
352 | * Example of usage |
||
353 | * <code> |
||
354 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ". $db->quote($id)); |
||
355 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ?", [$id]); |
||
356 | * $db->fetchRow("SELECT name, email FROM users WHERE id = :id", [':id'=>$id]); |
||
357 | * </code> |
||
358 | * |
||
359 | * @param string $sql SQL query with placeholders |
||
360 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
361 | * @param array $params params for query placeholders (optional) |
||
362 | * array (':name' => 'John', ':pass' => '123456') |
||
363 | * |
||
364 | * @return array array ('name' => 'John', 'email' => '[email protected]') |
||
365 | * @throws DbException |
||
366 | */ |
||
367 | 1 | public function fetchRow($sql, $params = []) |
|
375 | |||
376 | /** |
||
377 | * Returns an array containing all of the result set rows |
||
378 | * |
||
379 | * Example of usage |
||
380 | * <code> |
||
381 | * $db->fetchAll("SELECT * FROM users WHERE ip = ?", ['192.168.1.1']); |
||
382 | * </code> |
||
383 | * |
||
384 | * @param string $sql SQL query with placeholders |
||
385 | * "SELECT * FROM users WHERE ip = :ip" |
||
386 | * @param array $params params for query placeholders (optional) |
||
387 | * array (':ip' => '127.0.0.1') |
||
388 | * |
||
389 | * @return array[] |
||
390 | * @throws DbException |
||
391 | */ |
||
392 | 3 | public function fetchAll($sql, $params = []) |
|
400 | |||
401 | /** |
||
402 | * Returns an array containing one column from the result set rows |
||
403 | * |
||
404 | * @param string $sql SQL query with placeholders |
||
405 | * "SELECT id FROM users WHERE ip = :ip" |
||
406 | * @param array $params params for query placeholders (optional) |
||
407 | * array (':ip' => '127.0.0.1') |
||
408 | * |
||
409 | * @return array |
||
410 | * @throws DbException |
||
411 | */ |
||
412 | 1 | public function fetchColumn($sql, $params = []) |
|
420 | |||
421 | /** |
||
422 | * Returns an array containing all of the result set rows |
||
423 | * |
||
424 | * Group by first column |
||
425 | * |
||
426 | * <code> |
||
427 | * $db->fetchGroup("SELECT ip, COUNT(id) FROM users GROUP BY ip", []); |
||
428 | * </code> |
||
429 | * |
||
430 | * @param string $sql SQL query with placeholders |
||
431 | * "SELECT ip, id FROM users" |
||
432 | * @param array $params params for query placeholders (optional) |
||
433 | * @param mixed $object |
||
434 | * |
||
435 | * @return array |
||
436 | * @throws DbException |
||
437 | */ |
||
438 | 1 | public function fetchGroup($sql, $params = [], $object = null) |
|
451 | |||
452 | /** |
||
453 | * Returns an array containing all of the result set rows |
||
454 | * |
||
455 | * Group by first column |
||
456 | * |
||
457 | * @param string $sql SQL query with placeholders |
||
458 | * "SELECT ip, id FROM users" |
||
459 | * @param array $params params for query placeholders (optional) |
||
460 | * |
||
461 | * @return array |
||
462 | * @throws DbException |
||
463 | */ |
||
464 | 1 | public function fetchColumnGroup($sql, $params = []) |
|
472 | |||
473 | /** |
||
474 | * Returns an array containing all of the result set rows |
||
475 | * |
||
476 | * Group by first unique column |
||
477 | * |
||
478 | * @param string $sql SQL query with placeholders |
||
479 | * "SELECT email, name, sex FROM users" |
||
480 | * @param array $params params for query placeholders (optional) |
||
481 | * |
||
482 | * @return array |
||
483 | * @throws DbException |
||
484 | */ |
||
485 | 2 | public function fetchUniqueGroup($sql, $params = []) |
|
493 | |||
494 | /** |
||
495 | * Returns a key-value array |
||
496 | * |
||
497 | * @param string $sql SQL query with placeholders |
||
498 | * "SELECT id, username FROM users WHERE ip = :ip" |
||
499 | * @param array $params params for query placeholders (optional) |
||
500 | * array (':ip' => '127.0.0.1') |
||
501 | * |
||
502 | * @return array |
||
503 | * @throws DbException |
||
504 | */ |
||
505 | 1 | public function fetchPairs($sql, $params = []) |
|
513 | |||
514 | /** |
||
515 | * Returns an object containing first row from the result set |
||
516 | * |
||
517 | * Example of usage |
||
518 | * <code> |
||
519 | * // Fetch object to stdClass |
||
520 | * $stdClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id]); |
||
521 | * // Fetch object to new Some object |
||
522 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], 'Some'); |
||
523 | * // Fetch object to exists instance of Some object |
||
524 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], $someClass); |
||
525 | * </code> |
||
526 | * |
||
527 | * @param string $sql SQL query with placeholders |
||
528 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
529 | * @param array $params params for query placeholders (optional) |
||
530 | * array (':name' => 'John', ':pass' => '123456') |
||
531 | * @param mixed $object |
||
532 | * |
||
533 | * @return array |
||
534 | * @throws DbException |
||
535 | */ |
||
536 | 3 | public function fetchObject($sql, $params = [], $object = 'stdClass') |
|
553 | |||
554 | /** |
||
555 | * Returns an array of objects containing the result set |
||
556 | * |
||
557 | * @param string $sql SQL query with placeholders |
||
558 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
559 | * @param array $params params for query placeholders (optional) |
||
560 | * array (':name' => 'John', ':pass' => '123456') |
||
561 | * @param mixed $object Class name or instance |
||
562 | * |
||
563 | * @return array |
||
564 | * @throws DbException |
||
565 | */ |
||
566 | 12 | public function fetchObjects($sql, $params = [], $object = null) |
|
582 | |||
583 | /** |
||
584 | * Returns an array of linked objects containing the result set |
||
585 | * |
||
586 | * @param string $sql SQL query with placeholders |
||
587 | * "SELECT '__users', u.*, '__users_profile', up.* |
||
588 | * FROM users u |
||
589 | * LEFT JOIN users_profile up ON up.userId = u.id |
||
590 | * WHERE u.name = :name" |
||
591 | * @param array $params params for query placeholders (optional) |
||
592 | * array (':name' => 'John') |
||
593 | * |
||
594 | * @return array |
||
595 | * @throws DbException |
||
596 | */ |
||
597 | public function fetchRelations($sql, $params = []) |
||
610 | |||
611 | /** |
||
612 | * Transaction wrapper |
||
613 | * |
||
614 | * Example of usage |
||
615 | * <code> |
||
616 | * $db->transaction(function() use ($db) { |
||
617 | * $db->query("INSERT INTO `table` ..."); |
||
618 | * $db->query("UPDATE `table` ..."); |
||
619 | * $db->query("DELETE FROM `table` ..."); |
||
620 | * }) |
||
621 | * </code> |
||
622 | * |
||
623 | * @param callable $process callable structure - closure function or class with __invoke() method |
||
624 | * |
||
625 | * @return mixed|bool |
||
626 | * @throws DbException |
||
627 | */ |
||
628 | 6 | public function transaction(callable $process) |
|
641 | |||
642 | /** |
||
643 | * Setup timer |
||
644 | * |
||
645 | * @return void |
||
646 | */ |
||
647 | 39 | protected function ok() |
|
651 | |||
652 | /** |
||
653 | * Log queries by Application |
||
654 | * |
||
655 | * @param string $sql SQL query for logs |
||
656 | * @param array $context |
||
657 | * |
||
658 | * @return void |
||
659 | */ |
||
660 | 39 | protected function log($sql, array $context = []) |
|
670 | } |
||
671 |