Complex classes like MySQL 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 MySQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class MySQL implements Database { |
||
19 | /** @var array */ |
||
20 | private static $tableFields = array(); |
||
21 | /** @var PDO */ |
||
22 | private $pdo; |
||
23 | /** @var bool */ |
||
24 | private $outerTransaction = false; |
||
25 | /** @var AliasRegistry */ |
||
26 | private $aliasRegistry; |
||
27 | /** @var int */ |
||
28 | private $transactionLevel = 0; |
||
29 | /** @var QueryLoggers */ |
||
30 | private $queryLoggers = 0; |
||
31 | /** @var MySQLExceptionInterpreter */ |
||
32 | private $exceptionInterpreter = 0; |
||
33 | |||
34 | /** |
||
35 | * @param PDO $pdo |
||
36 | */ |
||
37 | public function __construct(PDO $pdo) { |
||
38 | if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
||
39 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
40 | } |
||
41 | $this->pdo = $pdo; |
||
42 | $this->aliasRegistry = new AliasRegistry(); |
||
43 | $this->queryLoggers = new QueryLoggers(); |
||
44 | $this->exceptionInterpreter = new MySQLExceptionInterpreter(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return QueryLoggers |
||
49 | */ |
||
50 | public function getQueryLoggers() { |
||
53 | |||
54 | /** |
||
55 | * @return AliasRegistry |
||
56 | */ |
||
57 | public function getAliasRegistry() { |
||
60 | |||
61 | /** |
||
62 | * @param string $query |
||
63 | * @throws Exception |
||
64 | * @return QueryStatement |
||
65 | */ |
||
66 | public function query($query) { |
||
72 | |||
73 | /** |
||
74 | * @param string $query |
||
75 | * @throws Exception |
||
76 | * @return QueryStatement |
||
77 | */ |
||
78 | public function prepare($query) { |
||
84 | |||
85 | /** |
||
86 | * @param string $query |
||
87 | * @param array $params |
||
88 | * @return int |
||
89 | */ |
||
90 | public function exec($query, array $params = array()) { |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getLastInsertId() { |
||
108 | |||
109 | /** |
||
110 | * @param string $table |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getTableFields($table) { |
||
124 | |||
125 | /** |
||
126 | * @param mixed $expression |
||
127 | * @param array $arguments |
||
128 | * @return string |
||
129 | */ |
||
130 | public function quoteExpression($expression, array $arguments = array()) { |
||
146 | |||
147 | /** |
||
148 | * @param mixed $value |
||
149 | * @return string |
||
150 | */ |
||
151 | public function quote($value) { |
||
163 | |||
164 | /** |
||
165 | * @param string $field |
||
166 | * @return string |
||
167 | */ |
||
168 | public function quoteField($field) { |
||
178 | |||
179 | /** |
||
180 | * @param array $fields |
||
181 | * @return RunnableSelect |
||
182 | */ |
||
183 | public function select(array $fields = null) { |
||
190 | |||
191 | /** |
||
192 | * @param array $fields |
||
193 | * @return Builder\RunnableInsert |
||
194 | */ |
||
195 | public function insert(array $fields = null) { |
||
202 | |||
203 | /** |
||
204 | * @param array $fields |
||
205 | * @return Builder\RunnableUpdate |
||
206 | */ |
||
207 | public function update(array $fields = null) { |
||
214 | |||
215 | /** |
||
216 | * @return Builder\RunnableDelete |
||
217 | */ |
||
218 | public function delete() { |
||
221 | |||
222 | /** |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function transactionStart() { |
||
236 | |||
237 | /** |
||
238 | * @return $this |
||
239 | * @throws \Exception |
||
240 | */ |
||
241 | public function transactionCommit() { |
||
246 | |||
247 | /** |
||
248 | * @return $this |
||
249 | * @throws \Exception |
||
250 | */ |
||
251 | public function transactionRollback() { |
||
256 | |||
257 | /** |
||
258 | * @param int|callable $tries |
||
259 | * @param callable|null $callback |
||
260 | * @return mixed |
||
261 | * @throws \Exception |
||
262 | * @throws null |
||
263 | */ |
||
264 | public function transaction($tries = 1, $callback = null) { |
||
284 | |||
285 | /** |
||
286 | * @param callable $fn |
||
287 | * @return $this |
||
288 | * @throws \Exception |
||
289 | */ |
||
290 | private function transactionEnd($fn) { |
||
304 | |||
305 | /** |
||
306 | * @param string $query |
||
307 | * @param callable $fn |
||
308 | * @return QueryStatement |
||
309 | * @throws Exception |
||
310 | */ |
||
311 | private function buildQueryStatement($query, $fn) { |
||
319 | |||
320 | /** |
||
321 | * @param callable $fn |
||
322 | * @return mixed |
||
323 | */ |
||
324 | private function exceptionHandler($fn) { |
||
331 | } |
||
332 |