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 | 1 | 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 | 1 | 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 | public function prepare(string $sql) : StatementInterface |
||
| 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 | 13 | 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 | 13 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : Collection |
|
| 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 | 4 | 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 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
||
| 195 | /** |
||
| 196 | * Begin a transaction. |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function begin() : DBInterface |
||
| 206 | /** |
||
| 207 | * Commit a transaction. |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function commit() : DBInterface |
||
| 217 | /** |
||
| 218 | * Rollback a transaction. |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function rollback() : DBInterface |
||
| 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.