Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MSSQLSchemaManager 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 MSSQLSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class MSSQLSchemaManager extends DBSchemaManager |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * Stores per-request cached constraint checks that come from the database. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected static $cached_checks = array(); |
||
24 | |||
25 | /** |
||
26 | * Builds the internal MS SQL Server index name given the silverstripe table and index name |
||
27 | * |
||
28 | * @param string $tableName |
||
29 | * @param string $indexName |
||
30 | * @param string $prefix The optional prefix for the index. Defaults to "ix" for indexes. |
||
31 | * @return string The name of the index |
||
32 | */ |
||
33 | public function buildMSSQLIndexName($tableName, $indexName, $prefix = 'ix') |
||
42 | |||
43 | |||
44 | /** |
||
45 | * This will set up the full text search capabilities. |
||
46 | * |
||
47 | * @param string $name Name of full text catalog to use |
||
48 | */ |
||
49 | public function createFullTextCatalog($name = 'ftCatalog') |
||
56 | |||
57 | /** |
||
58 | * Check that a fulltext catalog has been created yet. |
||
59 | * |
||
60 | * @param string $name Name of full text catalog to use |
||
61 | * @return boolean |
||
62 | */ |
||
63 | public function fullTextCatalogExists($name = 'ftCatalog') |
||
70 | |||
71 | /** |
||
72 | * Sleep until the catalog has been fully rebuilt. This is a busy wait designed for situations |
||
73 | * when you need to be sure the index is up to date - for example in unit tests. |
||
74 | * |
||
75 | * TODO: move this to Database class? Can we assume this will be useful for all databases? |
||
76 | * Also see the wrapper functions "waitUntilIndexingFinished" in SearchFormTest and TranslatableSearchFormTest |
||
77 | * |
||
78 | * @param int $maxWaitingTime Time in seconds to wait for the database. |
||
79 | */ |
||
80 | public function waitUntilIndexingFinished($maxWaitingTime = 15) |
||
100 | |||
101 | /** |
||
102 | * Check if a fulltext index exists on a particular table name. |
||
103 | * |
||
104 | * @param string $tableName |
||
105 | * @return boolean TRUE index exists | FALSE index does not exist | NULL no support |
||
106 | */ |
||
107 | public function fulltextIndexExists($tableName) |
||
121 | |||
122 | /** |
||
123 | * MSSQL stores the primary key column with an internal identifier, |
||
124 | * so a lookup needs to be done to determine it. |
||
125 | * |
||
126 | * @param string $tableName Name of table with primary key column "ID" |
||
127 | * @return string Internal identifier for primary key |
||
128 | */ |
||
129 | public function getPrimaryKey($tableName) |
||
142 | |||
143 | /** |
||
144 | * Gets the identity column of a table |
||
145 | * |
||
146 | * @param string $tableName |
||
147 | * @return string|null |
||
148 | */ |
||
149 | public function getIdentityColumn($tableName) |
||
163 | |||
164 | public function createDatabase($name) |
||
168 | |||
169 | public function dropDatabase($name) |
||
173 | |||
174 | public function databaseExists($name) |
||
184 | |||
185 | public function databaseList() |
||
189 | |||
190 | /** |
||
191 | * Create a new table. |
||
192 | * @param string $tableName The name of the table |
||
193 | * @param array $fields A map of field names to field types |
||
194 | * @param array $indexes A map of indexes |
||
195 | * @param array $options An map of additional options. The available keys are as follows: |
||
196 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL. |
||
197 | * - 'temporary' - If true, then a temporary table will be created |
||
198 | * @param array $advancedOptions |
||
199 | * @return string The table name generated. This may be different from the table name, for example with temporary tables. |
||
200 | */ |
||
201 | public function createTable($tableName, $fields = null, $indexes = null, $options = null, $advancedOptions = null) |
||
235 | |||
236 | /** |
||
237 | * Alter a table's schema. |
||
238 | * @param string $tableName The name of the table to alter |
||
239 | * @param array $newFields New fields, a map of field name => field schema |
||
240 | * @param array $newIndexes New indexes, a map of index name => index type |
||
241 | * @param array $alteredFields Updated fields, a map of field name => field schema |
||
242 | * @param array $alteredIndexes Updated indexes, a map of index name => index type |
||
243 | * @param array $alteredOptions |
||
244 | * @param array $advancedOptions |
||
245 | */ |
||
246 | public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null, $alteredOptions=null, $advancedOptions=null) |
||
285 | |||
286 | /** |
||
287 | * Given the table and column name, retrieve the constraint name for that column |
||
288 | * in the table. |
||
289 | * |
||
290 | * @param string $tableName Table name column resides in |
||
291 | * @param string $columnName Column name the constraint is for |
||
292 | * @return string|null |
||
293 | */ |
||
294 | public function getConstraintName($tableName, $columnName) |
||
303 | |||
304 | /** |
||
305 | * Given a table and column name, return a check constraint clause for that column in |
||
306 | * the table. |
||
307 | * |
||
308 | * This is an expensive query, so it is cached per-request and stored by table. The initial |
||
309 | * call for a table that has not been cached will query all columns and store that |
||
310 | * so subsequent calls are fast. |
||
311 | * |
||
312 | * @param string $tableName Table name column resides in |
||
313 | * @param string $columnName Column name the constraint is for |
||
314 | * @return string The check string |
||
315 | */ |
||
316 | public function getConstraintCheckClause($tableName, $columnName) |
||
342 | |||
343 | /** |
||
344 | * Return the name of the default constraint applied to $tableName.$colName. |
||
345 | * Will return null if no such constraint exists |
||
346 | * |
||
347 | * @param string $tableName Name of the table |
||
348 | * @param string $colName Name of the column |
||
349 | * @return string|null |
||
350 | */ |
||
351 | protected function defaultConstraintName($tableName, $colName) |
||
364 | |||
365 | /** |
||
366 | * Get enum values from a constraint check clause. |
||
367 | * |
||
368 | * @param string $clause Check clause to parse values from |
||
369 | * @return array Enum values |
||
370 | */ |
||
371 | protected function enumValuesFromCheckClause($clause) |
||
383 | |||
384 | /* |
||
385 | * Creates an ALTER expression for a column in MS SQL |
||
386 | * |
||
387 | * @param string $tableName Name of the table to be altered |
||
388 | * @param string $colName Name of the column to be altered |
||
389 | * @param string $colSpec String which contains conditions for a column |
||
390 | * @return string |
||
391 | */ |
||
392 | protected function alterTableAlterColumn($tableName, $colName, $colSpec) |
||
447 | |||
448 | public function renameTable($oldTableName, $newTableName) |
||
452 | |||
453 | /** |
||
454 | * Checks a table's integrity and repairs it if necessary. |
||
455 | * NOTE: MSSQL does not appear to support any vacuum or optimise commands |
||
456 | * |
||
457 | * @var string $tableName The name of the table. |
||
458 | * @return boolean Return true if the table has integrity after the method is complete. |
||
459 | */ |
||
460 | public function checkAndRepairTable($tableName) |
||
464 | |||
465 | public function createField($tableName, $fieldName, $fieldSpec) |
||
469 | |||
470 | /** |
||
471 | * Change the database type of the given field. |
||
472 | * @param string $tableName The name of the tbale the field is in. |
||
473 | * @param string $fieldName The name of the field to change. |
||
474 | * @param string $fieldSpec The new field specification |
||
475 | */ |
||
476 | public function alterField($tableName, $fieldName, $fieldSpec) |
||
480 | |||
481 | public function renameField($tableName, $oldName, $newName) |
||
485 | |||
486 | public function fieldList($table) |
||
585 | |||
586 | /** |
||
587 | * Create an index on a table. |
||
588 | * @param string $tableName The name of the table. |
||
589 | * @param string $indexName The name of the index. |
||
590 | * @param string $indexSpec The specification of the index, see SS_Database::requireIndex() for more details. |
||
591 | */ |
||
592 | public function createIndex($tableName, $indexName, $indexSpec) |
||
596 | |||
597 | /** |
||
598 | * Return SQL for dropping and recreating an index |
||
599 | * |
||
600 | * @param string $tableName Name of table to create this index against |
||
601 | * @param string $indexName Name of this index |
||
602 | * @param array|string $indexSpec Index specification, either as a raw string |
||
603 | * or parsed array form |
||
604 | * @return string The SQL required to generate this index |
||
605 | */ |
||
606 | protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec) |
||
635 | |||
636 | public function alterIndex($tableName, $indexName, $indexSpec) |
||
640 | |||
641 | /** |
||
642 | * Return the list of indexes in a table. |
||
643 | * @param string $table The table name. |
||
644 | * @return array |
||
645 | */ |
||
646 | public function indexList($table) |
||
695 | |||
696 | /** |
||
697 | * For a given table name, get all the internal index names, |
||
698 | * except for those that are primary keys and fulltext indexes. |
||
699 | * |
||
700 | * @return array |
||
701 | */ |
||
702 | public function indexNames($tableName) |
||
711 | |||
712 | public function tableList() |
||
720 | |||
721 | /** |
||
722 | * Return a boolean type-formatted string |
||
723 | * We use 'bit' so that we can do numeric-based comparisons |
||
724 | * |
||
725 | * @param array $values Contains a tokenised list of info about this data type |
||
726 | * @return string |
||
727 | */ |
||
728 | public function boolean($values) |
||
733 | |||
734 | /** |
||
735 | * Return a date type-formatted string. |
||
736 | * |
||
737 | * @param array $values Contains a tokenised list of info about this data type |
||
738 | * @return string |
||
739 | */ |
||
740 | public function date($values) |
||
744 | |||
745 | /** |
||
746 | * Return a decimal type-formatted string |
||
747 | * |
||
748 | * @param array $values Contains a tokenised list of info about this data type |
||
749 | * @return string |
||
750 | */ |
||
751 | public function decimal($values) |
||
767 | |||
768 | /** |
||
769 | * Return a enum type-formatted string |
||
770 | * |
||
771 | * @param array $values Contains a tokenised list of info about this data type |
||
772 | * @return string |
||
773 | */ |
||
774 | public function enum($values) |
||
786 | |||
787 | /** |
||
788 | * @todo Make this work like {@link MySQLDatabase::set()} |
||
789 | */ |
||
790 | public function set($values) |
||
794 | |||
795 | /** |
||
796 | * Return a float type-formatted string. |
||
797 | * |
||
798 | * @param array $values Contains a tokenised list of info about this data type |
||
799 | * @return string |
||
800 | */ |
||
801 | public function float($values) |
||
805 | |||
806 | /** |
||
807 | * Return a int type-formatted string |
||
808 | * |
||
809 | * @param array $values Contains a tokenised list of info about this data type |
||
810 | * @return string |
||
811 | */ |
||
812 | public function int($values) |
||
816 | |||
817 | /** |
||
818 | * Return a bigint type-formatted string |
||
819 | * |
||
820 | * @param array $values Contains a tokenised list of info about this data type |
||
821 | * @return string |
||
822 | */ |
||
823 | public function bigint($values) |
||
827 | |||
828 | /** |
||
829 | * Return a datetime type-formatted string |
||
830 | * For MS SQL, we simply return the word 'timestamp', no other parameters are necessary |
||
831 | * |
||
832 | * @param array $values Contains a tokenised list of info about this data type |
||
833 | * @return string |
||
834 | */ |
||
835 | public function datetime($values) |
||
839 | |||
840 | /** |
||
841 | * Return a text type-formatted string |
||
842 | * |
||
843 | * @param array $values Contains a tokenised list of info about this data type |
||
844 | * @return string |
||
845 | */ |
||
846 | public function text($values) |
||
852 | |||
853 | /** |
||
854 | * Return a time type-formatted string. |
||
855 | * |
||
856 | * @param array $values Contains a tokenised list of info about this data type |
||
857 | * @return string |
||
858 | */ |
||
859 | public function time($values) |
||
863 | |||
864 | /** |
||
865 | * Return a varchar type-formatted string |
||
866 | * |
||
867 | * @param array $values Contains a tokenised list of info about this data type |
||
868 | * @return string |
||
869 | */ |
||
870 | public function varchar($values) |
||
876 | |||
877 | /** |
||
878 | * Return a 4 digit numeric type. |
||
879 | * |
||
880 | * @param array $values |
||
881 | * @return string |
||
882 | */ |
||
883 | public function year($values) |
||
887 | |||
888 | /** |
||
889 | * This returns the column which is the primary key for each table |
||
890 | * |
||
891 | * @param bool $asDbValue |
||
892 | * @param bool $hasAutoIncPK |
||
893 | * @return string |
||
894 | */ |
||
895 | public function IdColumn($asDbValue = false, $hasAutoIncPK = true) |
||
905 | |||
906 | public function hasTable($tableName) |
||
913 | |||
914 | /** |
||
915 | * Returns the values of the given enum field |
||
916 | * NOTE: Experimental; introduced for db-abstraction and may changed before 2.4 is released. |
||
917 | * |
||
918 | * @param string $tableName |
||
919 | * @param string $fieldName |
||
920 | * @return array |
||
921 | */ |
||
922 | public function enumValuesForField($tableName, $fieldName) |
||
934 | |||
935 | /** |
||
936 | * This is a lookup table for data types. |
||
937 | * |
||
938 | * For instance, MSSQL uses 'BIGINT', while MySQL uses 'UNSIGNED' |
||
939 | * and PostgreSQL uses 'INT'. |
||
940 | * |
||
941 | * @param string $type |
||
942 | * @return string |
||
943 | */ |
||
944 | public function dbDataType($type) |
||
955 | |||
956 | protected function indexKey($table, $index, $spec) |
||
960 | } |
||
961 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: