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 | 18 | public function __construct( |
|
84 | |||
85 | /** |
||
86 | */ |
||
87 | 13 | public function connect() |
|
88 | { |
||
89 | 13 | if ($this->isConnected()) { |
|
90 | 13 | return; |
|
91 | } |
||
92 | |||
93 | $retries = isset($this->options['connectRetries'])? $this->options['connectRetries'] : 0; |
||
94 | do { |
||
95 | try { |
||
96 | $defaultPdoOptions = [ |
||
97 | PDO::ATTR_TIMEOUT => 5, |
||
98 | // We want emulation by default (faster for single queries). Disable if you want to |
||
99 | // use proper native prepared statements |
||
100 | PDO::ATTR_EMULATE_PREPARES => true, |
||
101 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
102 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
||
103 | ]; |
||
104 | $pdoOptions = $defaultPdoOptions + (isset($this->options['pdo']) ? $this->options['pdo'] : []); |
||
105 | |||
106 | $this->pdo = new PDO( |
||
107 | $this->dsn, |
||
108 | $this->username, |
||
109 | $this->password, |
||
110 | $pdoOptions |
||
111 | ); |
||
112 | |||
113 | return; |
||
114 | } catch (PDOException $e) { |
||
115 | if ($this->isConnectionExceptionCausedByConnection($e) && $retries > 0) { |
||
116 | // Sleep for 100 - 500 ms until next retry |
||
117 | usleep(rand(100000, 500000)); |
||
118 | } else { |
||
119 | throw new ConnectionException($e); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | } while ($retries-- > 0); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param PDOException $exception |
||
128 | * @return bool |
||
129 | */ |
||
130 | private function isConnectionExceptionCausedByConnection(PDOException $exception) |
||
131 | { |
||
132 | return in_array($exception->getCode(), [ |
||
133 | 2002, // Can't connect to MySQL server (Socket) |
||
134 | 2003, // Can't connect to MySQL server (TCP) |
||
135 | 2006, // MySQL server has gone away |
||
136 | 2013, // Lost connection to MySQL server during query |
||
137 | ]); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Close database connection. |
||
142 | */ |
||
143 | 1 | public function disconnect() |
|
147 | |||
148 | /** |
||
149 | * Check if database connection is open. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 14 | public function isConnected() |
|
157 | |||
158 | /** |
||
159 | * Returns the PDO handle. |
||
160 | * |
||
161 | * Can be used to gain access to any special PDO methods. |
||
162 | * |
||
163 | * @return PDO |
||
164 | */ |
||
165 | 2 | public function getPdo() |
|
169 | |||
170 | /** |
||
171 | * Creates and executes a PDO statement. |
||
172 | * |
||
173 | * @param string $sql |
||
174 | * @param array $parameters |
||
175 | * @return PDOStatement |
||
176 | */ |
||
177 | 5 | protected function executeQuery($sql, array $parameters = []) |
|
190 | |||
191 | /** |
||
192 | * Execute an SQL statement and return the number of affected rows. |
||
193 | * |
||
194 | * @param string $sql |
||
195 | * @param array $parameters |
||
196 | * @return int The number of rows affected |
||
197 | */ |
||
198 | 2 | public function exec($sql, array $parameters = []) |
|
204 | |||
205 | /** |
||
206 | * Execute an SQL statement and return the first row. |
||
207 | * |
||
208 | * @param string $sql |
||
209 | * @param array $parameters |
||
210 | * @param bool $indexedKeys |
||
211 | * @return array|false |
||
212 | */ |
||
213 | 1 | public function fetchRow($sql, array $parameters = [], $indexedKeys = false) |
|
219 | |||
220 | /** |
||
221 | * Execute an SQL statement and return all rows as an array. |
||
222 | * |
||
223 | * @param string $sql |
||
224 | * @param array $parameters |
||
225 | * @param bool $indexedKeys |
||
226 | * @return array |
||
227 | */ |
||
228 | 1 | public function fetchAll($sql, array $parameters = [], $indexedKeys = false) |
|
234 | |||
235 | /** |
||
236 | * Execute an SQL statement and return the first column of the first row. |
||
237 | * |
||
238 | * @param string $sql |
||
239 | * @param array $parameters |
||
240 | * @return string|false |
||
241 | */ |
||
242 | 1 | public function fetchOne($sql, array $parameters = []) |
|
248 | |||
249 | /** |
||
250 | * Quote a value for a safe use in query (eg. bla'bla -> 'bla''bla'). |
||
251 | * |
||
252 | * @param mixed $value |
||
253 | * @return string |
||
254 | */ |
||
255 | 1 | public function quote($value) |
|
261 | |||
262 | /** |
||
263 | * Get id of the last inserted row. |
||
264 | * |
||
265 | * @return int |
||
266 | */ |
||
267 | 1 | public function getLastInsertId() |
|
273 | |||
274 | /** |
||
275 | * Prepare parameters for database use. |
||
276 | * |
||
277 | * @param array $parameters |
||
278 | * @return array |
||
279 | */ |
||
280 | 4 | protected function prepareParameters(array $parameters = []) |
|
281 | { |
||
282 | 4 | foreach ($parameters as &$parameterValue) { |
|
283 | 4 | if (is_bool($parameterValue)) { |
|
284 | 4 | $parameterValue = (int) $parameterValue; |
|
285 | } |
||
286 | } |
||
287 | 4 | unset($parameterValue); |
|
288 | |||
289 | 4 | return $parameters; |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * Begin transaction (turns off autocommit mode). |
||
294 | * |
||
295 | * @param bool $onlyIfNoActiveTransaction |
||
296 | * @return bool |
||
297 | */ |
||
298 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
311 | |||
312 | /** |
||
313 | * Commits current active transaction (restores autocommit mode). |
||
314 | */ |
||
315 | 2 | public function commit() |
|
322 | |||
323 | /** |
||
324 | * Rolls back current active transaction (restores autocommit mode). |
||
325 | */ |
||
326 | 2 | public function rollBack() |
|
333 | |||
334 | /** |
||
335 | * @return bool |
||
336 | */ |
||
337 | 3 | public function hasActiveTransaction() |
|
341 | |||
342 | /** |
||
343 | * @param string $table |
||
344 | * @param array $data |
||
345 | * @return int The number of rows affected |
||
346 | */ |
||
347 | 1 | public function insert($table, array $data) |
|
359 | |||
360 | /** |
||
361 | * @param string $table |
||
362 | * @param array $data |
||
363 | * @param string $whereSql |
||
364 | * @param array $whereParameters |
||
365 | * @return int The number of rows affected |
||
366 | */ |
||
367 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
383 | } |
||
384 |