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 | * @var PdoFactoryInterface |
||
58 | */ |
||
59 | private $pdoFactory; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param string|PDO $hostDsnOrPdo A MySQL host, a dsn or an existing PDO instance. |
||
65 | * @param string|null $username |
||
66 | * @param string|null $password |
||
67 | * @param string|null $database |
||
68 | * @param array $options |
||
69 | * @param PdoFactoryInterface $pdoFactory |
||
70 | * |
||
71 | * @deprecated The signature of the constructor will change in version 1.0.0 and accept |
||
72 | * only a PDO dsn string as first parameter dropping support to inject a PDO instance or host string |
||
73 | */ |
||
74 | 28 | public function __construct( |
|
106 | |||
107 | /** |
||
108 | */ |
||
109 | 18 | public function connect() |
|
132 | |||
133 | /** |
||
134 | * @param PDOException $exception |
||
135 | * @return bool |
||
136 | */ |
||
137 | 2 | private function isConnectionExceptionCausedByConnection(PDOException $exception) |
|
146 | |||
147 | /** |
||
148 | * Close the database connection. |
||
149 | */ |
||
150 | 1 | public function disconnect() |
|
154 | |||
155 | /** |
||
156 | */ |
||
157 | 1 | public function reconnect() |
|
162 | |||
163 | /** |
||
164 | * Check if database connection is open. |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 19 | public function isConnected() |
|
172 | |||
173 | /** |
||
174 | * Returns the PDO handle. |
||
175 | * |
||
176 | * Can be used to gain access to any special PDO methods. |
||
177 | * |
||
178 | * @return PDO |
||
179 | */ |
||
180 | 4 | public function getPdo() |
|
184 | |||
185 | /** |
||
186 | * Creates and executes a PDO statement. |
||
187 | * |
||
188 | * @param string $sql |
||
189 | * @param array $parameters |
||
190 | * @return PDOStatement |
||
191 | */ |
||
192 | 7 | protected function executeQuery($sql, array $parameters = []) |
|
206 | |||
207 | /** |
||
208 | * Execute an SQL statement and return the number of affected rows. |
||
209 | * |
||
210 | * @param string $sql |
||
211 | * @param array $parameters |
||
212 | * @return int The number of rows affected |
||
213 | */ |
||
214 | 4 | public function exec($sql, array $parameters = []) |
|
220 | |||
221 | /** |
||
222 | * Execute an SQL statement and return the first row. |
||
223 | * |
||
224 | * @param string $sql |
||
225 | * @param array $parameters |
||
226 | * @param bool $indexedKeys |
||
227 | * @return array|false |
||
228 | */ |
||
229 | 1 | public function fetchRow($sql, array $parameters = [], $indexedKeys = false) |
|
235 | |||
236 | /** |
||
237 | * Execute an SQL statement and return all rows as an array. |
||
238 | * |
||
239 | * @param string $sql |
||
240 | * @param array $parameters |
||
241 | * @param bool $indexedKeys |
||
242 | * @return array |
||
243 | */ |
||
244 | 1 | public function fetchRows($sql, array $parameters = [], $indexedKeys = false) |
|
250 | |||
251 | /** |
||
252 | * Execute an SQL statement and return the first column of the first row. |
||
253 | * |
||
254 | * @param string $sql |
||
255 | * @param array $parameters |
||
256 | * @return string|false |
||
257 | */ |
||
258 | 1 | public function fetchValue($sql, array $parameters = []) |
|
264 | |||
265 | /** |
||
266 | * Quote a value for a safe use in query (eg. bla'bla -> 'bla''bla'). |
||
267 | * |
||
268 | * @param mixed $value |
||
269 | * @return string |
||
270 | */ |
||
271 | 1 | public function quote($value) |
|
277 | |||
278 | /** |
||
279 | * Get id of the last inserted row. |
||
280 | * |
||
281 | * @return int |
||
282 | */ |
||
283 | 1 | public function getLastInsertId() |
|
289 | |||
290 | /** |
||
291 | * Prepare parameters for database use. |
||
292 | * |
||
293 | * @param array $parameters |
||
294 | * @return array |
||
295 | */ |
||
296 | 7 | protected function prepareParameters(array $parameters = []) |
|
313 | |||
314 | /** |
||
315 | * Begin transaction (turns off autocommit mode). |
||
316 | * |
||
317 | * @param bool $onlyIfNoActiveTransaction |
||
318 | * @return bool |
||
319 | */ |
||
320 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
333 | |||
334 | /** |
||
335 | * Commits current active transaction (restores autocommit mode). |
||
336 | */ |
||
337 | 2 | public function commit() |
|
344 | |||
345 | /** |
||
346 | * Rolls back current active transaction (restores autocommit mode). |
||
347 | */ |
||
348 | 2 | public function rollBack() |
|
355 | |||
356 | /** |
||
357 | * @return bool |
||
358 | */ |
||
359 | 3 | public function hasActiveTransaction() |
|
363 | |||
364 | /** |
||
365 | * @param string $table |
||
366 | * @param array $data |
||
367 | * @param bool $updateOnDuplicateKey |
||
368 | * @return int The number of rows affected |
||
369 | */ |
||
370 | 3 | public function insert($table, array $data, $updateOnDuplicateKey = false) |
|
395 | |||
396 | /** |
||
397 | * @param array $assignmentData |
||
398 | * @return string |
||
399 | */ |
||
400 | 2 | protected function getAssignmentSql(array $assignmentData) |
|
409 | |||
410 | /** |
||
411 | * @param string $table |
||
412 | * @param array $data |
||
413 | * @param string $whereSql |
||
414 | * @param array $whereParameters |
||
415 | * @return int The number of rows affected |
||
416 | */ |
||
417 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
431 | } |
||
432 |
If you suppress an error, we recommend checking for the error condition explicitly: