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 DriverInterface|string $driver a driver instance or a connection string |
||
| 29 | */ |
||
| 30 | 15 | public function __construct($driver) |
|
| 34 | /** |
||
| 35 | * Create a driver instance from a connection string |
||
| 36 | * @param string $connectionString the connection string |
||
| 37 | * @return DriverInterface |
||
| 38 | */ |
||
| 39 | 15 | public static function getDriver(string $connectionString) |
|
| 92 | /** |
||
| 93 | * Prepare a statement. |
||
| 94 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 95 | * |
||
| 96 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 97 | * @return StatementInterface the prepared statement |
||
| 98 | */ |
||
| 99 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 100 | { |
||
| 101 | 1 | return $this->driver->prepare($sql); |
|
| 102 | } |
||
| 103 | /** |
||
| 104 | * Test the connection |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 1 | public function test() : bool |
|
| 112 | 27 | protected function expand(string $sql, $par = null) : array |
|
| 113 | { |
||
| 114 | 27 | $new = ''; |
|
| 115 | 27 | $par = array_values($par); |
|
| 116 | 27 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
|
| 117 | 3 | $par = [ $par ]; |
|
| 118 | } |
||
| 119 | 27 | $parts = explode('??', $sql); |
|
| 120 | 27 | $index = 0; |
|
| 121 | 27 | foreach ($parts as $part) { |
|
| 122 | 27 | $tmp = explode('?', $part); |
|
| 123 | 27 | $new .= $part; |
|
| 124 | 27 | $index += count($tmp) - 1; |
|
| 125 | 27 | if (isset($par[$index])) { |
|
| 126 | 27 | if (!is_array($par[$index])) { |
|
| 127 | $par[$index] = [ $par[$index] ]; |
||
| 128 | } |
||
| 129 | 27 | $params = $par[$index]; |
|
| 130 | 27 | array_splice($par, $index, 1, $params); |
|
| 131 | 27 | $index += count($params); |
|
| 132 | 27 | $new .= implode(',', array_fill(0, count($params), '?')); |
|
| 133 | } |
||
| 134 | } |
||
| 135 | 27 | return [ $new, $par ]; |
|
| 136 | } |
||
| 137 | /** |
||
| 138 | * Run a query (prepare & execute). |
||
| 139 | * @param string $sql SQL query |
||
| 140 | * @param mixed $par parameters (optional) |
||
| 141 | * @return ResultInterface the result of the execution |
||
| 142 | */ |
||
| 143 | 166 | public function query(string $sql, $par = null) : ResultInterface |
|
| 151 | /** |
||
| 152 | * Run a SELECT query and get an array-like result. |
||
| 153 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 154 | * |
||
| 155 | * @param string $sql SQL query |
||
| 156 | * @param array $par parameters |
||
| 157 | * @param string $key column name to use as the array index |
||
| 158 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 159 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 160 | * |
||
| 161 | * @return Collection the result of the execution |
||
| 162 | */ |
||
| 163 | 160 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 164 | { |
||
| 165 | 160 | $coll = Collection::from($this->query($sql, $par)); |
|
| 166 | 160 | if (($keys = $this->driver->option('mode')) && in_array($keys, ['strtoupper', 'strtolower'])) { |
|
| 167 | 1 | $coll->map(function ($v) use ($keys) { |
|
| 168 | 1 | $new = []; |
|
| 169 | 1 | foreach ($v as $k => $vv) { |
|
| 170 | 1 | $new[call_user_func($keys, $k)] = $vv; |
|
| 171 | } |
||
| 172 | 1 | return $new; |
|
| 173 | 1 | }); |
|
| 174 | } |
||
| 175 | 160 | if ($key !== null) { |
|
| 176 | 2 | $coll->mapKey(function ($v) use ($key) { |
|
| 177 | 2 | return $v[$key]; |
|
| 178 | 2 | }); |
|
| 179 | } |
||
| 180 | 160 | if ($skip) { |
|
| 181 | 2 | $coll->map(function ($v) use ($key) { |
|
| 182 | 2 | unset($v[$key]); |
|
| 183 | 2 | return $v; |
|
| 184 | 2 | }); |
|
| 185 | } |
||
| 186 | 160 | if ($opti) { |
|
| 187 | 67 | $coll->map(function ($v) { |
|
| 188 | 67 | return count($v) === 1 ? current($v) : $v; |
|
| 189 | 67 | }); |
|
| 190 | } |
||
| 191 | 160 | return $coll; |
|
| 192 | } |
||
| 193 | /** |
||
| 194 | * Run a SELECT query and get a single row |
||
| 195 | * @param string $sql SQL query |
||
| 196 | * @param array $par parameters |
||
| 197 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 198 | * @return mixed the result of the execution |
||
| 199 | */ |
||
| 200 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 204 | /** |
||
| 205 | * Run a SELECT query and get an array |
||
| 206 | * @param string $sql SQL query |
||
| 207 | * @param array $par parameters |
||
| 208 | * @param string $key column name to use as the array index |
||
| 209 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 210 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 211 | * @return array the result of the execution |
||
| 212 | */ |
||
| 213 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 217 | /** |
||
| 218 | * Begin a transaction. |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | 1 | public function begin() : DBInterface |
|
| 222 | { |
||
| 223 | 1 | if (!$this->driver->begin()) { |
|
| 224 | throw new DBException('Could not begin'); |
||
| 225 | } |
||
| 226 | 1 | return $this; |
|
| 227 | } |
||
| 228 | /** |
||
| 229 | * Commit a transaction. |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | 1 | public function commit() : DBInterface |
|
| 233 | { |
||
| 234 | 1 | if (!$this->driver->commit()) { |
|
| 235 | throw new DBException('Could not commit'); |
||
| 236 | } |
||
| 237 | 1 | return $this; |
|
| 238 | } |
||
| 239 | /** |
||
| 240 | * Rollback a transaction. |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | 1 | public function rollback() : DBInterface |
|
| 244 | { |
||
| 245 | 1 | if (!$this->driver->rollback()) { |
|
| 246 | throw new DBException('Could not rollback'); |
||
| 247 | } |
||
| 248 | 1 | return $this; |
|
| 249 | } |
||
| 250 | /** |
||
| 251 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 252 | * @return string the current driver name |
||
| 253 | */ |
||
| 254 | 16 | public function driverName() : string |
|
| 258 | /** |
||
| 259 | * Get an option from the driver |
||
| 260 | * |
||
| 261 | * @param string $key the option name |
||
| 262 | * @param mixed $default the default value to return if the option key is not defined |
||
| 263 | * @return mixed the option value |
||
| 264 | */ |
||
| 265 | 76 | public function driverOption(string $key, $default = null) |
|
| 269 | |||
| 270 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 276 | /** |
||
| 277 | * Parse all tables from the database. |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function parseSchema() |
||
| 285 | /** |
||
| 286 | * Get the full schema as an array that you can serialize and store |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getSchema($asPlainArray = true) |
||
| 319 | /** |
||
| 320 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 321 | * @param array $data the schema definition |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function setSchema(array $data) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Initialize a table query |
||
| 356 | * @param string $table the table to query |
||
| 357 | * @return TableQuery |
||
| 358 | */ |
||
| 359 | 156 | public function table($table, bool $mapped = false) |
|
| 369 | } |
||
| 370 |