Complex classes like MySQL 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 MySQL, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MySQL implements Database { |
||
| 19 | /** @var array */ |
||
| 20 | private static $tableFields = array(); |
||
| 21 | /** @var PDO */ |
||
| 22 | private $pdo; |
||
| 23 | /** @var bool */ |
||
| 24 | private $outerTransaction = false; |
||
| 25 | /** @var AliasRegistry */ |
||
| 26 | private $aliasRegistry; |
||
| 27 | /** @var int */ |
||
| 28 | private $transactionLevel = 0; |
||
| 29 | /** @var QueryLoggers */ |
||
| 30 | private $queryLoggers = 0; |
||
| 31 | /** @var MySQLExceptionInterpreter */ |
||
| 32 | private $exceptionInterpreter = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param PDO $pdo |
||
| 36 | */ |
||
| 37 | public function __construct(PDO $pdo) { |
||
| 38 | if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
||
| 39 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 40 | } |
||
| 41 | $this->pdo = $pdo; |
||
| 42 | $this->aliasRegistry = new AliasRegistry(); |
||
| 43 | $this->queryLoggers = new QueryLoggers(); |
||
| 44 | $this->exceptionInterpreter = new MySQLExceptionInterpreter(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return QueryLoggers |
||
| 49 | */ |
||
| 50 | public function getQueryLoggers() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return AliasRegistry |
||
| 56 | */ |
||
| 57 | public function getAliasRegistry() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $query |
||
| 63 | * @throws Exception |
||
| 64 | * @return QueryStatement |
||
| 65 | */ |
||
| 66 | public function query($query) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $query |
||
| 75 | * @throws Exception |
||
| 76 | * @return QueryStatement |
||
| 77 | */ |
||
| 78 | public function prepare($query) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $query |
||
| 87 | * @param array $params |
||
| 88 | * @return int |
||
| 89 | */ |
||
| 90 | public function exec($query, array $params = array()) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getLastInsertId() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $table |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public function getTableFields($table) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param mixed $expression |
||
| 127 | * @param array $arguments |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function quoteExpression($expression, array $arguments = array()) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param mixed $value |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function quote($value) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $field |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function quoteField($field) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param array $fields |
||
| 183 | * @return RunnableSelect |
||
| 184 | */ |
||
| 185 | public function select(array $fields = null) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param array $fields |
||
| 195 | * @return Builder\RunnableInsert |
||
| 196 | */ |
||
| 197 | public function insert(array $fields = null) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param array $fields |
||
| 207 | * @return Builder\RunnableUpdate |
||
| 208 | */ |
||
| 209 | public function update(array $fields = null) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return Builder\RunnableDelete |
||
| 219 | */ |
||
| 220 | public function delete() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function transactionStart() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return $this |
||
| 241 | * @throws \Exception |
||
| 242 | */ |
||
| 243 | public function transactionCommit() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return $this |
||
| 251 | * @throws \Exception |
||
| 252 | */ |
||
| 253 | public function transactionRollback() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param callable|null $callback |
||
| 261 | * @return mixed |
||
| 262 | * @throws \Exception |
||
| 263 | * @throws null |
||
| 264 | */ |
||
| 265 | public function dryRun($callback = null) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param int|callable $tries |
||
| 297 | * @param callable|null $callback |
||
| 298 | * @return mixed |
||
| 299 | * @throws \Exception |
||
| 300 | * @throws null |
||
| 301 | */ |
||
| 302 | public function transaction($tries = 1, $callback = null) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param callable $fn |
||
| 337 | * @return $this |
||
| 338 | * @throws \Exception |
||
| 339 | */ |
||
| 340 | private function transactionEnd($fn) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $query |
||
| 357 | * @param callable $fn |
||
| 358 | * @return QueryStatement |
||
| 359 | * @throws Exception |
||
| 360 | */ |
||
| 361 | private function buildQueryStatement($query, $fn) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param callable $fn |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | private function exceptionHandler($fn) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | private function genUniqueId() { |
||
| 398 | } |
||
| 399 |