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( |
|
66 | $hostDsnOrPdo, |
||
67 | $username = null, |
||
68 | $password = null, |
||
69 | $database = null, |
||
70 | array $options = [] |
||
71 | ) { |
||
72 | 18 | if ($hostDsnOrPdo instanceof PDO) { |
|
73 | 18 | $this->pdo = $hostDsnOrPdo; |
|
74 | } elseif (strpos($hostDsnOrPdo, ':') !== false) { |
||
75 | $this->dsn = $hostDsnOrPdo; |
||
76 | } else { |
||
77 | $this->dsn = "mysql:host={$hostDsnOrPdo}" . ($database ? ";dbname={$database}" : ''); |
||
78 | } |
||
79 | |||
80 | 18 | $this->username = $username; |
|
81 | 18 | $this->password = $password; |
|
82 | 18 | $this->options = $options; |
|
83 | 18 | } |
|
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 ms until next retry |
||
117 | usleep(100000); |
||
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 | 2003, // Can't connect to MySQL server |
||
134 | 2006, // MySQL server has gone away |
||
135 | 2013, // Lost connection to MySQL server during query |
||
136 | ]); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Close database connection. |
||
141 | */ |
||
142 | 1 | public function disconnect() |
|
146 | |||
147 | /** |
||
148 | * Check if database connection is open. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 14 | public function isConnected() |
|
156 | |||
157 | /** |
||
158 | * Returns the PDO handle. |
||
159 | * |
||
160 | * Can be used to gain access to any special PDO methods. |
||
161 | * |
||
162 | * @return PDO |
||
163 | */ |
||
164 | 2 | public function getPdo() |
|
168 | |||
169 | /** |
||
170 | * Creates and executes a PDO statement. |
||
171 | * |
||
172 | * @param string $sql |
||
173 | * @param array $parameters |
||
174 | * @return PDOStatement |
||
175 | */ |
||
176 | 5 | protected function executeQuery($sql, array $parameters = []) |
|
189 | |||
190 | /** |
||
191 | * Execute an SQL statement and return the number of affected rows. |
||
192 | * |
||
193 | * @param string $sql |
||
194 | * @param array $parameters |
||
195 | * @return int The number of rows affected |
||
196 | */ |
||
197 | 2 | public function exec($sql, array $parameters = []) |
|
203 | |||
204 | /** |
||
205 | * Execute an SQL statement and return the first row. |
||
206 | * |
||
207 | * @param string $sql |
||
208 | * @param array $parameters |
||
209 | * @param bool $indexedKeys |
||
210 | * @return array|false |
||
211 | */ |
||
212 | 1 | public function fetchRow($sql, array $parameters = [], $indexedKeys = false) |
|
218 | |||
219 | /** |
||
220 | * Execute an SQL statement and return all rows as an array. |
||
221 | * |
||
222 | * @param string $sql |
||
223 | * @param array $parameters |
||
224 | * @param bool $indexedKeys |
||
225 | * @return array |
||
226 | */ |
||
227 | 1 | public function fetchAll($sql, array $parameters = [], $indexedKeys = false) |
|
233 | |||
234 | /** |
||
235 | * Execute an SQL statement and return the first column of the first row. |
||
236 | * |
||
237 | * @param string $sql |
||
238 | * @param array $parameters |
||
239 | * @return string|false |
||
240 | */ |
||
241 | 1 | public function fetchOne($sql, array $parameters = []) |
|
247 | |||
248 | /** |
||
249 | * Quote a value for a safe use in query (eg. bla'bla -> 'bla''bla'). |
||
250 | * |
||
251 | * @param mixed $value |
||
252 | * @return string |
||
253 | */ |
||
254 | 1 | public function quote($value) |
|
260 | |||
261 | /** |
||
262 | * Get id of the last inserted row. |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | 1 | public function getLastInsertId() |
|
272 | |||
273 | /** |
||
274 | * Prepare parameters for database use. |
||
275 | * |
||
276 | * @param array $parameters |
||
277 | * @return array |
||
278 | */ |
||
279 | 4 | protected function prepareParameters(array $parameters = []) |
|
280 | { |
||
281 | 4 | foreach ($parameters as &$parameterValue) { |
|
282 | 4 | if (is_bool($parameterValue)) { |
|
283 | 4 | $parameterValue = (int) $parameterValue; |
|
284 | } |
||
285 | } |
||
286 | 4 | unset($parameterValue); |
|
287 | |||
288 | 4 | return $parameters; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Begin transaction (turns off autocommit mode). |
||
293 | * |
||
294 | * @param bool $onlyIfNoActiveTransaction |
||
295 | * @return bool |
||
296 | */ |
||
297 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
310 | |||
311 | /** |
||
312 | * Commits current active transaction (restores autocommit mode). |
||
313 | */ |
||
314 | 2 | public function commit() |
|
321 | |||
322 | /** |
||
323 | * Rolls back current active transaction (restores autocommit mode). |
||
324 | */ |
||
325 | 2 | public function rollBack() |
|
332 | |||
333 | /** |
||
334 | * @return bool |
||
335 | */ |
||
336 | 3 | public function hasActiveTransaction() |
|
340 | |||
341 | /** |
||
342 | * @param string $table |
||
343 | * @param array $data |
||
344 | * @return int The number of rows affected |
||
345 | */ |
||
346 | 1 | public function insert($table, array $data) |
|
358 | |||
359 | /** |
||
360 | * @param string $table |
||
361 | * @param array $data |
||
362 | * @param string $whereSql |
||
363 | * @param array $whereParameters |
||
364 | * @return int The number of rows affected |
||
365 | */ |
||
366 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
382 | } |
||
383 |