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 |
||
| 14 | class DB implements DBInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var DriverInterface |
||
| 18 | */ |
||
| 19 | protected $driver; |
||
| 20 | /** |
||
| 21 | * @var Table[] |
||
| 22 | */ |
||
| 23 | protected $tables = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Create an instance. |
||
| 27 | * |
||
| 28 | * @param string $connectionString a driver instance or a connection string |
||
| 29 | */ |
||
| 30 | 15 | public function __construct(string $connectionString) |
|
| 89 | |||
| 90 | public function driver(): DriverInterface |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Prepare a statement. |
||
| 97 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 98 | * |
||
| 99 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 100 | * @return StatementInterface the prepared statement |
||
| 101 | */ |
||
| 102 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 106 | /** |
||
| 107 | * Test the connection |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | 1 | public function test() : bool |
|
| 140 | /** |
||
| 141 | * Run a query (prepare & execute). |
||
| 142 | * @param string $sql SQL query |
||
| 143 | * @param mixed $par parameters (optional) |
||
| 144 | * @param bool $buff should the results be buffered (defaults to true) |
||
| 145 | * @return ResultInterface the result of the execution |
||
| 146 | */ |
||
| 147 | 166 | public function query(string $sql, $par = null, bool $buff = true) : ResultInterface |
|
| 155 | /** |
||
| 156 | * Run a query. |
||
| 157 | * @param string $sql SQL query |
||
| 158 | * @return mixed the result of the execution |
||
| 159 | */ |
||
| 160 | public function raw(string $sql) |
||
| 164 | /** |
||
| 165 | * Run a SELECT query and get an array-like result. |
||
| 166 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 167 | * |
||
| 168 | * @param string $sql SQL query |
||
| 169 | * @param array $par parameters |
||
| 170 | * @param string $key column name to use as the array index |
||
| 171 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 172 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 173 | * @param bool $buff should the results be buffered (defaults to `false`) |
||
| 174 | * |
||
| 175 | * @return Collection the result of the execution |
||
| 176 | */ |
||
| 177 | 160 | public function get( |
|
| 213 | /** |
||
| 214 | * Run a SELECT query and get a single row |
||
| 215 | * @param string $sql SQL query |
||
| 216 | * @param array $par parameters |
||
| 217 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 218 | * @return mixed the result of the execution |
||
| 219 | */ |
||
| 220 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 224 | /** |
||
| 225 | * Run a SELECT query and get an array |
||
| 226 | * @param string $sql SQL query |
||
| 227 | * @param array $par parameters |
||
| 228 | * @param string $key column name to use as the array index |
||
| 229 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 230 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 231 | * @return array the result of the execution |
||
| 232 | */ |
||
| 233 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 246 | /** |
||
| 247 | * Begin a transaction. |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 1 | public function begin() : DBInterface |
|
| 257 | /** |
||
| 258 | * Commit a transaction. |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | 1 | public function commit() : DBInterface |
|
| 268 | /** |
||
| 269 | * Rollback a transaction. |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | 1 | public function rollback() : DBInterface |
|
| 279 | /** |
||
| 280 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 281 | * @return string the current driver name |
||
| 282 | */ |
||
| 283 | 16 | public function driverName() : string |
|
| 287 | /** |
||
| 288 | * Get an option from the driver |
||
| 289 | * |
||
| 290 | * @param string $key the option name |
||
| 291 | * @param mixed $default the default value to return if the option key is not defined |
||
| 292 | * @return mixed the option value |
||
| 293 | */ |
||
| 294 | 76 | public function driverOption(string $key, $default = null) |
|
| 298 | |||
| 299 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 305 | /** |
||
| 306 | * Parse all tables from the database. |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function parseSchema() |
||
| 314 | /** |
||
| 315 | * Get the full schema as an array that you can serialize and store |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getSchema($asPlainArray = true) |
||
| 319 | { |
||
| 320 | 4 | return !$asPlainArray ? $this->tables : array_map(function ($table) { |
|
| 321 | return [ |
||
| 322 | 'name' => $table->getName(), |
||
| 323 | 'schema' => $table->getSchema(), |
||
| 324 | 'pkey' => $table->getPrimaryKey(), |
||
| 325 | 'comment' => $table->getComment(), |
||
| 326 | 'columns' => array_map(function ($column) { |
||
| 327 | return [ |
||
| 328 | 'name' => $column->getName(), |
||
| 329 | 'type' => $column->getType(), |
||
| 330 | 'length' => $column->getLength(), |
||
| 331 | 'comment' => $column->getComment(), |
||
| 332 | 'values' => $column->getValues(), |
||
| 333 | 'default' => $column->getDefault(), |
||
| 334 | 'nullable' => $column->isNullable() |
||
| 335 | ]; |
||
| 336 | }, $table->getFullColumns()), |
||
| 337 | 'relations' => array_map(function ($rel) { |
||
| 338 | $relation = clone $rel; |
||
| 339 | $relation = (array)$relation; |
||
| 340 | $relation['table'] = $rel->table->getName(); |
||
| 341 | if ($rel->pivot) { |
||
| 342 | $relation['pivot'] = $rel->pivot->getName(); |
||
| 343 | } |
||
| 344 | return $relation; |
||
| 345 | }, $table->getRelations()) |
||
| 346 | ]; |
||
| 347 | 4 | }, $this->tables); |
|
| 348 | } |
||
| 349 | /** |
||
| 350 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 351 | * @param array $data the schema definition |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function setSchema(array $data) |
||
| 355 | { |
||
| 356 | foreach ($data as $tableData) { |
||
| 357 | $this->tables[$tableData['name']] = (new Table($tableData['name'], $tableData['schema'])) |
||
| 358 | ->setPrimaryKey($tableData['pkey']) |
||
| 359 | ->setComment($tableData['comment']) |
||
| 360 | ->addColumns($tableData['columns']); |
||
| 361 | } |
||
| 362 | foreach ($data as $tableData) { |
||
| 363 | $table = $this->definition($tableData['name']); |
||
| 364 | foreach ($tableData['relations'] as $relationName => $relationData) { |
||
| 365 | $relationData['table'] = $this->definition($relationData['table']); |
||
| 366 | if ($relationData['pivot']) { |
||
| 367 | $relationData['pivot'] = $this->definition($relationData['pivot']); |
||
| 368 | } |
||
| 369 | $table->addRelation(new TableRelation( |
||
| 370 | $relationData['name'], |
||
| 371 | $relationData['table'], |
||
| 372 | $relationData['keymap'], |
||
| 373 | $relationData['many'], |
||
| 374 | $relationData['pivot'] ?? null, |
||
| 375 | $relationData['pivot_keymap'], |
||
| 376 | $relationData['sql'], |
||
| 377 | $relationData['par'] |
||
| 378 | )); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | return $this; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Initialize a table query |
||
| 386 | * @param string $table the table to query |
||
| 387 | * @return TableQuery |
||
| 388 | */ |
||
| 389 | 156 | public function table(string $table, bool $mapped = false) |
|
| 395 | 148 | public function __call($method, $args) |
|
| 396 | { |
||
| 397 | 148 | return $this->table($method, $args[0] ?? false); |
|
| 399 | } |
||
| 400 |