Complex classes like MysqlInformationSchema 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 MysqlInformationSchema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class MysqlInformationSchema extends Source\AbstractSource |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Schema name |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $schema; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var MysqlConnectionAdapter |
||
| 20 | */ |
||
| 21 | protected $adapter; |
||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Used to restore innodb stats mysql global variable |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $mysql_innodbstats_value; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $localCache = array(); |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | protected $useLocalCaching = true; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected static $fullyCachedSchemas = array(); |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * |
||
| 53 | * @param \PDO|\mysqli $connection |
||
| 54 | * @param string $schema default schema, taken from adapter if not given |
||
| 55 | * @throws Exception\InvalidArgumentException for invalid connection |
||
| 56 | * @throws Exception\InvalidUsageException thrown if no schema can be found. |
||
| 57 | */ |
||
| 58 | 24 | public function __construct($connection, $schema = null) |
|
| 59 | { |
||
| 60 | try { |
||
| 61 | 24 | $this->adapter = new MysqlConnectionAdapter($connection); |
|
| 62 | 24 | } catch (Exception\InvalidArgumentException $e) { |
|
| 63 | $msg = "MysqlInformationSchema requires a valid 'mysqli' or 'pdo:mysql' connection object ({$e->getMessage()})."; |
||
| 64 | throw new Exception\InvalidArgumentException($msg); |
||
| 65 | } |
||
| 66 | |||
| 67 | 24 | if ($schema === null) { |
|
| 68 | 24 | $schema = $this->adapter->getCurrentSchema(); |
|
| 69 | 24 | if ($schema === false || $schema == '') { |
|
| 70 | $msg = "Database name (schema) parameter missing and no default schema set on connection"; |
||
| 71 | throw new Exception\InvalidUsageException($msg); |
||
| 72 | } |
||
| 73 | 24 | } |
|
| 74 | |||
| 75 | 24 | $this->setDefaultSchema($schema); |
|
| 76 | 24 | } |
|
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Return all uniques keys defined for a table. |
||
| 81 | * |
||
| 82 | * By default it does not include the primary key, simply set |
||
| 83 | * the $include_primary parameter to true to get it. In this case |
||
| 84 | * the associative key will be 'PRIMARY'. |
||
| 85 | * |
||
| 86 | * If no unique keys can be found returns an empty array |
||
| 87 | * |
||
| 88 | * |
||
| 89 | * @param string $table table name |
||
| 90 | * @param boolean $include_primary include primary keys in the list (indexed as PRIMARY) |
||
| 91 | * @throws Exception\InvalidArgumentException |
||
| 92 | * @throws Exception\ErrorException |
||
| 93 | * @throws Exception\ExceptionInterface |
||
| 94 | * @throws Exception\TableNotFoundException |
||
| 95 | * @return array associative array 'index_name' => ['col1', 'col2'], 'index_name_2' => ['col3'] |
||
| 96 | */ |
||
| 97 | 1 | public function getUniqueKeys($table, $include_primary = false) |
|
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Return indexes information on a table |
||
| 117 | * |
||
| 118 | * @param string $table table name |
||
| 119 | * |
||
| 120 | * @throws Exception\InvalidArgumentException |
||
| 121 | * @throws Exception\ErrorException |
||
| 122 | * @throws Exception\ExceptionInterface |
||
| 123 | * @throws Exception\TableNotFoundException |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getIndexesInformation($table) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Return unique table primary key |
||
| 135 | * |
||
| 136 | * @throws Exception\InvalidArgumentException |
||
| 137 | * @throws Exception\ErrorException |
||
| 138 | * @throws Exception\NoPrimaryKeyException when no pk |
||
| 139 | * @throws Exception\MultiplePrimaryKeyException when multiple pk found |
||
| 140 | * @throws Exception\ExceptionInterface |
||
| 141 | * @throws Exception\TableNotFoundException |
||
| 142 | * |
||
| 143 | * @param string $table |
||
| 144 | * |
||
| 145 | * @return string|int primary key |
||
| 146 | */ |
||
| 147 | 5 | public function getPrimaryKey($table) |
|
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Return composite primary keys |
||
| 160 | * |
||
| 161 | * @throws Exception\InvalidArgumentException |
||
| 162 | * @throws Exception\ErrorException |
||
| 163 | * @throws Exception\NoPrimaryKeyException |
||
| 164 | * @throws Exception\ExceptionInterface |
||
| 165 | * @throws Exception\TableNotFoundException |
||
| 166 | * |
||
| 167 | * @param string $table |
||
| 168 | * |
||
| 169 | * @return array primary keys |
||
| 170 | */ |
||
| 171 | 9 | public function getPrimaryKeys($table) |
|
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Return column information |
||
| 184 | * |
||
| 185 | * @throws Exception\InvalidArgumentException |
||
| 186 | * @throws Exception\ErrorException |
||
| 187 | * @throws Exception\ExceptionInterface |
||
| 188 | * @throws Exception\TableNotFoundException |
||
| 189 | * |
||
| 190 | * @param string $table |
||
| 191 | * @return array associative array [column_name => infos] |
||
| 192 | */ |
||
| 193 | 1 | public function getColumnsInformation($table) |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Return relations information |
||
| 202 | * |
||
| 203 | * @throws Exception\InvalidArgumentException |
||
| 204 | * @throws Exception\ErrorException |
||
| 205 | * @throws Exception\ExceptionInterface |
||
| 206 | * @throws Exception\TableNotFoundException |
||
| 207 | * |
||
| 208 | * @param string $table |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | 1 | public function getRelations($table) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Return table informations |
||
| 220 | * |
||
| 221 | * @throws Exception\InvalidArgumentException |
||
| 222 | * @throws Exception\ErrorException |
||
| 223 | * @throws Exception\ExceptionInterface |
||
| 224 | * |
||
| 225 | * @return array associative array indexed by table_name |
||
| 226 | */ |
||
| 227 | 3 | public function getTablesInformation() |
|
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Get a table configuration |
||
| 237 | * |
||
| 238 | * @throws Exception\ErrorException |
||
| 239 | * @throws Exception\TableNotFoundException |
||
| 240 | * |
||
| 241 | * @param string $table table name |
||
| 242 | * @param boolean $include_options include extended information |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | 11 | public function getTableConfig($table, $include_options = false) |
|
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Get schema configuration |
||
| 274 | * |
||
| 275 | * @throws Exception\ErrorException |
||
| 276 | * @throws Exception\SchemaNotFoundException |
||
| 277 | * |
||
| 278 | * @param boolean $include_options include extended information |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 2 | public function getSchemaConfig($include_options = false) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Return object (table/schema) configuration |
||
| 301 | * |
||
| 302 | * @throws Exception\ErrorException |
||
| 303 | * |
||
| 304 | * @param string $table |
||
| 305 | * @param boolean $include_options |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | 6 | protected function getObjectConfig($table = null, $include_options = false) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Disable innodbstats will increase speed of metadata lookups |
||
| 544 | * |
||
| 545 | * @return void |
||
| 546 | */ |
||
| 547 | 6 | protected function disableInnoDbStats() |
|
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * Restore old innodbstats variable |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | 6 | protected function restoreInnoDbStats() |
|
| 581 | |||
| 582 | |||
| 583 | /** |
||
| 584 | * |
||
| 585 | * @param string $table |
||
| 586 | * @throws Exception\InvalidArgumentException |
||
| 587 | * @throws Exception\TableNotFoundException |
||
| 588 | * |
||
| 589 | */ |
||
| 590 | 14 | protected function loadCacheInformation($table = null) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Clear local cache information for the current schema |
||
| 609 | * |
||
| 610 | * @throws Exception\InvalidArgumentException |
||
| 611 | */ |
||
| 612 | 1 | public function clearCacheInformation() |
|
| 622 | } |
||
| 623 |