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 |
||
| 12 | abstract class _Abstract { |
||
| 13 | /** |
||
| 14 | * Is connection established |
||
| 15 | * |
||
| 16 | * @var bool |
||
| 17 | */ |
||
| 18 | protected $connected = false; |
||
| 19 | /** |
||
| 20 | * DB type, may be used for constructing requests, accounting particular features of current DB (lowercase name) |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $db_type = ''; |
||
| 25 | /** |
||
| 26 | * Current DB |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $database; |
||
| 31 | /** |
||
| 32 | * Current prefix |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $prefix; |
||
| 37 | /** |
||
| 38 | * Total time of requests execution |
||
| 39 | * |
||
| 40 | * @var float |
||
| 41 | */ |
||
| 42 | protected $time; |
||
| 43 | /** |
||
| 44 | * Array for storing of data of the last executed request |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $query = [ |
||
| 49 | 'time' => 0, |
||
| 50 | 'text' => '' |
||
| 51 | ]; |
||
| 52 | /** |
||
| 53 | * Array for storing data of all executed requests |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $queries = [ |
||
| 58 | 'num' => 0, |
||
| 59 | 'time' => [], |
||
| 60 | 'text' => [] |
||
| 61 | ]; |
||
| 62 | /** |
||
| 63 | * Connection time |
||
| 64 | * |
||
| 65 | * @var float |
||
| 66 | */ |
||
| 67 | protected $connecting_time; |
||
| 68 | /** |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $in_transaction = false; |
||
| 72 | /** |
||
| 73 | * Connecting to the DB |
||
| 74 | * |
||
| 75 | * @param string $database |
||
| 76 | * @param string $user |
||
| 77 | * @param string $password |
||
| 78 | * @param string $host |
||
| 79 | * @param string $prefix |
||
| 80 | */ |
||
| 81 | abstract public function __construct ($database, $user = '', $password = '', $host = 'localhost', $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 or may |
||
| 90 | * contain markers for prepared statements (but not both at the same time) |
||
| 91 | * @param array $parameters There might be arbitrary number of parameters for formatting SQL statement or for using in prepared statements.<br> |
||
|
1 ignored issue
–
show
|
|||
| 92 | * If an array provided as second argument - its items will be used, so that you can either specify parameters as an |
||
| 93 | * array, or in line. |
||
| 94 | * |
||
| 95 | * @return bool|object|resource |
||
| 96 | */ |
||
| 97 | 44 | public function q ($query, ...$parameters) { |
|
| 111 | /** |
||
| 112 | * @param string|string[] $query |
||
| 113 | * @param array $parameters |
||
| 114 | * |
||
| 115 | * @return array|false |
||
|
1 ignored issue
–
show
|
|||
| 116 | */ |
||
| 117 | 44 | protected function normalize_parameters ($query, $parameters) { |
|
| 131 | /** |
||
| 132 | * @param string[] $queries |
||
| 133 | * @param string[] $parameters |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | 12 | protected function execute_multiple ($queries, $parameters) { |
|
| 155 | /** |
||
| 156 | * @param string $query |
||
| 157 | * @param string[] $parameters |
||
| 158 | * |
||
| 159 | * @return array |
||
|
1 ignored issue
–
show
|
|||
| 160 | */ |
||
| 161 | 44 | protected function prepare_query_and_parameters ($query, $parameters) { |
|
| 170 | /** |
||
| 171 | * @param string $query |
||
| 172 | * @param string[] $parameters |
||
| 173 | * |
||
| 174 | * @return false|object|resource |
||
| 175 | */ |
||
| 176 | 44 | protected function execute_single ($query, $parameters) { |
|
| 192 | /** |
||
| 193 | * SQL request to DB |
||
| 194 | * |
||
| 195 | * @abstract |
||
| 196 | * |
||
| 197 | * @param string $query |
||
| 198 | * @param string[] $parameters If not empty, than server-side prepared statements should be used |
||
| 199 | * |
||
| 200 | * @return false|object|resource |
||
| 201 | */ |
||
| 202 | abstract protected function q_internal ($query, $parameters = []); |
||
| 203 | /** |
||
| 204 | * Multiple SQL request to DB |
||
| 205 | * |
||
| 206 | * @abstract |
||
| 207 | * |
||
| 208 | * @param string[] $query |
||
| 209 | * @param string[] $parameters If not empty, than server-side prepared statements should be used |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 12 | protected function q_multi_internal ($query, $parameters = []) { |
|
| 220 | /** |
||
| 221 | * Fetch |
||
| 222 | * |
||
| 223 | * Fetch a result row as an associative array |
||
| 224 | * |
||
| 225 | * @abstract |
||
| 226 | * |
||
| 227 | * @param false|object|resource $query_result |
||
| 228 | * @param bool $single_column If <b>true</b> function will return not array with one element, but directly its value |
||
| 229 | * @param bool $array If <b>true</b> returns array of associative arrays of all fetched rows |
||
| 230 | * @param bool $indexed If <b>false</b> - associative array will be returned |
||
| 231 | * |
||
| 232 | * @return array[]|false|int|int[]|string|string[] |
||
| 233 | */ |
||
| 234 | abstract public function f ($query_result, $single_column = false, $array = false, $indexed = false); |
||
| 235 | /** |
||
| 236 | * Query, Fetch |
||
| 237 | * |
||
| 238 | * Short for `::f(::q())`, arguments are exactly the same as in `::q()` |
||
| 239 | * |
||
| 240 | * @param string[] $query |
||
| 241 | * |
||
| 242 | * @return array|false |
||
| 243 | */ |
||
| 244 | 38 | public function qf (...$query) { |
|
| 247 | /** |
||
| 248 | * Query, Fetch, Array |
||
| 249 | * |
||
| 250 | * Short for `::f(::q(), false, true)`, arguments are exactly the same as in `::q()` |
||
| 251 | * |
||
| 252 | * @param string[] $query |
||
| 253 | * |
||
| 254 | * @return array[]|false |
||
| 255 | */ |
||
| 256 | 19 | public function qfa (...$query) { |
|
| 259 | /** |
||
| 260 | * Query, Fetch, Single |
||
| 261 | * |
||
| 262 | * Short for `::f(::q(), true)`, arguments are exactly the same as in `::q()` |
||
| 263 | * |
||
| 264 | * @param string[] $query |
||
| 265 | * |
||
| 266 | * @return false|int|string |
||
| 267 | */ |
||
| 268 | 26 | public function qfs (...$query) { |
|
| 271 | /** |
||
| 272 | * Query, Fetch, Array, Single |
||
| 273 | * |
||
| 274 | * Short for `::f(::q(), true, true)`, arguments are exactly the same as in `::q()` |
||
| 275 | * |
||
| 276 | * @param string[] $query |
||
| 277 | * |
||
| 278 | * @return false|int[]|string[] |
||
| 279 | */ |
||
| 280 | 31 | public function qfas (...$query) { |
|
| 283 | /** |
||
| 284 | * Method for simplified inserting of several rows |
||
| 285 | * |
||
| 286 | * @param string $query |
||
| 287 | * @param array|array[] $parameters Array of array of parameters for inserting |
||
| 288 | * @param bool $join If true - inserting of several rows will be combined in one query. For this, be sure, that your query has keyword |
||
| 289 | * <i>VALUES</i> in uppercase. Part of query after this keyword will be multiplied with coma separator. |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 26 | public function insert ($query, $parameters, $join = true) { |
|
| 319 | /** |
||
| 320 | * Id |
||
| 321 | * |
||
| 322 | * Get id of last inserted row |
||
| 323 | * |
||
| 324 | * @abstract |
||
| 325 | * |
||
| 326 | * @return int |
||
| 327 | */ |
||
| 328 | abstract public function id (); |
||
| 329 | /** |
||
| 330 | * Affected |
||
| 331 | * |
||
| 332 | * Get number of affected rows during last query |
||
| 333 | * |
||
| 334 | * @abstract |
||
| 335 | * |
||
| 336 | * @return int |
||
| 337 | */ |
||
| 338 | abstract public function affected (); |
||
| 339 | /** |
||
| 340 | * Execute transaction |
||
| 341 | * |
||
| 342 | * All queries done inside callback will be within single transaction, throwing any exception or returning boolean `false` from callback will cause |
||
| 343 | * rollback. Nested transaction calls will be wrapped into single big outer transaction, so you might call it safely if needed. |
||
| 344 | * |
||
| 345 | * @param callable $callback This instance will be used as single argument |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | * |
||
| 349 | * @throws Exception Re-throws exception thrown inside callback |
||
| 350 | */ |
||
| 351 | 38 | public function transaction ($callback) { |
|
| 380 | /** |
||
| 381 | * Free result memory |
||
| 382 | * |
||
| 383 | * @abstract |
||
| 384 | * |
||
| 385 | * @param false|object|resource $query_result |
||
| 386 | */ |
||
| 387 | abstract public function free ($query_result); |
||
| 388 | /** |
||
| 389 | * Get columns list of table |
||
| 390 | * |
||
| 391 | * @param string $table |
||
| 392 | * @param false|string $like |
||
| 393 | * |
||
| 394 | * @return string[] |
||
| 395 | */ |
||
| 396 | abstract public function columns ($table, $like = false); |
||
| 397 | /** |
||
| 398 | * Get tables list |
||
| 399 | * |
||
| 400 | * @param false|string $like |
||
| 401 | * |
||
| 402 | * @return string[] |
||
| 403 | */ |
||
| 404 | abstract public function tables ($like = false); |
||
| 405 | /** |
||
| 406 | * Safe |
||
| 407 | * |
||
| 408 | * Preparing string for using in SQL query |
||
| 409 | * SQL Injection Protection |
||
| 410 | * |
||
| 411 | * @param string|string[] $string |
||
| 412 | * @param bool $single_quotes_around |
||
| 413 | * |
||
| 414 | * @return string|string[] |
||
| 415 | */ |
||
| 416 | 44 | public function s ($string, $single_quotes_around = true) { |
|
| 425 | /** |
||
| 426 | * Preparing string for using in SQL query |
||
| 427 | * SQL Injection Protection |
||
| 428 | * |
||
| 429 | * @param string $string |
||
| 430 | * @param bool $single_quotes_around |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | abstract protected function s_internal ($string, $single_quotes_around); |
||
| 435 | /** |
||
| 436 | * Get information about server |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | abstract public function server (); |
||
| 441 | /** |
||
| 442 | * Connection state |
||
| 443 | * |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | 46 | public function connected () { |
|
| 449 | /** |
||
| 450 | * Database type (lowercase, for example <i>mysql</i>) |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 2 | public function db_type () { |
|
| 457 | /** |
||
| 458 | * Database name |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 4 | public function database () { |
|
| 465 | /** |
||
| 466 | * Queries array, has 3 properties:<ul> |
||
| 467 | * <li>num - total number of performed queries |
||
| 468 | * <li>time - array with time of each query execution |
||
| 469 | * <li>text - array with text text of each query |
||
| 470 | * |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | 4 | public function queries () { |
|
| 476 | /** |
||
| 477 | * Last query information, has 2 properties:<ul> |
||
| 478 | * <li>time - execution time |
||
| 479 | * <li>text - query text |
||
| 480 | * |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | 2 | public function query () { |
|
| 486 | /** |
||
| 487 | * Total working time (including connection, queries execution and other delays) |
||
| 488 | * |
||
| 489 | * @return float |
||
| 490 | */ |
||
| 491 | 4 | public function time () { |
|
| 494 | /** |
||
| 495 | * Connecting time |
||
| 496 | * |
||
| 497 | * @return float |
||
| 498 | */ |
||
| 499 | 4 | public function connecting_time () { |
|
| 502 | /** |
||
| 503 | * Cloning restriction |
||
| 504 | * |
||
| 505 | * @final |
||
| 506 | */ |
||
| 507 | final protected function __clone () { |
||
| 509 | /** |
||
| 510 | * Disconnecting from DB |
||
| 511 | */ |
||
| 512 | abstract public function __destruct (); |
||
| 513 | } |
||
| 514 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.