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 | public function __construct($driver) { |
||
| 29 | $this->driver = $driver instanceof DriverInterface ? $driver : static::getDriver($driver); |
||
| 30 | } |
||
| 31 | /** |
||
| 32 | * Create a driver instance from a connection string |
||
| 33 | * @param string $connectionString the connection string |
||
| 34 | * @return DriverInterface |
||
| 35 | */ |
||
| 36 | public static function getDriver(string $connectionString) |
||
| 37 | { |
||
| 38 | $connection = [ |
||
| 39 | 'orig' => $connectionString, |
||
| 40 | 'type' => null, |
||
| 41 | 'user' => null, |
||
| 42 | 'pass' => null, |
||
| 43 | 'host' => null, |
||
| 44 | 'port' => null, |
||
| 45 | 'name' => null, |
||
| 46 | 'opts' => [] |
||
| 47 | ]; |
||
| 48 | $aliases = [ |
||
| 49 | 'mysqli' => 'mysql', |
||
| 50 | 'pg' => 'postgre', |
||
| 51 | 'oci' => 'oracle', |
||
| 52 | 'firebird' => 'ibase' |
||
| 53 | ]; |
||
| 54 | $connectionString = array_pad(explode('://', $connectionString, 2), 2, ''); |
||
| 55 | $connection['type'] = $connectionString[0]; |
||
| 56 | $connectionString = $connectionString[1]; |
||
| 57 | if (strpos($connectionString, '@') !== false) { |
||
| 58 | $connectionString = array_pad(explode('@', $connectionString, 2), 2, ''); |
||
| 59 | list($connection['user'], $connection['pass']) = array_pad(explode(':', $connectionString[0], 2), 2, ''); |
||
| 60 | $connectionString = $connectionString[1]; |
||
| 61 | } |
||
| 62 | $connectionString = array_pad(explode('/', $connectionString, 2), 2, ''); |
||
| 63 | list($connection['host'], $connection['port']) = array_pad(explode(':', $connectionString[0], 2), 2, null); |
||
| 64 | $connectionString = $connectionString[1]; |
||
| 65 | if ($pos = strrpos($connectionString, '?')) { |
||
| 66 | $opt = substr($connectionString, $pos + 1); |
||
| 67 | parse_str($opt, $connection['opts']); |
||
| 68 | if ($connection['opts'] && count($connection['opts'])) { |
||
| 69 | $connectionString = substr($connectionString, 0, $pos); |
||
| 70 | } else { |
||
| 71 | $connection['opts'] = []; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | $connection['name'] = $connectionString; |
||
| 75 | $connection['type'] = isset($aliases[$connection['type']]) ? |
||
| 76 | $aliases[$connection['type']] : |
||
| 77 | $connection['type']; |
||
| 78 | $tmp = '\\vakata\\database\\driver\\'.strtolower($connection['type']).'\\Driver'; |
||
| 79 | return new $tmp($connection); |
||
| 80 | } |
||
| 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 |
||
| 92 | protected function expand(string $sql, $par = null) : array |
||
| 93 | { |
||
| 94 | $new = ''; |
||
| 95 | $par = array_values($par); |
||
| 96 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
||
| 97 | $par = [ $par ]; |
||
| 98 | } |
||
| 99 | $parts = explode('??', $sql); |
||
| 100 | $index = 0; |
||
| 101 | foreach ($parts as $part) { |
||
| 102 | $tmp = explode('?', $part); |
||
| 103 | $new .= $part; |
||
| 104 | $index += count($tmp) - 1; |
||
| 105 | if (isset($par[$index])) { |
||
| 106 | if (!is_array($par[$index])) { |
||
| 107 | $par[$index] = [ $par[$index] ]; |
||
| 108 | } |
||
| 109 | $params = $par[$index]; |
||
| 110 | array_splice($par, $index, 1, $params); |
||
| 111 | $index += count($params); |
||
| 112 | $new .= implode(',', array_fill(0, count($params), '?')); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | 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 | public function query(string $sql, $par = null) : ResultInterface |
||
| 124 | { |
||
| 125 | $par = isset($par) ? (is_array($par) ? $par : [$par]) : []; |
||
| 126 | if (strpos($sql, '??') && count($par)) { |
||
| 127 | list($sql, $par) = $this->expand($sql, $par); |
||
| 128 | } |
||
| 129 | return $this->driver->prepare($sql)->execute($par); |
||
| 130 | } |
||
| 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 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : Collection |
||
| 144 | { |
||
| 145 | $coll = Collection::from($this->query($sql, $par)); |
||
| 146 | if (($keys = $this->driver->option('mode')) && in_array($keys, ['strtoupper', 'strtolower'])) { |
||
| 147 | $coll->map(function ($v) use ($keys) { |
||
| 148 | $new = []; |
||
| 149 | foreach ($v as $k => $vv) { |
||
| 150 | $new[call_user_func($keys, $k)] = $vv; |
||
| 151 | } |
||
| 152 | return $new; |
||
| 153 | }); |
||
| 154 | } |
||
| 155 | if ($key) { |
||
| 156 | $coll->mapKey(function ($v) use ($key) { return $v[$key]; }); |
||
| 157 | } |
||
| 158 | if ($skip) { |
||
| 159 | $coll->map(function ($v) use ($key) { unset($v[$key]); return $v; }); |
||
| 160 | } |
||
| 161 | if ($opti) { |
||
| 162 | $coll->map(function ($v) { return count($v) === 1 ? current($v) : $v; }); |
||
| 163 | } |
||
| 164 | if ($keys) { |
||
| 165 | $coll->map(function ($v) use ($key) { unset($v[$key]); return $v; }); |
||
| 166 | } |
||
| 167 | 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 | public function one(string $sql, $par = null, bool $opti = true) |
||
| 178 | { |
||
| 179 | return $this->get($sql, $par, null, false, $opti)->value(); |
||
| 180 | } |
||
| 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 | public function driver() : string |
||
| 233 | { |
||
| 234 | return array_reverse(explode('\\', get_class($this->driver)))[1]; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function definition(string $table, bool $detectRelations = true) : Table |
||
| 238 | { |
||
| 239 | return isset($this->tables[$table]) ? |
||
| 240 | $this->tables[$table] : |
||
| 241 | $this->driver->table($table, $detectRelations); |
||
| 242 | } |
||
| 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) |
||
| 257 | { |
||
| 258 | return !$asPlainArray ? $this->tables : array_map(function ($table) { |
||
| 259 | return [ |
||
| 260 | 'name' => $table->getName(), |
||
| 261 | 'pkey' => $table->getPrimaryKey(), |
||
| 262 | 'comment' => $table->getComment(), |
||
| 263 | 'columns' => array_map(function ($column) { |
||
| 264 | return [ |
||
| 265 | 'name' => $column->getName(), |
||
| 266 | 'type' => $column->getType(), |
||
| 267 | 'comment' => $column->getComment(), |
||
| 268 | 'values' => $column->getValues(), |
||
| 269 | 'default' => $column->getDefault(), |
||
| 270 | 'nullable' => $column->isNullable() |
||
| 271 | ]; |
||
| 272 | }, $table->getFullColumns()), |
||
| 273 | 'relations' => array_map(function ($rel) { |
||
| 274 | $relation = clone $rel; |
||
| 275 | $relation->table = $relation->table->getName(); |
||
| 276 | if ($relation->pivot) { |
||
| 277 | $relation->pivot = $relation->pivot->getName(); |
||
| 278 | } |
||
| 279 | return (array)$relation; |
||
| 280 | }, $table->getRelations()) |
||
| 281 | ]; |
||
| 282 | }, $this->tables); |
||
| 283 | } |
||
| 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 | public function table($table) |
||
| 325 | { |
||
| 326 | return new TableQuery($this, $this->definition($table)); |
||
| 327 | } |
||
| 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.