Complex classes like SqliteDb 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 SqliteDb, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class SqliteDb extends MySqlDb { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | 4 | protected function alterTableDb(array $alterDef, array $options = []) { |
|
23 | 4 | $this->alterTableMigrate($alterDef, $options); |
|
24 | 4 | } |
|
25 | |||
26 | /** |
||
27 | * Alter a table by creating a new table and copying the old table's data to it. |
||
28 | * |
||
29 | * @param array $alterDef The new definition. |
||
30 | * @param array $options An array of options for the migration. |
||
31 | */ |
||
32 | 4 | private function alterTableMigrate(array $alterDef, array $options = []) { |
|
33 | 4 | $table = $alterDef['name']; |
|
|
|||
34 | 4 | $currentDef = $this->fetchTableDef($table); |
|
35 | |||
36 | // Merge the table definitions if we aren't dropping stuff. |
||
37 | 4 | if (!self::val(Db::OPTION_DROP, $options)) { |
|
38 | 3 | $tableDef = $this->mergeTableDefs($currentDef, $alterDef); |
|
39 | 3 | } else { |
|
40 | 1 | $tableDef = $alterDef['def']; |
|
41 | } |
||
42 | |||
43 | // Drop all of the indexes on the current table. |
||
44 | 4 | foreach (self::val('indexes', $currentDef, []) as $indexDef) { |
|
45 | 4 | if (self::val('type', $indexDef, Db::INDEX_IX) === Db::INDEX_IX) { |
|
46 | 2 | $this->dropIndex($indexDef['name']); |
|
47 | 2 | } |
|
48 | 4 | } |
|
49 | |||
50 | 4 | $tmpTable = $table.'_'.time(); |
|
51 | |||
52 | // Rename the current table. |
||
53 | 4 | $this->renameTable($table, $tmpTable); |
|
54 | |||
55 | // Create the new table. |
||
56 | 4 | $this->createTableDb($tableDef, $options); |
|
57 | |||
58 | // Figure out the columns that we can insert. |
||
59 | 4 | $columns = array_keys(array_intersect_key($tableDef['columns'], $currentDef['columns'])); |
|
60 | |||
61 | // Build the insert/select statement. |
||
62 | 4 | $sql = 'insert into '.$this->prefixTable($table)."\n". |
|
63 | 4 | $this->bracketList($columns, '`')."\n". |
|
64 | 4 | $this->buildSelect($tmpTable, [], ['columns' => $columns]); |
|
65 | |||
66 | 4 | $this->queryDefine($sql); |
|
67 | |||
68 | // Drop the temp table. |
||
69 | 4 | $this->dropTable($tmpTable); |
|
70 | 4 | } |
|
71 | |||
72 | /** |
||
73 | * Rename a table. |
||
74 | * |
||
75 | * @param string $old The old name of the table. |
||
76 | * @param string $new The new name of the table. |
||
77 | */ |
||
78 | 4 | private function renameTable($old, $new) { |
|
79 | $renameSql = 'alter table '. |
||
80 | 4 | $this->prefixTable($old). |
|
81 | 4 | ' rename to '. |
|
82 | 4 | $this->prefixTable($new); |
|
83 | 4 | $this->queryDefine($renameSql); |
|
84 | 4 | } |
|
85 | |||
86 | /** |
||
87 | * Merge a table def with its alter def so that no columns/indexes are lost in an alter. |
||
88 | * |
||
89 | * @param array $tableDef The table def. |
||
90 | * @param array $alterDef The alter def. |
||
91 | * @return array The new table def. |
||
92 | */ |
||
93 | 3 | private function mergeTableDefs(array $tableDef, array $alterDef) { |
|
94 | 3 | $result = $tableDef; |
|
95 | |||
96 | 3 | if ($this->findPrimaryKeyIndex($alterDef['add']['indexes'])) { |
|
97 | 2 | $remove = null; |
|
98 | 2 | foreach ($result['indexes'] as $i => $index) { |
|
99 | 2 | if ($index['type'] === Db::INDEX_PK) { |
|
100 | 2 | $remove = $i; |
|
101 | 2 | } |
|
102 | 2 | } |
|
103 | 2 | if ($remove !== null) { |
|
104 | 2 | unset($result['indexes'][$i]); |
|
105 | 2 | } |
|
106 | 2 | } |
|
107 | |||
108 | 3 | $result['columns'] = array_merge($result['columns'], $alterDef['def']['columns']); |
|
109 | 3 | $result['indexes'] = array_merge($result['indexes'], $alterDef['add']['indexes']); |
|
110 | |||
111 | 3 | return $result; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Drop an index. |
||
116 | * |
||
117 | * @param string $index The name of the index to drop. |
||
118 | */ |
||
119 | 2 | protected function dropIndex($index) { |
|
120 | $sql = 'drop index if exists '. |
||
121 | 2 | $this->escape($index); |
|
122 | 2 | $this->queryDefine($sql); |
|
123 | 2 | } |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 28 | protected function buildInsert($table, array $row, $options = []) { |
|
129 | 28 | if (self::val(Db::OPTION_UPSERT, $options)) { |
|
130 | throw new \Exception("Upsert is not supported."); |
||
131 | 28 | } elseif (self::val(Db::OPTION_IGNORE, $options)) { |
|
132 | 2 | $sql = 'insert or ignore into '; |
|
133 | 28 | } elseif (self::val(Db::OPTION_REPLACE, $options)) { |
|
134 | 2 | $sql = 'insert or replace into '; |
|
135 | 2 | } else { |
|
136 | 26 | $sql = 'insert into '; |
|
137 | } |
||
138 | 28 | $sql .= $this->prefixTable($table); |
|
139 | |||
140 | // Add the list of values. |
||
141 | $sql .= |
||
142 | 28 | "\n".$this->bracketList(array_keys($row), '`'). |
|
143 | 28 | "\nvalues".$this->bracketList($row, "'"); |
|
144 | |||
145 | 28 | return $sql; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | protected function buildLike($column, $value) { |
||
152 | return "$column like ".$this->quote($value)." escape '\\'"; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 5 | protected function buildUpdate($table, array $set, array $where, array $options = []) { |
|
159 | $sql = 'update '. |
||
160 | 5 | (empty($options[Db::OPTION_IGNORE]) ? '' : 'or ignore '). |
|
161 | 5 | $this->prefixTable($table). |
|
162 | 5 | "\nset\n "; |
|
163 | |||
164 | 5 | $parts = []; |
|
165 | 5 | foreach ($set as $key => $value) { |
|
166 | 5 | $escapedKey = $this->escape($key); |
|
167 | 5 | $parts[] = "$escapedKey = ".$this->quote($value, $escapedKey); |
|
168 | 5 | } |
|
169 | 5 | $sql .= implode(",\n ", $parts); |
|
170 | |||
171 | 5 | if (!empty($where)) { |
|
172 | 5 | $sql .= "\nwhere ".$this->buildWhere($where, Db::OP_AND); |
|
173 | 5 | } |
|
174 | |||
175 | 5 | return $sql; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Construct a column definition string from an array defintion. |
||
180 | * |
||
181 | * @param string $name The name of the column. |
||
182 | * @param array $cdef The column definition. |
||
183 | * @return string Returns a string representing the column definition. |
||
184 | */ |
||
185 | 11 | protected function columnDefString($name, array $cdef) { |
|
186 | $cdef += [ |
||
187 | 11 | 'autoIncrement' => false, |
|
188 | 11 | 'primary' => false, |
|
189 | 'allowNull' => false |
||
190 | 11 | ]; |
|
191 | |||
192 | // Auto-increments MUST be of type integer. |
||
193 | 11 | if ($cdef['autoIncrement']) { |
|
194 | 4 | $cdef['dbtype'] = 'integer'; |
|
195 | 4 | } |
|
196 | |||
197 | 11 | $result = $this->escape($name).' '.$this->nativeDbType($cdef); |
|
198 | |||
199 | 11 | if ($cdef['primary'] && $cdef['autoIncrement']) { |
|
200 | // if (val('autoincrement', $def)) { |
||
201 | 4 | $result .= ' primary key autoincrement'; |
|
202 | 4 | $cdef['primary'] = true; |
|
203 | // } |
||
204 | 4 | } else { |
|
205 | 11 | if (!$cdef['allowNull']) { |
|
206 | 10 | $result .= ' not null'; |
|
207 | 10 | } |
|
208 | |||
209 | 11 | if (isset($cdef['default'])) { |
|
210 | 8 | $result .= ' default '.$this->quote($cdef['default']); |
|
211 | 8 | } |
|
212 | } |
||
213 | |||
214 | 11 | return $result; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 11 | protected function nativeDbType(array $type) { |
|
221 | 11 | static $translations = ['bool' => 'boolean', 'byte' => 'tinyint', 'short' => 'smallint', 'long' => 'bigint']; |
|
222 | |||
223 | // Translate the dbtype to a MySQL native type. |
||
224 | 11 | if (isset($translations[$type['dbtype']])) { |
|
225 | 1 | $type['dbtype'] = $translations[$type['dbtype']]; |
|
226 | 1 | } |
|
227 | |||
228 | 11 | if (!empty($type['autoIncrement'])) { |
|
229 | 4 | $type['dbtype'] = 'integer'; |
|
230 | 4 | } |
|
231 | |||
232 | // Unsigned is represented differently in MySQL. |
||
233 | 11 | $unsigned = !empty($type['unsigned']) && empty($type['autoIncrement']); |
|
234 | 11 | unset($type['unsigned']); |
|
235 | |||
236 | 11 | $dbType = static::dbType($type).($unsigned ? ' unsigned' : ''); |
|
237 | |||
238 | 11 | return $dbType; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | 11 | protected function createTableDb(array $tableDef, array $options = []) { |
|
245 | 11 | $table = $tableDef['name']; |
|
246 | 11 | $parts = []; |
|
247 | |||
248 | // Make sure the primary key columns are defined first and in order. |
||
249 | 11 | $autoInc = false; |
|
250 | 11 | if ($pkIndex = $this->findPrimaryKeyIndex($tableDef['indexes'])) { |
|
251 | 8 | foreach ($pkIndex['columns'] as $column) { |
|
252 | 8 | $cdef = $tableDef['columns'][$column]; |
|
253 | 8 | $parts[] = $this->columnDefString($column, $cdef); |
|
254 | 8 | $autoInc |= !empty($cdef['autoIncrement']); |
|
255 | 8 | unset($tableDef['columns'][$column]); |
|
256 | 8 | } |
|
257 | 8 | } |
|
258 | |||
259 | 11 | foreach ($tableDef['columns'] as $name => $cdef) { |
|
260 | 10 | $parts[] = $this->columnDefString($name, $cdef); |
|
261 | 11 | } |
|
262 | |||
263 | // Add the primary key index. |
||
264 | 11 | if (isset($pkIndex) && !$autoInc) { |
|
265 | 4 | $parts[] = 'primary key '.$this->bracketList($pkIndex['columns'], '`'); |
|
266 | 4 | } |
|
267 | |||
268 | 11 | $fullTableName = $this->prefixTable($table); |
|
269 | 11 | $sql = "create table $fullTableName (\n ". |
|
270 | 11 | implode(",\n ", $parts). |
|
271 | 11 | "\n)"; |
|
272 | |||
273 | 11 | $this->queryDefine($sql); |
|
274 | |||
275 | // Add the rest of the indexes. |
||
276 | 11 | foreach (self::val('indexes', $tableDef, []) as $index) { |
|
277 | 11 | if (self::val('type', $index, Db::INDEX_IX) !== Db::INDEX_PK) { |
|
278 | 7 | $this->createIndex($table, $index, $options); |
|
279 | 7 | } |
|
280 | 11 | } |
|
281 | 11 | } |
|
282 | |||
283 | /** |
||
284 | * Create an index. |
||
285 | * |
||
286 | * @param string $table The name of the table to create the index on. |
||
287 | * @param array $indexDef The index definition. |
||
288 | * @param array $options Additional options for the index creation. |
||
289 | */ |
||
290 | 7 | public function createIndex($table, array $indexDef, $options = []) { |
|
302 | |||
303 | /** |
||
304 | * Force a value into the appropriate php type based on its Sqlite type. |
||
305 | * |
||
306 | * @param mixed $value The value to force. |
||
307 | * @param string $type The sqlite type name. |
||
308 | * @return mixed Returns $value cast to the appropriate type. |
||
309 | */ |
||
310 | 3 | protected function forceType($value, $type) { |
|
311 | 3 | $type = strtolower($type); |
|
312 | |||
313 | 3 | if ($type === 'null') { |
|
325 | |||
326 | /** |
||
327 | * Get the columns for a table.. |
||
328 | * |
||
329 | * @param string $table The table to get the columns for. |
||
330 | * @return array|null Returns an array of columns. |
||
331 | */ |
||
332 | 4 | protected function fetchColumnDefsDb($table) { |
|
371 | |||
372 | /** |
||
373 | * Get the indexes for a table. |
||
374 | * |
||
375 | * @param string $table The name of the table to get the indexes for or an empty string to get all indexes. |
||
376 | * @return array|null |
||
377 | */ |
||
378 | 4 | protected function fetchIndexesDb($table = '') { |
|
403 | |||
404 | /** |
||
405 | * Get the primary or secondary keys from the given rows. |
||
406 | * |
||
407 | * @param string $table The name of the table. |
||
408 | * @param array $row The row to examine. |
||
409 | * @param bool $quick Whether or not to quickly look for <tablename>ID for the primary key. |
||
410 | * @return array|null Returns the primary keys and values from {@link $rows} or null if the primary key isn't found. |
||
411 | */ |
||
412 | 2 | private function getPKValue($table, array $row, $quick = false) { |
|
431 | |||
432 | /** |
||
433 | * Get the all of table names in the database. |
||
434 | * |
||
435 | * @return array Returns an array of table names. |
||
436 | */ |
||
437 | protected function fetchTableNamesDb() { |
||
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | 8 | public function insert($table, array $row, array $options = []) { |
|
490 | |||
491 | /** |
||
492 | * Optionally quote a where value. |
||
493 | * |
||
494 | * @param mixed $value The value to quote. |
||
495 | * @param string $column The name of the column being operated on. |
||
496 | * @return string Returns the value, optionally quoted. |
||
497 | * @internal param bool $quote Whether or not to quote the value. |
||
498 | */ |
||
499 | 35 | public function quote($value, $column = '') { |
|
515 | } |
||
516 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.