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 | } 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 | } |
||
48 | } |
||
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 | } |
||
102 | } |
||
103 | 2 | if ($remove !== null) { |
|
104 | 2 | unset($result['indexes'][$i]); |
|
105 | } |
||
106 | } |
||
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 | 27 | } elseif (self::val(Db::OPTION_REPLACE, $options)) { |
|
134 | 2 | $sql = 'insert or replace into '; |
|
135 | } 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 | } |
||
169 | 5 | $sql .= implode(",\n ", $parts); |
|
170 | |||
171 | 5 | if (!empty($where)) { |
|
172 | 5 | $sql .= "\nwhere ".$this->buildWhere($where, Db::OP_AND); |
|
173 | } |
||
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) { |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 11 | protected function nativeDbType(array $type) { |
|
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | 11 | protected function createTableDb(array $tableDef, array $options = []) { |
|
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) { |
|
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.