Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Database |
||
16 | { |
||
17 | /** |
||
18 | * @var Connector |
||
19 | */ |
||
20 | protected $connector; |
||
21 | |||
22 | /** |
||
23 | * @var \PDO |
||
24 | */ |
||
25 | protected $pdo; |
||
26 | |||
27 | /** |
||
28 | * @var \PDOStatement |
||
29 | */ |
||
30 | protected $statement; |
||
31 | |||
32 | /** |
||
33 | * @var Query |
||
34 | */ |
||
35 | protected $query; |
||
36 | |||
37 | /** |
||
38 | * @var Grammar |
||
39 | */ |
||
40 | protected $grammar; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $driver = [ |
||
46 | "mysql" => [ |
||
47 | "connector" => ConnectorMySql::class, |
||
48 | "grammar" => GrammarMySql::class, |
||
49 | ] |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $config = [ |
||
56 | "driver" => "mysql", |
||
57 | |||
58 | "host" => "", |
||
59 | "username" => "", |
||
60 | "password" => "", |
||
61 | |||
62 | "dbname" => "", |
||
63 | "options" => [ |
||
64 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
65 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC |
||
66 | ] |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $model = null; |
||
73 | |||
74 | /** |
||
75 | * @param $config |
||
76 | */ |
||
77 | public function __construct($config) |
||
90 | |||
91 | /** |
||
92 | * @param $config |
||
93 | * @return static |
||
94 | */ |
||
95 | public static function create($config) |
||
99 | |||
100 | /** |
||
101 | * |
||
102 | */ |
||
103 | protected function initialize() |
||
113 | |||
114 | /** |
||
115 | * @return PDO |
||
116 | */ |
||
117 | public function createConnection() |
||
122 | |||
123 | /** |
||
124 | * @param $query |
||
125 | * @param null $params |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function sql($query, $params = null) |
||
133 | |||
134 | /** |
||
135 | * @param $table |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function table($table) |
||
143 | |||
144 | /** |
||
145 | * @param null $columns |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function select($columns = null) |
||
153 | |||
154 | /** |
||
155 | * @param array $columnsVal |
||
156 | * @param bool $runExecute |
||
157 | * @return $this|mixed |
||
158 | */ |
||
159 | public function insert(array $columnsVal, $runExecute = false) |
||
172 | |||
173 | /** |
||
174 | * @param array $columnsVal |
||
175 | * @param null $primaryKey |
||
176 | * @return mixed |
||
177 | */ |
||
178 | public function update(array $columnsVal, $primaryKey = null) |
||
196 | |||
197 | /** |
||
198 | * @param null $primaryKey |
||
199 | * @param null $primaryKeyVal |
||
200 | * @return mixed |
||
201 | */ |
||
202 | public function delete($primaryKey = null, $primaryKeyVal = null) |
||
216 | |||
217 | /** |
||
218 | * @param $query |
||
219 | * @param string $type |
||
220 | * @return Query |
||
221 | */ |
||
222 | public function whereQ($query, $type = "AND") |
||
227 | |||
228 | /** |
||
229 | * @param $column |
||
230 | * @param $value |
||
231 | * @param string $operator |
||
232 | * @param string $type |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function where($column, $value, $operator = "=", $type = "AND") |
||
240 | |||
241 | /** |
||
242 | * @param $column |
||
243 | * @param $value |
||
244 | * @param string $operator |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function andWhere($column, $value, $operator = "=") |
||
252 | |||
253 | /** |
||
254 | * @param $column |
||
255 | * @param $value |
||
256 | * @param string $operator |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function orWhere($column, $value, $operator = "=") |
||
264 | |||
265 | /** |
||
266 | * @param $column |
||
267 | * @param $value |
||
268 | * @param string $type |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function like($column, $value, $type = "AND") |
||
276 | |||
277 | /** |
||
278 | * @param $column |
||
279 | * @param $value |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function andLike($column, $value) |
||
287 | |||
288 | /** |
||
289 | * @param $column |
||
290 | * @param $value |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function orLike($column, $value) |
||
298 | |||
299 | /** |
||
300 | * @param $column |
||
301 | * @param $value |
||
302 | * @param string $type |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function between($column, $value, $type = "AND") |
||
310 | |||
311 | /** |
||
312 | * @param $column |
||
313 | * @param $value |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function andBetween($column, $value) |
||
321 | |||
322 | /** |
||
323 | * @param $column |
||
324 | * @param $value |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function orBetween($column, $value) |
||
332 | |||
333 | /** |
||
334 | * @param $column |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function asc($column) |
||
342 | |||
343 | /** |
||
344 | * @param $column |
||
345 | * @return $this |
||
346 | */ |
||
347 | public function desc($column) |
||
352 | |||
353 | /** |
||
354 | * @param $num |
||
355 | * @param null $offset |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function limit($num, $offset = null) |
||
363 | |||
364 | /** |
||
365 | * @param bool $returnThis |
||
366 | * @return mixed |
||
367 | */ |
||
368 | public function execute($returnThis = true) |
||
377 | |||
378 | /** |
||
379 | * @return array |
||
380 | * @throws \Exception |
||
381 | */ |
||
382 | public function all() |
||
402 | |||
403 | /** |
||
404 | * @return mixed |
||
405 | */ |
||
406 | public function first() |
||
412 | |||
413 | /** |
||
414 | * @return mixed |
||
415 | * @throws \Exception |
||
416 | */ |
||
417 | protected function get() |
||
428 | |||
429 | /** |
||
430 | * @throws \Exception |
||
431 | */ |
||
432 | protected function executeIfNotAlreadyExecutedPreviously() |
||
438 | |||
439 | /** |
||
440 | * Clear statement PDO and query builder |
||
441 | */ |
||
442 | public function clear() |
||
450 | |||
451 | /** |
||
452 | * |
||
453 | */ |
||
454 | public function clearUsingModel() |
||
458 | |||
459 | /** |
||
460 | * @return string |
||
461 | */ |
||
462 | public function getModel() |
||
466 | |||
467 | /** |
||
468 | * @param string $model |
||
469 | * @param null $table |
||
470 | * @throws \Exception |
||
471 | */ |
||
472 | public function setModel($model, $table = null) |
||
483 | |||
484 | /** |
||
485 | * @return string |
||
486 | */ |
||
487 | public function lastInsertId() |
||
491 | } |
||
492 |