Complex classes like DBSchemaManager 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 DBSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | abstract class DBSchemaManager { |
||
10 | |||
11 | /** |
||
12 | * |
||
13 | * @config |
||
14 | * Check tables when running /dev/build, and repair them if necessary. |
||
15 | * In case of large databases or more fine-grained control on how to handle |
||
16 | * data corruption in tables, you can disable this behaviour and handle it |
||
17 | * outside of this class, e.g. through a nightly system task with extended logging capabilities. |
||
18 | * |
||
19 | * @var boolean |
||
20 | */ |
||
21 | private static $check_and_repair_on_build = true; |
||
22 | |||
23 | /** |
||
24 | * Instance of the database controller this schema belongs to |
||
25 | * |
||
26 | * @var SS_Database |
||
27 | */ |
||
28 | protected $database = null; |
||
29 | |||
30 | /** |
||
31 | * If this is false, then information about database operations |
||
32 | * will be displayed, eg creation of tables. |
||
33 | * |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $supressOutput = false; |
||
37 | |||
38 | /** |
||
39 | * Injector injection point for database controller |
||
40 | * |
||
41 | * @param SS_Database $connector |
||
|
|||
42 | */ |
||
43 | public function setDatabase(SS_Database $database) { |
||
46 | |||
47 | /** |
||
48 | * The table list, generated by the tableList() function. |
||
49 | * Used by the requireTable() function. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $tableList; |
||
54 | |||
55 | /** |
||
56 | * Keeps track whether we are currently updating the schema. |
||
57 | * |
||
58 | * @var boolean |
||
59 | */ |
||
60 | protected $schemaIsUpdating = false; |
||
61 | |||
62 | /** |
||
63 | * Large array structure that represents a schema update transaction |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $schemaUpdateTransaction; |
||
68 | |||
69 | /** |
||
70 | * Enable supression of database messages. |
||
71 | */ |
||
72 | public function quiet() { |
||
75 | |||
76 | /** |
||
77 | * Execute the given SQL query. |
||
78 | * This abstract function must be defined by subclasses as part of the actual implementation. |
||
79 | * It should return a subclass of SS_Query as the result. |
||
80 | * |
||
81 | * @param string $sql The SQL query to execute |
||
82 | * @param int $errorLevel The level of error reporting to enable for the query |
||
83 | * @return SS_Query |
||
84 | */ |
||
85 | public function query($sql, $errorLevel = E_USER_ERROR) { |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Execute the given SQL parameterised query with the specified arguments |
||
92 | * |
||
93 | * @param string $sql The SQL query to execute. The ? character will denote parameters. |
||
94 | * @param array $parameters An ordered list of arguments. |
||
95 | * @param int $errorLevel The level of error reporting to enable for the query |
||
96 | * @return SS_Query |
||
97 | */ |
||
98 | public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) { |
||
101 | |||
102 | /** |
||
103 | * Initiates a schema update within a single callback |
||
104 | * |
||
105 | * @var callable $callback |
||
106 | * @throws Exception |
||
107 | */ |
||
108 | public function schemaUpdate($callback) { |
||
109 | // Begin schema update |
||
110 | $this->schemaIsUpdating = true; |
||
111 | |||
112 | // Update table list |
||
113 | $this->tableList = array(); |
||
114 | $tables = $this->tableList(); |
||
115 | foreach ($tables as $table) { |
||
116 | $this->tableList[strtolower($table)] = $table; |
||
117 | } |
||
118 | |||
119 | // Clear update list for client code to mess around with |
||
120 | $this->schemaUpdateTransaction = array(); |
||
121 | |||
122 | $error = null; |
||
123 | try { |
||
124 | |||
125 | // Yield control to client code |
||
126 | $callback(); |
||
127 | |||
128 | // If the client code has cancelled the update then abort |
||
129 | if(!$this->isSchemaUpdating()) return; |
||
130 | |||
131 | // End schema update |
||
132 | foreach ($this->schemaUpdateTransaction as $tableName => $changes) { |
||
133 | $advancedOptions = isset($changes['advancedOptions']) ? $changes['advancedOptions'] : null; |
||
134 | switch ($changes['command']) { |
||
135 | case 'create': |
||
136 | $this->createTable($tableName, $changes['newFields'], $changes['newIndexes'], |
||
137 | $changes['options'], $advancedOptions); |
||
138 | break; |
||
139 | |||
140 | case 'alter': |
||
141 | $this->alterTable($tableName, $changes['newFields'], $changes['newIndexes'], |
||
142 | $changes['alteredFields'], $changes['alteredIndexes'], |
||
143 | $changes['alteredOptions'], $advancedOptions); |
||
144 | break; |
||
145 | } |
||
146 | } |
||
147 | } catch(Exception $ex) { |
||
148 | $error = $ex; |
||
149 | } |
||
150 | // finally { |
||
151 | $this->schemaUpdateTransaction = null; |
||
152 | $this->schemaIsUpdating = false; |
||
153 | // } |
||
154 | |||
155 | if($error) throw $error; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Cancels the schema updates requested during (but not after) schemaUpdate() call. |
||
160 | */ |
||
161 | public function cancelSchemaUpdate() { |
||
165 | |||
166 | /** |
||
167 | * Returns true if we are during a schema update. |
||
168 | * |
||
169 | * @return boolean |
||
170 | */ |
||
171 | function isSchemaUpdating() { |
||
174 | |||
175 | /** |
||
176 | * Returns true if schema modifications were requested during (but not after) schemaUpdate() call. |
||
177 | * |
||
178 | * @return boolean |
||
179 | */ |
||
180 | public function doesSchemaNeedUpdating() { |
||
183 | |||
184 | // Transactional schema altering functions - they don't do anything except for update schemaUpdateTransaction |
||
185 | |||
186 | /** |
||
187 | * Instruct the schema manager to record a table creation to later execute |
||
188 | * |
||
189 | * @param string $table Name of the table |
||
190 | * @param array $options Create table options (ENGINE, etc.) |
||
191 | * @param array $advanced_options Advanced table creation options |
||
192 | */ |
||
193 | public function transCreateTable($table, $options = null, $advanced_options = null) { |
||
194 | $this->schemaUpdateTransaction[$table] = array( |
||
195 | 'command' => 'create', |
||
196 | 'newFields' => array(), |
||
197 | 'newIndexes' => array(), |
||
198 | 'options' => $options, |
||
199 | 'advancedOptions' => $advanced_options |
||
200 | ); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Instruct the schema manager to record a table alteration to later execute |
||
205 | * |
||
206 | * @param string $table Name of the table |
||
207 | * @param array $options Create table options (ENGINE, etc.) |
||
208 | * @param array $advanced_options Advanced table creation options |
||
209 | */ |
||
210 | public function transAlterTable($table, $options, $advanced_options) { |
||
211 | $this->transInitTable($table); |
||
212 | $this->schemaUpdateTransaction[$table]['alteredOptions'] = $options; |
||
213 | $this->schemaUpdateTransaction[$table]['advancedOptions'] = $advanced_options; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Instruct the schema manager to record a field to be later created |
||
218 | * |
||
219 | * @param string $table Name of the table to hold this field |
||
220 | * @param string $field Name of the field to create |
||
221 | * @param string $schema Field specification as a string |
||
222 | */ |
||
223 | public function transCreateField($table, $field, $schema) { |
||
227 | |||
228 | /** |
||
229 | * Instruct the schema manager to record an index to be later created |
||
230 | * |
||
231 | * @param string $table Name of the table to hold this index |
||
232 | * @param string $index Name of the index to create |
||
233 | * @param array $schema Already parsed index specification |
||
234 | */ |
||
235 | public function transCreateIndex($table, $index, $schema) { |
||
239 | |||
240 | /** |
||
241 | * Instruct the schema manager to record a field to be later updated |
||
242 | * |
||
243 | * @param string $table Name of the table to hold this field |
||
244 | * @param string $field Name of the field to update |
||
245 | * @param string $schema Field specification as a string |
||
246 | */ |
||
247 | public function transAlterField($table, $field, $schema) { |
||
251 | |||
252 | /** |
||
253 | * Instruct the schema manager to record an index to be later updated |
||
254 | * |
||
255 | * @param string $table Name of the table to hold this index |
||
256 | * @param string $index Name of the index to update |
||
257 | * @param array $schema Already parsed index specification |
||
258 | */ |
||
259 | public function transAlterIndex($table, $index, $schema) { |
||
263 | |||
264 | /** |
||
265 | * Handler for the other transXXX methods - mark the given table as being altered |
||
266 | * if it doesn't already exist |
||
267 | * |
||
268 | * @param string $table Name of the table to initialise |
||
269 | */ |
||
270 | protected function transInitTable($table) { |
||
282 | |||
283 | /** |
||
284 | * Generate the following table in the database, modifying whatever already exists |
||
285 | * as necessary. |
||
286 | * |
||
287 | * @todo Change detection for CREATE TABLE $options other than "Engine" |
||
288 | * |
||
289 | * @param string $table The name of the table |
||
290 | * @param array $fieldSchema A list of the fields to create, in the same form as DataObject::$db |
||
291 | * @param array $indexSchema A list of indexes to create. See {@link requireIndex()} |
||
292 | * The values of the array can be one of: |
||
293 | * - true: Create a single column index on the field named the same as the index. |
||
294 | * - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full |
||
295 | * control over the index. |
||
296 | * @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type |
||
297 | * @param string $options SQL statement to append to the CREATE TABLE call. |
||
298 | * @param array $extensions List of extensions |
||
299 | */ |
||
300 | public function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
||
367 | |||
368 | /** |
||
369 | * If the given table exists, move it out of the way by renaming it to _obsolete_(tablename). |
||
370 | * @param string $table The table name. |
||
371 | */ |
||
372 | public function dontRequireTable($table) { |
||
384 | |||
385 | /** |
||
386 | * Generate the given index in the database, modifying whatever already exists as necessary. |
||
387 | * |
||
388 | * The keys of the array are the names of the index. |
||
389 | * The values of the array can be one of: |
||
390 | * - true: Create a single column index on the field named the same as the index. |
||
391 | * - array('type' => 'index|unique|fulltext', 'value' => 'FieldA, FieldB'): This gives you full |
||
392 | * control over the index. |
||
393 | * |
||
394 | * @param string $table The table name. |
||
395 | * @param string $index The index name. |
||
396 | * @param string|array|boolean $spec The specification of the index in any |
||
397 | * loose format. See requireTable() for more information. |
||
398 | */ |
||
399 | public function requireIndex($table, $index, $spec) { |
||
433 | |||
434 | /** |
||
435 | * Splits a spec string safely, considering quoted columns, whitespace, |
||
436 | * and cleaning brackets |
||
437 | * |
||
438 | * @param string $spec The input index specification string |
||
439 | * @return array List of columns in the spec |
||
440 | */ |
||
441 | protected function explodeColumnString($spec) { |
||
450 | |||
451 | /** |
||
452 | * Builds a properly quoted column list from an array |
||
453 | * |
||
454 | * @param array $columns List of columns to implode |
||
455 | * @return string A properly quoted list of column names |
||
456 | */ |
||
457 | protected function implodeColumnList($columns) { |
||
461 | |||
462 | /** |
||
463 | * Given an index specification in the form of a string ensure that each |
||
464 | * column name is property quoted, stripping brackets and modifiers. |
||
465 | * This index may also be in the form of a "CREATE INDEX..." sql fragment |
||
466 | * |
||
467 | * @param string $spec The input specification or query. E.g. 'unique (Column1, Column2)' |
||
468 | * @return string The properly quoted column list. E.g. '"Column1", "Column2"' |
||
469 | */ |
||
470 | protected function quoteColumnSpecString($spec) { |
||
474 | |||
475 | /** |
||
476 | * Given an index spec determines the index type |
||
477 | * |
||
478 | * @param array|string $spec |
||
479 | * @return string |
||
480 | */ |
||
481 | protected function determineIndexType($spec) { |
||
491 | |||
492 | /** |
||
493 | * Converts an array or string index spec into a universally useful array |
||
494 | * |
||
495 | * @see convertIndexSpec() for approximate inverse |
||
496 | * @param string|array $spec |
||
497 | * @return array The resulting spec array with the required fields name, type, and value |
||
498 | */ |
||
499 | protected function parseIndexSpec($name, $spec) { |
||
523 | |||
524 | /** |
||
525 | * This takes the index spec which has been provided by a class (ie static $indexes = blah blah) |
||
526 | * and turns it into a proper string. |
||
527 | * Some indexes may be arrays, such as fulltext and unique indexes, and this allows database-specific |
||
528 | * arrays to be created. See {@link requireTable()} for details on the index format. |
||
529 | * |
||
530 | * @see http://dev.mysql.com/doc/refman/5.0/en/create-index.html |
||
531 | * @see parseIndexSpec() for approximate inverse |
||
532 | * |
||
533 | * @param string|array $indexSpec |
||
534 | */ |
||
535 | protected function convertIndexSpec($indexSpec) { |
||
542 | |||
543 | /** |
||
544 | * Returns true if the given table is exists in the current database |
||
545 | * |
||
546 | * @param string $table Name of table to check |
||
547 | * @return boolean Flag indicating existence of table |
||
548 | */ |
||
549 | abstract public function hasTable($tableName); |
||
550 | |||
551 | /** |
||
552 | * Return true if the table exists and already has a the field specified |
||
553 | * |
||
554 | * @param string $tableName - The table to check |
||
555 | * @param string $fieldName - The field to check |
||
556 | * @return bool - True if the table exists and the field exists on the table |
||
557 | */ |
||
558 | public function hasField($tableName, $fieldName) { |
||
563 | |||
564 | /** |
||
565 | * Generate the given field on the table, modifying whatever already exists as necessary. |
||
566 | * |
||
567 | * @param string $table The table name. |
||
568 | * @param string $field The field name. |
||
569 | * @param array|string $spec The field specification. If passed in array syntax, the specific database |
||
570 | * driver takes care of the ALTER TABLE syntax. If passed as a string, its assumed to |
||
571 | * be prepared as a direct SQL framgment ready for insertion into ALTER TABLE. In this case you'll |
||
572 | * need to take care of database abstraction in your DBField subclass. |
||
573 | */ |
||
574 | public function requireField($table, $field, $spec) { |
||
670 | |||
671 | /** |
||
672 | * If the given field exists, move it out of the way by renaming it to _obsolete_(fieldname). |
||
673 | * |
||
674 | * @param string $table |
||
675 | * @param string $fieldName |
||
676 | */ |
||
677 | public function dontRequireField($table, $fieldName) { |
||
693 | |||
694 | /** |
||
695 | * Show a message about database alteration |
||
696 | * |
||
697 | * @param string $message to display |
||
698 | * @param string $type one of [created|changed|repaired|obsolete|deleted|error] |
||
699 | */ |
||
700 | public function alterationMessage($message, $type = "") { |
||
754 | |||
755 | /** |
||
756 | * This returns the data type for the id column which is the primary key for each table |
||
757 | * |
||
758 | * @param boolean $asDbValue |
||
759 | * @param boolean $hasAutoIncPK |
||
760 | * @return string |
||
761 | */ |
||
762 | abstract public function IdColumn($asDbValue = false, $hasAutoIncPK = true); |
||
763 | |||
764 | /** |
||
765 | * Checks a table's integrity and repairs it if necessary. |
||
766 | * |
||
767 | * @param string $tableName The name of the table. |
||
768 | * @return boolean Return true if the table has integrity after the method is complete. |
||
769 | */ |
||
770 | abstract public function checkAndRepairTable($tableName); |
||
771 | |||
772 | /** |
||
773 | * Returns the values of the given enum field |
||
774 | * |
||
775 | * @param string $tableName Name of table to check |
||
776 | * @param string $fieldName name of enum field to check |
||
777 | * @return array List of enum values |
||
778 | */ |
||
779 | abstract public function enumValuesForField($tableName, $fieldName); |
||
780 | |||
781 | |||
782 | /* |
||
783 | * This is a lookup table for data types. |
||
784 | * For instance, Postgres uses 'INT', while MySQL uses 'UNSIGNED' |
||
785 | * So this is a DB-specific list of equivilents. |
||
786 | * |
||
787 | * @param string $type |
||
788 | * @return string |
||
789 | */ |
||
790 | abstract public function dbDataType($type); |
||
791 | |||
792 | /** |
||
793 | * Retrieves the list of all databases the user has access to |
||
794 | * |
||
795 | * @return array List of database names |
||
796 | */ |
||
797 | abstract public function databaseList(); |
||
798 | |||
799 | /** |
||
800 | * Determine if the database with the specified name exists |
||
801 | * |
||
802 | * @param string $name Name of the database to check for |
||
803 | * @return boolean Flag indicating whether this database exists |
||
804 | */ |
||
805 | abstract public function databaseExists($name); |
||
806 | |||
807 | /** |
||
808 | * Create a database with the specified name |
||
809 | * |
||
810 | * @param string $name Name of the database to create |
||
811 | * @return boolean True if successful |
||
812 | */ |
||
813 | abstract public function createDatabase($name); |
||
814 | |||
815 | /** |
||
816 | * Drops a database with the specified name |
||
817 | * |
||
818 | * @param string $name Name of the database to drop |
||
819 | */ |
||
820 | abstract public function dropDatabase($name); |
||
821 | |||
822 | /** |
||
823 | * Alter an index on a table. |
||
824 | * |
||
825 | * @param string $tableName The name of the table. |
||
826 | * @param string $indexName The name of the index. |
||
827 | * @param string $indexSpec The specification of the index, see {@link SS_Database::requireIndex()} |
||
828 | * for more details. |
||
829 | * @todo Find out where this is called from - Is it even used? Aren't indexes always dropped and re-added? |
||
830 | */ |
||
831 | abstract public function alterIndex($tableName, $indexName, $indexSpec); |
||
832 | |||
833 | /** |
||
834 | * Determines the key that should be used to identify this index |
||
835 | * when retrieved from DBSchemaManager->indexList. |
||
836 | * In some connectors this is the database-visible name, in others the |
||
837 | * usercode-visible name. |
||
838 | * |
||
839 | * @param string $table |
||
840 | * @param string $index |
||
841 | * @param array $spec |
||
842 | * @return string Key for this index |
||
843 | */ |
||
844 | abstract protected function indexKey($table, $index, $spec); |
||
845 | |||
846 | /** |
||
847 | * Return the list of indexes in a table. |
||
848 | * |
||
849 | * @param string $table The table name. |
||
850 | * @return array[array] List of current indexes in the table, each in standard |
||
851 | * array form. The key for this array should be predictable using the indexKey |
||
852 | * method |
||
853 | */ |
||
854 | abstract public function indexList($table); |
||
855 | |||
856 | /** |
||
857 | * Returns a list of all tables in the database. |
||
858 | * Keys are table names in lower case, values are table names in case that |
||
859 | * database expects. |
||
860 | * |
||
861 | * @return array |
||
862 | */ |
||
863 | abstract public function tableList(); |
||
864 | |||
865 | /** |
||
866 | * Create a new table. |
||
867 | * |
||
868 | * @param string $table The name of the table |
||
869 | * @param array $fields A map of field names to field types |
||
870 | * @param array $indexes A map of indexes |
||
871 | * @param array $options An map of additional options. The available keys are as follows: |
||
872 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL. |
||
873 | * - 'temporary' - If true, then a temporary table will be created |
||
874 | * @param $advancedOptions Advanced creation options |
||
875 | * @return string The table name generated. This may be different from the table name, for example with temporary |
||
876 | * tables. |
||
877 | */ |
||
878 | abstract public function createTable($table, $fields = null, $indexes = null, $options = null, |
||
880 | |||
881 | /** |
||
882 | * Alter a table's schema. |
||
883 | * |
||
884 | * @param string $table The name of the table to alter |
||
885 | * @param array $newFields New fields, a map of field name => field schema |
||
886 | * @param array $newIndexes New indexes, a map of index name => index type |
||
887 | * @param array $alteredFields Updated fields, a map of field name => field schema |
||
888 | * @param array $alteredIndexes Updated indexes, a map of index name => index type |
||
889 | * @param array $alteredOptions |
||
890 | * @param array $advancedOptions |
||
891 | */ |
||
892 | abstract public function alterTable($table, $newFields = null, $newIndexes = null, $alteredFields = null, |
||
894 | |||
895 | /** |
||
896 | * Rename a table. |
||
897 | * |
||
898 | * @param string $oldTableName The old table name. |
||
899 | * @param string $newTableName The new table name. |
||
900 | */ |
||
901 | abstract public function renameTable($oldTableName, $newTableName); |
||
902 | |||
903 | /** |
||
904 | * Create a new field on a table. |
||
905 | * |
||
906 | * @param string $table Name of the table. |
||
907 | * @param string $field Name of the field to add. |
||
908 | * @param string $spec The field specification, eg 'INTEGER NOT NULL' |
||
909 | */ |
||
910 | abstract public function createField($table, $field, $spec); |
||
911 | |||
912 | /** |
||
913 | * Change the database column name of the given field. |
||
914 | * |
||
915 | * @param string $tableName The name of the tbale the field is in. |
||
916 | * @param string $oldName The name of the field to change. |
||
917 | * @param string $newName The new name of the field |
||
918 | */ |
||
919 | abstract public function renameField($tableName, $oldName, $newName); |
||
920 | |||
921 | /** |
||
922 | * Get a list of all the fields for the given table. |
||
923 | * Returns a map of field name => field spec. |
||
924 | * |
||
925 | * @param string $table The table name. |
||
926 | * @return array |
||
927 | */ |
||
928 | abstract public function fieldList($table); |
||
929 | |||
930 | /** |
||
931 | * |
||
932 | * This allows the cached values for a table's field list to be erased. |
||
933 | * If $tablename is empty, then the whole cache is erased. |
||
934 | * |
||
935 | * @param string $tableName |
||
936 | * |
||
937 | * @return boolean |
||
938 | */ |
||
939 | public function clearCachedFieldlist($tableName = false) { |
||
942 | |||
943 | } |
||
944 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.