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 |
||
22 | class Db |
||
23 | { |
||
24 | /** |
||
25 | * Database handle/connection. |
||
26 | * |
||
27 | * @var PDO |
||
28 | */ |
||
29 | protected $pdo; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $dsn; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $username; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $password; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $options; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $hasActiveTransaction = false; |
||
55 | |||
56 | /** |
||
57 | * Constructor. |
||
58 | * |
||
59 | * @param string|PDO $hostDsnOrPdo A MySQL host, a dsn or an existing PDO instance. |
||
60 | * @param string|null $username |
||
61 | * @param string|null $password |
||
62 | * @param string|null $database |
||
63 | * @param array $options |
||
64 | */ |
||
65 | 24 | public function __construct( |
|
84 | |||
85 | /** |
||
86 | */ |
||
87 | 16 | public function connect() |
|
125 | |||
126 | /** |
||
127 | * @param PDOException $exception |
||
128 | * @return bool |
||
129 | */ |
||
130 | private function isConnectionExceptionCausedByConnection(PDOException $exception) |
||
139 | |||
140 | /** |
||
141 | * Close the database connection. |
||
142 | */ |
||
143 | 1 | public function disconnect() |
|
147 | |||
148 | /** |
||
149 | */ |
||
150 | 1 | public function reconnect() |
|
155 | |||
156 | /** |
||
157 | * Check if database connection is open. |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 17 | public function isConnected() |
|
165 | |||
166 | /** |
||
167 | * Returns the PDO handle. |
||
168 | * |
||
169 | * Can be used to gain access to any special PDO methods. |
||
170 | * |
||
171 | * @return PDO |
||
172 | */ |
||
173 | 2 | public function getPdo() |
|
177 | |||
178 | /** |
||
179 | * Creates and executes a PDO statement. |
||
180 | * |
||
181 | * @param string $sql |
||
182 | * @param array $parameters |
||
183 | * @return PDOStatement |
||
184 | */ |
||
185 | 8 | protected function executeQuery($sql, array $parameters = []) |
|
203 | |||
204 | /** |
||
205 | * Execute an SQL statement and return the number of affected rows. |
||
206 | * |
||
207 | * @param string $sql |
||
208 | * @param array $parameters |
||
209 | * @return int The number of rows affected |
||
210 | */ |
||
211 | 5 | public function exec($sql, array $parameters = []) |
|
217 | |||
218 | /** |
||
219 | * Execute an SQL statement and return the first row. |
||
220 | * |
||
221 | * @param string $sql |
||
222 | * @param array $parameters |
||
223 | * @param bool $indexedKeys |
||
224 | * @return array|false |
||
225 | */ |
||
226 | 1 | public function fetchRow($sql, array $parameters = [], $indexedKeys = false) |
|
232 | |||
233 | /** |
||
234 | * Execute an SQL statement and return all rows as an array. |
||
235 | * |
||
236 | * @param string $sql |
||
237 | * @param array $parameters |
||
238 | * @param bool $indexedKeys |
||
239 | * @return array |
||
240 | */ |
||
241 | 1 | public function fetchRows($sql, array $parameters = [], $indexedKeys = false) |
|
247 | |||
248 | /** |
||
249 | * Execute an SQL statement and return the first column of the first row. |
||
250 | * |
||
251 | * @param string $sql |
||
252 | * @param array $parameters |
||
253 | * @return string|false |
||
254 | */ |
||
255 | 1 | public function fetchValue($sql, array $parameters = []) |
|
261 | |||
262 | /** |
||
263 | * Quote a value for a safe use in query (eg. bla'bla -> 'bla''bla'). |
||
264 | * |
||
265 | * @param mixed $value |
||
266 | * @return string |
||
267 | */ |
||
268 | 1 | public function quote($value) |
|
274 | |||
275 | /** |
||
276 | * Get id of the last inserted row. |
||
277 | * |
||
278 | * @return int |
||
279 | */ |
||
280 | 1 | public function getLastInsertId() |
|
286 | |||
287 | /** |
||
288 | * Prepare parameters for database use. |
||
289 | * |
||
290 | * @param array $parameters |
||
291 | * @return array |
||
292 | */ |
||
293 | 8 | protected function prepareParameters(array $parameters = []) |
|
310 | |||
311 | /** |
||
312 | * Begin transaction (turns off autocommit mode). |
||
313 | * |
||
314 | * @param bool $onlyIfNoActiveTransaction |
||
315 | * @return bool |
||
316 | */ |
||
317 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
330 | |||
331 | /** |
||
332 | * Commits current active transaction (restores autocommit mode). |
||
333 | */ |
||
334 | 2 | public function commit() |
|
341 | |||
342 | /** |
||
343 | * Rolls back current active transaction (restores autocommit mode). |
||
344 | */ |
||
345 | 2 | public function rollBack() |
|
352 | |||
353 | /** |
||
354 | * @return bool |
||
355 | */ |
||
356 | 3 | public function hasActiveTransaction() |
|
360 | |||
361 | /** |
||
362 | * @param string $table |
||
363 | * @param array $data |
||
364 | * @param bool $updateOnDuplicateKey |
||
365 | * @return int The number of rows affected |
||
366 | */ |
||
367 | 3 | public function insert($table, array $data, $updateOnDuplicateKey = false) |
|
392 | |||
393 | /** |
||
394 | * @param array $assignmentData |
||
395 | * @return string |
||
396 | */ |
||
397 | 2 | protected function getAssignmentSql(array $assignmentData) |
|
406 | |||
407 | /** |
||
408 | * @param string $table |
||
409 | * @param array $data |
||
410 | * @param string $whereSql |
||
411 | * @param array $whereParameters |
||
412 | * @return int The number of rows affected |
||
413 | */ |
||
414 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
428 | } |
||
429 |