Complex classes like _Abstract 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 _Abstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | abstract class _Abstract { |
||
10 | /** |
||
11 | * Is connection established |
||
12 | * |
||
13 | * @var bool |
||
14 | */ |
||
15 | protected $connected = false; |
||
16 | /** |
||
17 | * DB type, may be used for constructing requests, accounting particular features of current DB (lowercase name) |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $db_type = ''; |
||
22 | /** |
||
23 | * Current DB |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $database; |
||
28 | /** |
||
29 | * Current prefix |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $prefix; |
||
34 | /** |
||
35 | * Total time of requests execution |
||
36 | * |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $time; |
||
40 | /** |
||
41 | * Array for storing of data of the last executed request |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $query = [ |
||
46 | 'time' => '', |
||
47 | 'text' => '' |
||
48 | ]; |
||
49 | /** |
||
50 | * Array for storing data of all executed requests |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $queries = [ |
||
55 | 'num' => '', |
||
56 | 'time' => [], |
||
57 | 'text' => [] |
||
58 | ]; |
||
59 | /** |
||
60 | * Connection time |
||
61 | * |
||
62 | * @var float |
||
63 | */ |
||
64 | protected $connecting_time; |
||
65 | /** |
||
66 | * Asynchronous request |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $async = false; |
||
71 | /** |
||
72 | * Connecting to the DB |
||
73 | * |
||
74 | * @param string $database |
||
75 | * @param string $user |
||
76 | * @param string $password |
||
77 | * @param string $host |
||
78 | * @param string $charset |
||
79 | * @param string $prefix |
||
80 | */ |
||
81 | abstract function __construct ($database, $user = '', $password = '', $host = 'localhost', $charset = 'utf8', $prefix = ''); |
||
82 | /** |
||
83 | * Query |
||
84 | * |
||
85 | * SQL request into DB |
||
86 | * |
||
87 | * @abstract |
||
88 | * |
||
89 | * @param string|string[] $query SQL query string or array, may be a format string in accordance with the first parameter of sprintf() function |
||
90 | * @param string|string[] $params May be array of arguments for formatting of <b>$query</b><br> |
||
91 | * or string - in this case it will be first argument for formatting of <b>$query</b> |
||
92 | * @param string[] $param if <b>$params</b> is string - this parameter will be second argument for formatting of <b>$query</b>. |
||
93 | * If you need more arguments - add them after this one, function will accept them. |
||
94 | * |
||
95 | * @return false|object|resource |
||
96 | */ |
||
97 | function q ($query, $params = [], ...$param) { |
||
98 | $normalized = $this->prepare_and_normalize_arguments($query, func_get_args()); |
||
99 | if (!$normalized) { |
||
100 | return false; |
||
101 | } |
||
102 | list($query, $params) = $normalized; |
||
103 | /** |
||
104 | * Executing multiple queries |
||
105 | */ |
||
106 | if (is_array($query)) { |
||
107 | return $this->execute_multiple($query, $params); |
||
108 | } |
||
109 | return $this->execute_single($query, $params); |
||
110 | } |
||
111 | /** |
||
112 | * @param string|string[] $query |
||
113 | * @param array $arguments |
||
114 | * |
||
115 | * @return array|false |
||
1 ignored issue
–
show
|
|||
116 | */ |
||
117 | protected function prepare_and_normalize_arguments ($query, $arguments) { |
||
118 | if (!$query || !$arguments) { |
||
119 | return false; |
||
120 | } |
||
121 | $query = str_replace('[prefix]', $this->prefix, $query); |
||
122 | switch (count($arguments)) { |
||
123 | default: |
||
124 | $params = array_slice($arguments, 1); |
||
125 | break; |
||
126 | case 1: |
||
127 | $params = []; |
||
128 | break; |
||
129 | case 2: |
||
130 | $params = (array)$arguments[1]; |
||
131 | break; |
||
132 | } |
||
133 | foreach ($params as &$param) { |
||
134 | $param = $this->s($param, false); |
||
135 | } |
||
136 | return [ |
||
137 | $query, |
||
138 | $params |
||
139 | ]; |
||
140 | } |
||
141 | /** |
||
142 | * @param string[] $queries |
||
143 | * @param string[] $params |
||
144 | * |
||
145 | * @return false|object|resource |
||
146 | */ |
||
147 | protected function execute_multiple ($queries, $params) { |
||
148 | $time_from = microtime(true); |
||
149 | foreach ($queries as &$q) { |
||
150 | $q = $params ? vsprintf($q, $params) : $q; |
||
151 | } |
||
152 | unset($q); |
||
153 | $this->queries['num'] += count($queries); |
||
154 | $result = $this->q_multi_internal($queries); |
||
155 | $this->time += round(microtime(true) - $time_from, 6); |
||
156 | return $result; |
||
157 | } |
||
158 | /** |
||
159 | * @param string $query |
||
160 | * @param string[] $params |
||
161 | * |
||
162 | * @return false|object|resource |
||
163 | */ |
||
164 | protected function execute_single ($query, $params) { |
||
179 | /** |
||
180 | * Asynchronous, Query |
||
181 | * |
||
182 | * Asynchronous SQL request into DB (if is not supported - ordinary request will me executed). |
||
183 | * Result of execution can't be obtained, so, use it, for example, for deleting some non-critical data |
||
184 | * |
||
185 | * @abstract |
||
186 | * |
||
187 | * @param string|string[] $query SQL query string, may be a format string in accordance with the first parameter of sprintf() function |
||
188 | * @param string|string[] $params May be array of arguments for formatting of <b>$query</b><br> |
||
189 | * or string - in this case it will be first argument for formatting of <b>$query</b> |
||
190 | * @param string[] $param if <b>$params</b> is string - this parameter will be second argument for formatting of <b>$query</b>. |
||
191 | * If you need more arguments - add them after this one, function will accept them. |
||
192 | * |
||
193 | * @return false|object|resource |
||
194 | */ |
||
195 | function aq ($query, $params = [], ...$param) { |
||
196 | $this->async = true; |
||
197 | $result = $this->q($query, $params, ...$param); |
||
198 | $this->async = false; |
||
199 | return $result; |
||
200 | } |
||
201 | /** |
||
202 | * SQL request to DB |
||
203 | * |
||
204 | * @abstract |
||
205 | * |
||
206 | * @param string|string[] $query |
||
207 | * |
||
208 | * @return false|object|resource |
||
209 | */ |
||
210 | abstract protected function q_internal ($query); |
||
211 | /** |
||
212 | * Multiple SQL request to DB |
||
213 | * |
||
214 | * @abstract |
||
215 | * |
||
216 | * @param string[] $query |
||
217 | * |
||
218 | * @return false|object|resource |
||
219 | */ |
||
220 | abstract protected function q_multi_internal ($query); |
||
221 | /** |
||
222 | * Number |
||
223 | * |
||
224 | * Getting number of selected rows |
||
225 | * |
||
226 | * @deprecated |
||
227 | * @todo remove after 4.x release |
||
228 | * |
||
229 | * @abstract |
||
230 | * |
||
231 | * @param object|resource $query_result |
||
232 | * |
||
233 | * @return false|int |
||
234 | */ |
||
235 | abstract function n ($query_result); |
||
236 | /** |
||
237 | * Fetch |
||
238 | * |
||
239 | * Fetch a result row as an associative array |
||
240 | * |
||
241 | * @abstract |
||
242 | * |
||
243 | * @param false|object|resource $query_result |
||
244 | * @param bool $single_column If <b>true</b> function will return not array with one element, but directly its value |
||
245 | * @param bool $array If <b>true</b> returns array of associative arrays of all fetched rows |
||
246 | * @param bool $indexed If <b>false</b> - associative array will be returned |
||
247 | * |
||
248 | * @return array[]|false|int|int[]|string|string[] |
||
249 | */ |
||
250 | abstract function f ($query_result, $single_column = false, $array = false, $indexed = false); |
||
251 | /** |
||
252 | * Fetch, Array |
||
253 | * |
||
254 | * Similar to ::f() method, with parameter <b>$array</b> = true |
||
255 | * |
||
256 | * @deprecated |
||
257 | * @todo remove after 4.x release |
||
258 | * |
||
259 | * @param false|object|resource $query_result |
||
260 | * @param bool $single_column If <b>true</b> function will return not array with one element, but directly its value |
||
261 | * @param bool $indexed If <b>false</b> - associative array will be returned |
||
262 | * |
||
263 | * @return array[]|false |
||
264 | */ |
||
265 | function fa ($query_result, $single_column = false, $indexed = false) { |
||
268 | /** |
||
269 | * Fetch, Single |
||
270 | * |
||
271 | * Similar to ::f() method, with parameter <b>$single_column</b> = true |
||
272 | * |
||
273 | * @deprecated |
||
274 | * @todo remove after 4.x release |
||
275 | * |
||
276 | * @param false|object|resource $query_result |
||
277 | * @param bool $array If <b>true</b> returns array of associative arrays of all fetched rows |
||
278 | * |
||
279 | * @return false|int|int[]|string|string[] |
||
280 | */ |
||
281 | function fs ($query_result, $array = false) { |
||
284 | /** |
||
285 | * Fetch, Array, Single |
||
286 | * |
||
287 | * Combination of ::fa() and ::fs() methods |
||
288 | * |
||
289 | * @deprecated |
||
290 | * @todo remove after 4.x release |
||
291 | * |
||
292 | * @param false|object|resource $query_result |
||
293 | * |
||
294 | * @return false|int[]|string[] |
||
295 | */ |
||
296 | function fas ($query_result) { |
||
299 | /** |
||
300 | * Query, Fetch |
||
301 | * |
||
302 | * Combination of ::q() and ::f() methods |
||
303 | * |
||
304 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
1 ignored issue
–
show
|
|||
305 | * |
||
306 | * @return array|false |
||
1 ignored issue
–
show
|
|||
307 | */ |
||
308 | function qf (...$query) { |
||
327 | /** |
||
328 | * Query, Fetch, Array |
||
329 | * |
||
330 | * Short for ::f(::q(), false, true, false) |
||
331 | * |
||
332 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
1 ignored issue
–
show
|
|||
333 | * |
||
334 | * @return array[]|false |
||
1 ignored issue
–
show
|
|||
335 | */ |
||
336 | function qfa (...$query) { |
||
351 | /** |
||
352 | * Query, Fetch, Single |
||
353 | * |
||
354 | * Short for ::f(::q(), true, false, false) |
||
355 | * |
||
356 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
1 ignored issue
–
show
|
|||
357 | * |
||
358 | * @return false|int|string |
||
1 ignored issue
–
show
|
|||
359 | */ |
||
360 | function qfs (...$query) { |
||
371 | /** |
||
372 | * Query, Fetch, Array, Single |
||
373 | * |
||
374 | * Short for ::f(::q(), true, true, false) |
||
375 | * |
||
376 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
1 ignored issue
–
show
|
|||
377 | * |
||
378 | * @return false|int[]|string[] |
||
1 ignored issue
–
show
|
|||
379 | */ |
||
380 | function qfas (...$query) { |
||
383 | /** |
||
384 | * Method for simplified inserting of several rows |
||
385 | * |
||
386 | * @param string $query |
||
387 | * @param array|array[] $params Array of array of parameters for inserting |
||
388 | * @param bool $join If true - inserting of several rows will be combined in one query. For this, be sure, that your query has keyword |
||
389 | * <i>VALUES</i> in uppercase. Part of query after this keyword will be multiplied with coma separator. |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | function insert ($query, $params, $join = true) { |
||
422 | /** |
||
423 | * Id |
||
424 | * |
||
425 | * Get id of last inserted row |
||
426 | * |
||
427 | * @abstract |
||
428 | * |
||
429 | * @return int |
||
430 | */ |
||
431 | abstract function id (); |
||
432 | /** |
||
433 | * Affected |
||
434 | * |
||
435 | * Get number of affected rows during last query |
||
436 | * |
||
437 | * @abstract |
||
438 | * |
||
439 | * @return int |
||
440 | */ |
||
441 | abstract function affected (); |
||
442 | /** |
||
443 | * Free result memory |
||
444 | * |
||
445 | * @abstract |
||
446 | * |
||
447 | * @param object|resource $query_result |
||
448 | */ |
||
449 | abstract function free ($query_result); |
||
450 | /** |
||
451 | * Get columns list of table |
||
452 | * |
||
453 | * @param string $table |
||
454 | * @param false|string $like |
||
455 | * |
||
456 | * @return string[] |
||
457 | */ |
||
458 | function columns ($table, $like = false) { |
||
473 | /** |
||
474 | * Get tables list |
||
475 | * |
||
476 | * @param false|string $like |
||
477 | * |
||
478 | * @return string[] |
||
479 | */ |
||
480 | function tables ($like = false) { |
||
488 | /** |
||
489 | * Safe |
||
490 | * |
||
491 | * Preparing string for using in SQL query |
||
492 | * SQL Injection Protection |
||
493 | * |
||
494 | * @param string|string[] $string |
||
495 | * @param bool $single_quotes_around |
||
496 | * |
||
497 | * @return string|string[] |
||
498 | */ |
||
499 | function s ($string, $single_quotes_around = true) { |
||
508 | /** |
||
509 | * Preparing string for using in SQL query |
||
510 | * SQL Injection Protection |
||
511 | * |
||
512 | * @param string $string |
||
513 | * @param bool $single_quotes_around |
||
514 | * |
||
515 | * @return string |
||
516 | */ |
||
517 | abstract protected function s_internal ($string, $single_quotes_around); |
||
518 | /** |
||
519 | * Get information about server |
||
520 | * |
||
521 | * @return string |
||
522 | */ |
||
523 | abstract function server (); |
||
524 | /** |
||
525 | * Connection state |
||
526 | * |
||
527 | * @return bool |
||
528 | */ |
||
529 | function connected () { |
||
532 | /** |
||
533 | * Database type (lowercase, for example <i>mysql</i>) |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | function db_type () { |
||
540 | /** |
||
541 | * Database name |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | function database () { |
||
548 | /** |
||
549 | * Queries array, has 3 properties:<ul> |
||
550 | * <li>num - total number of performed queries |
||
551 | * <li>time - array with time of each query execution |
||
552 | * <li>text - array with text text of each query |
||
553 | * |
||
554 | * @return array |
||
555 | */ |
||
556 | function queries () { |
||
559 | /** |
||
560 | * Last query information, has 2 properties:<ul> |
||
561 | * <li>time - execution time |
||
562 | * <li>text - query text |
||
563 | * |
||
564 | * @return array |
||
565 | */ |
||
566 | function query () { |
||
569 | /** |
||
570 | * Total working time (including connection, queries execution and other delays) |
||
571 | * |
||
572 | * @return int |
||
573 | */ |
||
574 | function time () { |
||
577 | /** |
||
578 | * Connecting time |
||
579 | * |
||
580 | * @return float |
||
581 | */ |
||
582 | function connecting_time () { |
||
585 | /** |
||
586 | * Cloning restriction |
||
587 | * |
||
588 | * @final |
||
589 | */ |
||
590 | final function __clone () { |
||
592 | /** |
||
593 | * Disconnecting from DB |
||
594 | */ |
||
595 | abstract function __destruct (); |
||
596 | } |
||
597 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.