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 |
||
| 12 | class DB implements DBInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var DriverInterface |
||
| 16 | */ |
||
| 17 | protected $driver; |
||
| 18 | /** |
||
| 19 | * @var Table[] |
||
| 20 | */ |
||
| 21 | protected $tables = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Create an instance. |
||
| 25 | * |
||
| 26 | * @param DriverInterface|string $driver a driver instance or a connection string |
||
| 27 | */ |
||
| 28 | 3 | public function __construct($driver) { |
|
| 31 | /** |
||
| 32 | * Create a driver instance from a connection string |
||
| 33 | * @param string $connectionString the connection string |
||
| 34 | * @return DriverInterface |
||
| 35 | */ |
||
| 36 | 3 | public static function getDriver(string $connectionString) |
|
| 81 | /** |
||
| 82 | * Prepare a statement. |
||
| 83 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 84 | * |
||
| 85 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 86 | * @return StatementInterface the prepared statement |
||
| 87 | */ |
||
| 88 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 89 | { |
||
| 90 | 1 | return $this->driver->prepare($sql); |
|
| 91 | } |
||
| 92 | 8 | protected function expand(string $sql, $par = null) : array |
|
| 93 | { |
||
| 94 | 8 | $new = ''; |
|
| 95 | 8 | $par = array_values($par); |
|
| 96 | 8 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
|
| 97 | 3 | $par = [ $par ]; |
|
| 98 | } |
||
| 99 | 8 | $parts = explode('??', $sql); |
|
| 100 | 8 | $index = 0; |
|
| 101 | 8 | foreach ($parts as $part) { |
|
| 102 | 8 | $tmp = explode('?', $part); |
|
| 103 | 8 | $new .= $part; |
|
| 104 | 8 | $index += count($tmp) - 1; |
|
| 105 | 8 | if (isset($par[$index])) { |
|
| 106 | 8 | if (!is_array($par[$index])) { |
|
| 107 | $par[$index] = [ $par[$index] ]; |
||
| 108 | } |
||
| 109 | 8 | $params = $par[$index]; |
|
| 110 | 8 | array_splice($par, $index, 1, $params); |
|
| 111 | 8 | $index += count($params); |
|
| 112 | 8 | $new .= implode(',', array_fill(0, count($params), '?')); |
|
| 113 | } |
||
| 114 | } |
||
| 115 | 8 | return [ $new, $par ]; |
|
| 116 | } |
||
| 117 | /** |
||
| 118 | * Run a query (prepare & execute). |
||
| 119 | * @param string $sql SQL query |
||
| 120 | * @param array $data parameters (optional) |
||
|
|
|||
| 121 | * @return ResultInterface the result of the execution |
||
| 122 | */ |
||
| 123 | 26 | public function query(string $sql, $par = null) : ResultInterface |
|
| 131 | /** |
||
| 132 | * Run a SELECT query and get an array-like result. |
||
| 133 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 134 | * |
||
| 135 | * @param string $sql SQL query |
||
| 136 | * @param array $par parameters |
||
| 137 | * @param string $key column name to use as the array index |
||
| 138 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 139 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 140 | * |
||
| 141 | * @return Collection the result of the execution |
||
| 142 | */ |
||
| 143 | 20 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : Collection |
|
| 144 | { |
||
| 145 | 20 | $coll = Collection::from($this->query($sql, $par)); |
|
| 146 | 20 | if (($keys = $this->driver->option('mode')) && in_array($keys, ['strtoupper', 'strtolower'])) { |
|
| 147 | $coll->map(function ($v) use ($keys) { |
||
| 148 | 1 | $new = []; |
|
| 149 | 1 | foreach ($v as $k => $vv) { |
|
| 150 | 1 | $new[call_user_func($keys, $k)] = $vv; |
|
| 151 | } |
||
| 152 | 1 | return $new; |
|
| 153 | 1 | }); |
|
| 154 | } |
||
| 155 | 20 | if ($key) { |
|
| 156 | $coll->mapKey(function ($v) use ($key) { return $v[$key]; }); |
||
| 157 | } |
||
| 158 | 20 | if ($skip) { |
|
| 159 | $coll->map(function ($v) use ($key) { unset($v[$key]); return $v; }); |
||
| 160 | } |
||
| 161 | 20 | if ($opti) { |
|
| 162 | $coll->map(function ($v) { return count($v) === 1 ? current($v) : $v; }); |
||
| 163 | } |
||
| 164 | 20 | if ($keys) { |
|
| 165 | $coll->map(function ($v) use ($key) { unset($v[$key]); return $v; }); |
||
| 166 | } |
||
| 167 | 20 | return $coll; |
|
| 168 | } |
||
| 169 | /** |
||
| 170 | * Run a SELECT query and get a single row |
||
| 171 | * @param string $sql SQL query |
||
| 172 | * @param array $par parameters |
||
| 173 | * @param callable $keys an optional mutator to pass each row's keys through (the column names) |
||
| 174 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 175 | * @return Collection the result of the execution |
||
| 176 | */ |
||
| 177 | 7 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 181 | /** |
||
| 182 | * Run a SELECT query and get an array |
||
| 183 | * @param string $sql SQL query |
||
| 184 | * @param array $par parameters |
||
| 185 | * @param string $key column name to use as the array index |
||
| 186 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 187 | * @param callable $keys an optional mutator to pass each row's keys through (the column names) |
||
| 188 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 189 | * @return Collection the result of the execution |
||
| 190 | */ |
||
| 191 | 2 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 192 | { |
||
| 193 | 2 | return $this->get($sql, $par, $key, $skip, $opti)->toArray(); |
|
| 194 | } |
||
| 195 | /** |
||
| 196 | * Begin a transaction. |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | 1 | public function begin() : DBInterface |
|
| 200 | { |
||
| 201 | 1 | if (!$this->driver->begin()) { |
|
| 202 | throw new DBException('Could not begin'); |
||
| 203 | } |
||
| 204 | 1 | return $this; |
|
| 205 | } |
||
| 206 | /** |
||
| 207 | * Commit a transaction. |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | 1 | public function commit() : DBInterface |
|
| 211 | { |
||
| 212 | 1 | if (!$this->driver->commit()) { |
|
| 213 | throw new DBException('Could not commit'); |
||
| 214 | } |
||
| 215 | 1 | return $this; |
|
| 216 | } |
||
| 217 | /** |
||
| 218 | * Rollback a transaction. |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | 1 | public function rollback() : DBInterface |
|
| 222 | { |
||
| 223 | 1 | if (!$this->driver->rollback()) { |
|
| 224 | throw new DBException('Could not rollback'); |
||
| 225 | } |
||
| 226 | 1 | return $this; |
|
| 227 | } |
||
| 228 | /** |
||
| 229 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 230 | * @return string the current driver name |
||
| 231 | */ |
||
| 232 | 2 | public function driver() : string |
|
| 236 | |||
| 237 | 12 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 243 | /** |
||
| 244 | * Parse all tables from the database. |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function parseSchema() |
||
| 252 | /** |
||
| 253 | * Get the full schema as an array that you can serialize and store |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function getSchema($asPlainArray = true) |
||
| 284 | /** |
||
| 285 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 286 | * @param array $data the schema definition |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function setSchema(array $data) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Initialize a table query |
||
| 321 | * @param string $table the table to query |
||
| 322 | * @return TableQuery |
||
| 323 | */ |
||
| 324 | 12 | public function table($table) |
|
| 332 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.