Complex classes like MySqlDataManager 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 MySqlDataManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MySqlDataManager extends AbstractDataManager |
||
| 14 | { |
||
| 15 | private $columns = null; |
||
| 16 | |||
| 17 | private $connection; |
||
| 18 | |||
| 19 | private $database; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * cache |
||
| 23 | * @var array|null |
||
| 24 | */ |
||
| 25 | private $databases = null; |
||
| 26 | |||
| 27 | 6 | public function __construct(PDO $connection) |
|
| 31 | |||
| 32 | public function databases(array $sorting = []) |
||
| 33 | { |
||
| 34 | if ($this->databases !== null) { |
||
| 35 | return $this->databases; |
||
| 36 | } |
||
| 37 | |||
| 38 | $query = 'SELECT information_schema.SCHEMATA.*, count(information_schema.TABLES.TABLE_NAME) AS tables_count, SUM(information_schema.TABLES.DATA_LENGTH) AS size |
||
| 39 | FROM information_schema.SCHEMATA |
||
| 40 | LEFT JOIN information_schema.TABLES ON information_schema.SCHEMATA.SCHEMA_NAME = information_schema.TABLES.TABLE_SCHEMA |
||
| 41 | GROUP BY information_schema.SCHEMATA.SCHEMA_NAME |
||
| 42 | ORDER BY information_schema.SCHEMATA.SCHEMA_NAME'; |
||
| 43 | |||
| 44 | $databases = []; |
||
| 45 | foreach ($this->connection->query($query)->fetchAll(PDO::FETCH_ASSOC) as $database) { |
||
| 46 | $databases[$database['SCHEMA_NAME']] = [ |
||
| 47 | 'database' => $database['SCHEMA_NAME'], |
||
| 48 | 'charset' => $database['DEFAULT_CHARACTER_SET_NAME'], |
||
| 49 | 'collation' => $database['DEFAULT_COLLATION_NAME'], |
||
| 50 | 'tables_count' => $database['tables_count'], |
||
| 51 | 'size' => $database['size'] ?: 0, |
||
| 52 | ]; |
||
| 53 | } |
||
| 54 | $this->databases = $databases; |
||
| 55 | return Multisort::sort($databases, $sorting); |
||
| 56 | } |
||
| 57 | |||
| 58 | protected function getDatabaseNameColumn() |
||
| 62 | |||
| 63 | public function tables(array $sorting = []) |
||
| 64 | { |
||
| 65 | $tables = [ |
||
| 66 | MySqlDriver::TYPE_TABLE => [], |
||
| 67 | MySqlDriver::TYPE_VIEW => [], |
||
| 68 | ]; |
||
| 69 | $type = 'BASE TABLE'; |
||
| 70 | if ($this->database == 'information_schema') { |
||
| 71 | $type = 'SYSTEM VIEW'; |
||
| 72 | } |
||
| 73 | |||
| 74 | foreach ($this->connection->query("SELECT * FROM information_schema.TABLES WHERE TABLE_TYPE = '$type' AND TABLE_SCHEMA = '{$this->database}' ORDER BY TABLE_NAME")->fetchAll(PDO::FETCH_ASSOC) as $table) { |
||
| 75 | $tables[MySqlDriver::TYPE_TABLE][$table['TABLE_NAME']] = [ |
||
| 76 | 'table' => $table['TABLE_NAME'], |
||
| 77 | 'engine' => $table['ENGINE'], |
||
| 78 | 'collation' => $table['TABLE_COLLATION'], |
||
| 79 | 'data_length' => $table['DATA_LENGTH'], |
||
| 80 | 'index_length' => $table['INDEX_LENGTH'], |
||
| 81 | 'data_free' => $table['DATA_FREE'], |
||
| 82 | 'autoincrement' => $table['AUTO_INCREMENT'], |
||
| 83 | 'rows' => $table['TABLE_ROWS'], |
||
| 84 | ]; |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach ($this->connection->query("SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '{$this->database}' ORDER BY TABLE_NAME")->fetchAll(PDO::FETCH_ASSOC) as $view) { |
||
| 88 | $tables[MySqlDriver::TYPE_VIEW][$view['TABLE_NAME']] = [ |
||
| 89 | 'view' => $view['TABLE_NAME'], |
||
| 90 | 'check_option' => $view['CHECK_OPTION'], |
||
| 91 | 'is_updatable' => $view['IS_UPDATABLE'], |
||
| 92 | 'definer' => $view['DEFINER'], |
||
| 93 | 'security_type' => $view['SECURITY_TYPE'], |
||
| 94 | 'character_set' => $view['CHARACTER_SET_CLIENT'], |
||
| 95 | 'collation' => $view['COLLATION_CONNECTION'], |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | return [ |
||
| 99 | MySqlDriver::TYPE_TABLE => Multisort::sort($tables[MySqlDriver::TYPE_TABLE], $sorting), |
||
| 100 | MySqlDriver::TYPE_VIEW => Multisort::sort($tables[MySqlDriver::TYPE_VIEW], $sorting), |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function itemsCount($type, $table, array $filter = []) |
||
| 109 | |||
| 110 | public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = []) |
||
| 111 | { |
||
| 112 | $primaryColumns = $this->getPrimaryColumns($type, $table); |
||
| 113 | $query = sprintf('SELECT * FROM `%s`', $table); |
||
| 114 | $query .= $this->createWhere($filter); |
||
| 115 | $query .= $this->createOrderBy($sorting); |
||
| 116 | $query .= ' LIMIT ' . (($page - 1) * $onPage) . ', ' . $onPage; |
||
| 117 | $items = []; |
||
| 118 | foreach ($this->connection->query($query)->fetchAll(PDO::FETCH_ASSOC) as $item) { |
||
| 119 | $pk = []; |
||
| 120 | foreach ($primaryColumns as $primaryColumn) { |
||
| 121 | $pk[] = $item[$primaryColumn]; |
||
| 122 | } |
||
| 123 | $items[md5(implode('|', $pk))] = $item; |
||
| 124 | } |
||
| 125 | return $items; |
||
| 126 | } |
||
| 127 | |||
| 128 | private function createWhere(array $filter) |
||
| 129 | { |
||
| 130 | if (empty($filter)) { |
||
| 131 | return ''; |
||
| 132 | } |
||
| 133 | |||
| 134 | $operatorsMap = [ |
||
| 135 | Filter::OPERATOR_EQUAL => '= "%s"', |
||
| 136 | Filter::OPERATOR_GREATER_THAN => '> "%s"', |
||
| 137 | Filter::OPERATOR_GREATER_THAN_OR_EQUAL => '>= "%s"', |
||
| 138 | Filter::OPERATOR_LESS_THAN => '< "%s"', |
||
| 139 | Filter::OPERATOR_LESS_THAN_OR_EQUAL => '<= "%s"', |
||
| 140 | Filter::OPERATOR_NOT_EQUAL => '!= %s', |
||
| 141 | Filter::OPERATOR_CONTAINS => 'LIKE "%%%s%%"', |
||
| 142 | Filter::OPERATOR_NOT_CONTAINS => 'NOT LIKE "%%%s%%"', |
||
| 143 | Filter::OPERATOR_STARTS_WITH => 'LIKE "%s%%"', |
||
| 144 | Filter::OPERATOR_ENDS_WITH => 'LIKE "%%%s"', |
||
| 145 | Filter::OPERATOR_IS_NULL => 'IS NULL', |
||
| 146 | Filter::OPERATOR_IS_NOT_NULL => 'IS NOT NULL', |
||
| 147 | Filter::OPERATOR_IS_IN => 'IN (%s)', |
||
| 148 | Filter::OPERATOR_IS_NOT_IN => 'NOT IN (%s)', |
||
| 149 | ]; |
||
| 150 | |||
| 151 | $where = ' WHERE '; |
||
| 152 | $whereParts = []; |
||
| 153 | foreach ($filter as $filterPart) { |
||
| 154 | foreach ($filterPart as $key => $filterSettings) { |
||
| 155 | foreach ($filterSettings as $operator => $value) { |
||
| 156 | if (!isset($operatorsMap[$operator])) { |
||
| 157 | throw new OperatorNotSupportedException('Operator "' . $operator . '" is not supported.'); |
||
| 158 | } |
||
| 159 | $whereParts[] = "`$key` " . sprintf($operatorsMap[$operator], $value); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | $where .= implode(' AND ', $whereParts); |
||
| 164 | return $where; |
||
| 165 | } |
||
| 166 | |||
| 167 | private function createOrderBy(array $sorting) |
||
| 168 | { |
||
| 169 | if (empty($sorting)) { |
||
| 170 | return ''; |
||
| 171 | } |
||
| 172 | $orderBy = ' ORDER BY '; |
||
| 173 | $order = []; |
||
| 174 | foreach ($sorting as $sort) { |
||
| 175 | foreach ($sort as $key => $direction) { |
||
| 176 | $direction = strtolower($direction) == 'asc' ? 'ASC' : 'DESC'; |
||
| 177 | $order[] = "`$key` $direction"; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | $orderBy .= implode(', ', $order); |
||
| 181 | return $orderBy; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function loadItem($type, $table, $item) |
||
| 190 | |||
| 191 | public function deleteItem($type, $table, $item) |
||
| 197 | |||
| 198 | public function deleteTable($type, $table) |
||
| 199 | { |
||
| 200 | if ($type === MySqlDriver::TYPE_TABLE) { |
||
| 201 | $query = sprintf('DROP TABLE `%s`', $table); |
||
| 202 | } elseif ($type === MySqlDriver::TYPE_VIEW) { |
||
| 203 | $query = sprintf('DROP VIEW `%s`', $table); |
||
| 204 | } else { |
||
| 205 | throw new InvalidArgumentException('Type "' . $type . '" is not supported'); |
||
| 206 | } |
||
| 207 | return $this->connection->query($query); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function deleteDatabase($database) |
||
| 215 | |||
| 216 | public function selectDatabase($database) |
||
| 222 | |||
| 223 | public function getPrimaryColumns($type, $table) |
||
| 224 | { |
||
| 225 | $primaryColumns = []; |
||
| 226 | $columns = []; |
||
| 227 | foreach ($this->getColumns($type, $table) as $column) { |
||
| 228 | $columns[] = $column['Field']; |
||
| 238 | |||
| 239 | public function getColumns($type, $table) |
||
| 255 | |||
| 256 | public function execute($commands) |
||
| 275 | } |
||
| 276 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.