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) { |
||
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) { |
||
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) { |
||
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, |
||
361 | |||
362 | /** |
||
363 | * If the given table exists, move it out of the way by renaming it to _obsolete_(tablename). |
||
364 | * @param string $table The table name. |
||
365 | */ |
||
366 | public function dontRequireTable($table) { |
||
378 | |||
379 | /** |
||
380 | * Generate the given index in the database, modifying whatever already exists as necessary. |
||
381 | * |
||
382 | * The keys of the array are the names of the index. |
||
383 | * The values of the array can be one of: |
||
384 | * - true: Create a single column index on the field named the same as the index. |
||
385 | * - array('type' => 'index|unique|fulltext', 'value' => 'FieldA, FieldB'): This gives you full |
||
386 | * control over the index. |
||
387 | * |
||
388 | * @param string $table The table name. |
||
389 | * @param string $index The index name. |
||
390 | * @param string|array|boolean $spec The specification of the index in any |
||
391 | * loose format. See requireTable() for more information. |
||
392 | */ |
||
393 | public function requireIndex($table, $index, $spec) { |
||
427 | |||
428 | /** |
||
429 | * Splits a spec string safely, considering quoted columns, whitespace, |
||
430 | * and cleaning brackets |
||
431 | * |
||
432 | * @param string $spec The input index specification string |
||
433 | * @return array List of columns in the spec |
||
434 | */ |
||
435 | protected function explodeColumnString($spec) { |
||
444 | |||
445 | /** |
||
446 | * Builds a properly quoted column list from an array |
||
447 | * |
||
448 | * @param array $columns List of columns to implode |
||
449 | * @return string A properly quoted list of column names |
||
450 | */ |
||
451 | protected function implodeColumnList($columns) { |
||
455 | |||
456 | /** |
||
457 | * Given an index specification in the form of a string ensure that each |
||
458 | * column name is property quoted, stripping brackets and modifiers. |
||
459 | * This index may also be in the form of a "CREATE INDEX..." sql fragment |
||
460 | * |
||
461 | * @param string $spec The input specification or query. E.g. 'unique (Column1, Column2)' |
||
462 | * @return string The properly quoted column list. E.g. '"Column1", "Column2"' |
||
463 | */ |
||
464 | protected function quoteColumnSpecString($spec) { |
||
468 | |||
469 | /** |
||
470 | * Given an index spec determines the index type |
||
471 | * |
||
472 | * @param array|string $spec |
||
473 | * @return string |
||
474 | */ |
||
475 | protected function determineIndexType($spec) { |
||
485 | |||
486 | /** |
||
487 | * Converts an array or string index spec into a universally useful array |
||
488 | * |
||
489 | * @see convertIndexSpec() for approximate inverse |
||
490 | * @param string|array $spec |
||
491 | * @return array The resulting spec array with the required fields name, type, and value |
||
492 | */ |
||
493 | protected function parseIndexSpec($name, $spec) { |
||
517 | |||
518 | /** |
||
519 | * This takes the index spec which has been provided by a class (ie static $indexes = blah blah) |
||
520 | * and turns it into a proper string. |
||
521 | * Some indexes may be arrays, such as fulltext and unique indexes, and this allows database-specific |
||
522 | * arrays to be created. See {@link requireTable()} for details on the index format. |
||
523 | * |
||
524 | * @see http://dev.mysql.com/doc/refman/5.0/en/create-index.html |
||
525 | * @see parseIndexSpec() for approximate inverse |
||
526 | * |
||
527 | * @param string|array $indexSpec |
||
528 | */ |
||
529 | protected function convertIndexSpec($indexSpec) { |
||
536 | |||
537 | /** |
||
538 | * Returns true if the given table is exists in the current database |
||
539 | * |
||
540 | * @param string $table Name of table to check |
||
541 | * @return boolean Flag indicating existence of table |
||
542 | */ |
||
543 | abstract public function hasTable($tableName); |
||
544 | |||
545 | /** |
||
546 | * Return true if the table exists and already has a the field specified |
||
547 | * |
||
548 | * @param string $tableName - The table to check |
||
549 | * @param string $fieldName - The field to check |
||
550 | * @return bool - True if the table exists and the field exists on the table |
||
551 | */ |
||
552 | public function hasField($tableName, $fieldName) { |
||
557 | |||
558 | /** |
||
559 | * Generate the given field on the table, modifying whatever already exists as necessary. |
||
560 | * |
||
561 | * @param string $table The table name. |
||
562 | * @param string $field The field name. |
||
563 | * @param array|string $spec The field specification. If passed in array syntax, the specific database |
||
564 | * driver takes care of the ALTER TABLE syntax. If passed as a string, its assumed to |
||
565 | * be prepared as a direct SQL framgment ready for insertion into ALTER TABLE. In this case you'll |
||
566 | * need to take care of database abstraction in your DBField subclass. |
||
567 | */ |
||
568 | public function requireField($table, $field, $spec) { |
||
666 | |||
667 | /** |
||
668 | * If the given field exists, move it out of the way by renaming it to _obsolete_(fieldname). |
||
669 | * |
||
670 | * @param string $table |
||
671 | * @param string $fieldName |
||
672 | */ |
||
673 | public function dontRequireField($table, $fieldName) { |
||
689 | |||
690 | /** |
||
691 | * Show a message about database alteration |
||
692 | * |
||
693 | * @param string $message to display |
||
694 | * @param string $type one of [created|changed|repaired|obsolete|deleted|error] |
||
695 | */ |
||
696 | public function alterationMessage($message, $type = "") { |
||
750 | |||
751 | /** |
||
752 | * This returns the data type for the id column which is the primary key for each table |
||
753 | * |
||
754 | * @param boolean $asDbValue |
||
755 | * @param boolean $hasAutoIncPK |
||
756 | * @return string |
||
757 | */ |
||
758 | abstract public function IdColumn($asDbValue = false, $hasAutoIncPK = true); |
||
759 | |||
760 | /** |
||
761 | * Checks a table's integrity and repairs it if necessary. |
||
762 | * |
||
763 | * @param string $tableName The name of the table. |
||
764 | * @return boolean Return true if the table has integrity after the method is complete. |
||
765 | */ |
||
766 | abstract public function checkAndRepairTable($tableName); |
||
767 | |||
768 | /** |
||
769 | * Returns the values of the given enum field |
||
770 | * |
||
771 | * @param string $tableName Name of table to check |
||
772 | * @param string $fieldName name of enum field to check |
||
773 | * @return array List of enum values |
||
774 | */ |
||
775 | abstract public function enumValuesForField($tableName, $fieldName); |
||
776 | |||
777 | |||
778 | /* |
||
779 | * This is a lookup table for data types. |
||
780 | * For instance, Postgres uses 'INT', while MySQL uses 'UNSIGNED' |
||
781 | * So this is a DB-specific list of equivilents. |
||
782 | * |
||
783 | * @param string $type |
||
784 | * @return string |
||
785 | */ |
||
786 | abstract public function dbDataType($type); |
||
787 | |||
788 | /** |
||
789 | * Retrieves the list of all databases the user has access to |
||
790 | * |
||
791 | * @return array List of database names |
||
792 | */ |
||
793 | abstract public function databaseList(); |
||
794 | |||
795 | /** |
||
796 | * Determine if the database with the specified name exists |
||
797 | * |
||
798 | * @param string $name Name of the database to check for |
||
799 | * @return boolean Flag indicating whether this database exists |
||
800 | */ |
||
801 | abstract public function databaseExists($name); |
||
802 | |||
803 | /** |
||
804 | * Create a database with the specified name |
||
805 | * |
||
806 | * @param string $name Name of the database to create |
||
807 | * @return boolean True if successful |
||
808 | */ |
||
809 | abstract public function createDatabase($name); |
||
810 | |||
811 | /** |
||
812 | * Drops a database with the specified name |
||
813 | * |
||
814 | * @param string $name Name of the database to drop |
||
815 | */ |
||
816 | abstract public function dropDatabase($name); |
||
817 | |||
818 | /** |
||
819 | * Alter an index on a table. |
||
820 | * |
||
821 | * @param string $tableName The name of the table. |
||
822 | * @param string $indexName The name of the index. |
||
823 | * @param string $indexSpec The specification of the index, see {@link SS_Database::requireIndex()} |
||
824 | * for more details. |
||
825 | * @todo Find out where this is called from - Is it even used? Aren't indexes always dropped and re-added? |
||
826 | */ |
||
827 | abstract public function alterIndex($tableName, $indexName, $indexSpec); |
||
828 | |||
829 | /** |
||
830 | * Determines the key that should be used to identify this index |
||
831 | * when retrieved from DBSchemaManager->indexList. |
||
832 | * In some connectors this is the database-visible name, in others the |
||
833 | * usercode-visible name. |
||
834 | * |
||
835 | * @param string $table |
||
836 | * @param string $index |
||
837 | * @param array $spec |
||
838 | * @return string Key for this index |
||
839 | */ |
||
840 | abstract protected function indexKey($table, $index, $spec); |
||
841 | |||
842 | /** |
||
843 | * Return the list of indexes in a table. |
||
844 | * |
||
845 | * @param string $table The table name. |
||
846 | * @return array[array] List of current indexes in the table, each in standard |
||
847 | * array form. The key for this array should be predictable using the indexKey |
||
848 | * method |
||
849 | */ |
||
850 | abstract public function indexList($table); |
||
851 | |||
852 | /** |
||
853 | * Returns a list of all tables in the database. |
||
854 | * Keys are table names in lower case, values are table names in case that |
||
855 | * database expects. |
||
856 | * |
||
857 | * @return array |
||
858 | */ |
||
859 | abstract public function tableList(); |
||
860 | |||
861 | /** |
||
862 | * Create a new table. |
||
863 | * |
||
864 | * @param string $table The name of the table |
||
865 | * @param array $fields A map of field names to field types |
||
866 | * @param array $indexes A map of indexes |
||
867 | * @param array $options An map of additional options. The available keys are as follows: |
||
868 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL. |
||
869 | * - 'temporary' - If true, then a temporary table will be created |
||
870 | * @param $advancedOptions Advanced creation options |
||
871 | * @return string The table name generated. This may be different from the table name, for example with temporary |
||
872 | * tables. |
||
873 | */ |
||
874 | abstract public function createTable($table, $fields = null, $indexes = null, $options = null, |
||
876 | |||
877 | /** |
||
878 | * Alter a table's schema. |
||
879 | * |
||
880 | * @param string $table The name of the table to alter |
||
881 | * @param array $newFields New fields, a map of field name => field schema |
||
882 | * @param array $newIndexes New indexes, a map of index name => index type |
||
883 | * @param array $alteredFields Updated fields, a map of field name => field schema |
||
884 | * @param array $alteredIndexes Updated indexes, a map of index name => index type |
||
885 | * @param array $alteredOptions |
||
886 | * @param array $advancedOptions |
||
887 | */ |
||
888 | abstract public function alterTable($table, $newFields = null, $newIndexes = null, $alteredFields = null, |
||
890 | |||
891 | /** |
||
892 | * Rename a table. |
||
893 | * |
||
894 | * @param string $oldTableName The old table name. |
||
895 | * @param string $newTableName The new table name. |
||
896 | */ |
||
897 | abstract public function renameTable($oldTableName, $newTableName); |
||
898 | |||
899 | /** |
||
900 | * Create a new field on a table. |
||
901 | * |
||
902 | * @param string $table Name of the table. |
||
903 | * @param string $field Name of the field to add. |
||
904 | * @param string $spec The field specification, eg 'INTEGER NOT NULL' |
||
905 | */ |
||
906 | abstract public function createField($table, $field, $spec); |
||
907 | |||
908 | /** |
||
909 | * Change the database column name of the given field. |
||
910 | * |
||
911 | * @param string $tableName The name of the tbale the field is in. |
||
912 | * @param string $oldName The name of the field to change. |
||
913 | * @param string $newName The new name of the field |
||
914 | */ |
||
915 | abstract public function renameField($tableName, $oldName, $newName); |
||
916 | |||
917 | /** |
||
918 | * Get a list of all the fields for the given table. |
||
919 | * Returns a map of field name => field spec. |
||
920 | * |
||
921 | * @param string $table The table name. |
||
922 | * @return array |
||
923 | */ |
||
924 | abstract public function fieldList($table); |
||
925 | |||
926 | /** |
||
927 | * |
||
928 | * This allows the cached values for a table's field list to be erased. |
||
929 | * If $tablename is empty, then the whole cache is erased. |
||
930 | * |
||
931 | * @param string $tableName |
||
932 | * |
||
933 | * @return boolean |
||
934 | */ |
||
935 | public function clearCachedFieldlist($tableName = false) { |
||
938 | |||
939 | } |
||
940 |
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.