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 |
||
| 13 | class DB implements DBInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var DriverInterface |
||
| 17 | */ |
||
| 18 | protected $driver; |
||
| 19 | /** |
||
| 20 | * @var Table[] |
||
| 21 | */ |
||
| 22 | protected $tables = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create an instance. |
||
| 26 | * |
||
| 27 | * @param DriverInterface|string $driver a driver instance or a connection string |
||
| 28 | */ |
||
| 29 | 3 | public function __construct($driver) { |
|
| 32 | /** |
||
| 33 | * Create a driver instance from a connection string |
||
| 34 | * @param string $connectionString the connection string |
||
| 35 | * @return DriverInterface |
||
| 36 | */ |
||
| 37 | 3 | public static function getDriver(string $connectionString) |
|
| 82 | /** |
||
| 83 | * Prepare a statement. |
||
| 84 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 85 | * |
||
| 86 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 87 | * @return StatementInterface the prepared statement |
||
| 88 | */ |
||
| 89 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 118 | /** |
||
| 119 | * Run a query (prepare & execute). |
||
| 120 | * @param string $sql SQL query |
||
| 121 | * @param array $data parameters (optional) |
||
|
|
|||
| 122 | * @return ResultInterface the result of the execution |
||
| 123 | */ |
||
| 124 | 26 | public function query(string $sql, $par = null) : ResultInterface |
|
| 132 | /** |
||
| 133 | * Run a SELECT query and get an array-like result. |
||
| 134 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 135 | * |
||
| 136 | * @param string $sql SQL query |
||
| 137 | * @param array $par parameters |
||
| 138 | * @param string $key column name to use as the array index |
||
| 139 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 140 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 141 | * |
||
| 142 | * @return Collection the result of the execution |
||
| 143 | */ |
||
| 144 | 20 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : Collection |
|
| 170 | /** |
||
| 171 | * Run a SELECT query and get a single row |
||
| 172 | * @param string $sql SQL query |
||
| 173 | * @param array $par parameters |
||
| 174 | * @param callable $keys an optional mutator to pass each row's keys through (the column names) |
||
| 175 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 176 | * @return Collection the result of the execution |
||
| 177 | */ |
||
| 178 | 7 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 182 | /** |
||
| 183 | * Run a SELECT query and get an array |
||
| 184 | * @param string $sql SQL query |
||
| 185 | * @param array $par parameters |
||
| 186 | * @param string $key column name to use as the array index |
||
| 187 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 188 | * @param callable $keys an optional mutator to pass each row's keys through (the column names) |
||
| 189 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 190 | * @return Collection the result of the execution |
||
| 191 | */ |
||
| 192 | 2 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 196 | /** |
||
| 197 | * Begin a transaction. |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | 1 | public function begin() : DBInterface |
|
| 207 | /** |
||
| 208 | * Commit a transaction. |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 1 | public function commit() : DBInterface |
|
| 218 | /** |
||
| 219 | * Rollback a transaction. |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | 1 | public function rollback() : DBInterface |
|
| 229 | /** |
||
| 230 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 231 | * @return string the current driver name |
||
| 232 | */ |
||
| 233 | 2 | public function driver() : string |
|
| 237 | |||
| 238 | 12 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 244 | /** |
||
| 245 | * Parse all tables from the database. |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function parseSchema() |
||
| 253 | /** |
||
| 254 | * Get the full schema as an array that you can serialize and store |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 1 | public function getSchema($asPlainArray = true) |
|
| 258 | { |
||
| 259 | return !$asPlainArray ? $this->tables : array_map(function ($table) { |
||
| 260 | return [ |
||
| 261 | 'name' => $table->getName(), |
||
| 262 | 'pkey' => $table->getPrimaryKey(), |
||
| 263 | 'comment' => $table->getComment(), |
||
| 264 | 'columns' => array_map(function ($column) { |
||
| 265 | return [ |
||
| 266 | 'name' => $column->getName(), |
||
| 267 | 'type' => $column->getType(), |
||
| 268 | 'comment' => $column->getComment(), |
||
| 269 | 'values' => $column->getValues(), |
||
| 270 | 'default' => $column->getDefault(), |
||
| 271 | 'nullable' => $column->isNullable() |
||
| 272 | ]; |
||
| 273 | }, $table->getFullColumns()), |
||
| 274 | 'relations' => array_map(function ($rel) { |
||
| 275 | $relation = clone $rel; |
||
| 276 | $relation->table = $relation->table->getName(); |
||
| 277 | if ($relation->pivot) { |
||
| 278 | $relation->pivot = $relation->pivot->getName(); |
||
| 279 | } |
||
| 280 | return (array)$relation; |
||
| 281 | }, $table->getRelations()) |
||
| 282 | ]; |
||
| 283 | 1 | }, $this->tables); |
|
| 284 | } |
||
| 285 | /** |
||
| 286 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 287 | * @param array $data the schema definition |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function setSchema(array $data) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Initialize a table query |
||
| 322 | * @param string $table the table to query |
||
| 323 | * @return TableQuery |
||
| 324 | */ |
||
| 325 | 12 | public function table($table) |
|
| 333 | } |
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.