Complex classes like MysqlDriver 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 MysqlDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php /** MysqlDriverMicro */ |
||
| 19 | class MysqlDriver extends Driver |
||
| 20 | { |
||
| 21 | /** @var \PDO|null $conn Connection to DB */ |
||
| 22 | protected $conn; |
||
| 23 | /** @var string $tableSchema Table schema for postgres */ |
||
| 24 | protected $tableSchema = 'public'; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Construct for this class |
||
| 29 | * |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @param array $config |
||
| 33 | * @param array $options |
||
| 34 | * |
||
| 35 | * @result void |
||
| 36 | * @throws Exception |
||
| 37 | */ |
||
| 38 | public function __construct(array $config = [], array $options = []) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Destructor for this class |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function __destruct() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Send RAW query to DB |
||
| 70 | * |
||
| 71 | * @access public |
||
| 72 | * |
||
| 73 | * @param string $query raw query to db |
||
| 74 | * @param array $params params for query |
||
| 75 | * @param int $fetchType fetching type |
||
| 76 | * @param string $fetchClass fetching class |
||
| 77 | * |
||
| 78 | * @return \PDOStatement|array |
||
| 79 | * @throws Exception |
||
| 80 | */ |
||
| 81 | public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model') |
||
| 100 | |||
| 101 | /** |
||
| 102 | * List database names on this connection |
||
| 103 | * |
||
| 104 | * @access public |
||
| 105 | * @return array|boolean |
||
| 106 | */ |
||
| 107 | public function listDatabases() |
||
| 124 | |||
| 125 | public function getDriverType() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Info of database |
||
| 132 | * |
||
| 133 | * @access public |
||
| 134 | * @param string $dbName Database name |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function infoDatabase($dbName) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | public function tableExists($table) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @inheritdoc |
||
| 166 | */ |
||
| 167 | public function listTables() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | public function createTable($name, array $elements = [], $params = '') |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | public function clearTable($name) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @inheritdoc |
||
| 198 | */ |
||
| 199 | public function removeTable($name) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | public function fieldExists($field, $table) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @inheritdoc |
||
| 220 | */ |
||
| 221 | public function listFields($table) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @inheritdoc |
||
| 258 | */ |
||
| 259 | public function fieldInfo($field, $table) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @inheritdoc |
||
| 266 | */ |
||
| 267 | public function switchDatabase($dbName) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @inheritdoc |
||
| 278 | */ |
||
| 279 | public function insert($table, array $line = [], $multi = false) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @inheritdoc |
||
| 308 | */ |
||
| 309 | public function update($table, array $elements = [], $conditions = '') |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @inheritdoc |
||
| 331 | */ |
||
| 332 | public function delete($table, $conditions, array $ph = []) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @inheritdoc |
||
| 339 | */ |
||
| 340 | public function exists($table, array $params = []) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @inheritdoc |
||
| 356 | */ |
||
| 357 | public function count($subQuery = '', $table = '') |
||
| 372 | } |
||
| 373 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: