Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Schema extends \yii\db\Schema |
||
20 | { |
||
21 | use ViewFinderTrait; |
||
22 | |||
23 | /** |
||
24 | * @var string the default schema used for the current session. |
||
25 | */ |
||
26 | public $defaultSchema = 'dbo'; |
||
27 | /** |
||
28 | * @var array mapping from physical column types (keys) to abstract column types (values) |
||
29 | */ |
||
30 | public $typeMap = [ |
||
31 | // exact numbers |
||
32 | 'bigint' => self::TYPE_BIGINT, |
||
33 | 'numeric' => self::TYPE_DECIMAL, |
||
34 | 'bit' => self::TYPE_SMALLINT, |
||
35 | 'smallint' => self::TYPE_SMALLINT, |
||
36 | 'decimal' => self::TYPE_DECIMAL, |
||
37 | 'smallmoney' => self::TYPE_MONEY, |
||
38 | 'int' => self::TYPE_INTEGER, |
||
39 | 'tinyint' => self::TYPE_SMALLINT, |
||
40 | 'money' => self::TYPE_MONEY, |
||
41 | // approximate numbers |
||
42 | 'float' => self::TYPE_FLOAT, |
||
43 | 'double' => self::TYPE_DOUBLE, |
||
44 | 'real' => self::TYPE_FLOAT, |
||
45 | // date and time |
||
46 | 'date' => self::TYPE_DATE, |
||
47 | 'datetimeoffset' => self::TYPE_DATETIME, |
||
48 | 'datetime2' => self::TYPE_DATETIME, |
||
49 | 'smalldatetime' => self::TYPE_DATETIME, |
||
50 | 'datetime' => self::TYPE_DATETIME, |
||
51 | 'time' => self::TYPE_TIME, |
||
52 | // character strings |
||
53 | 'char' => self::TYPE_CHAR, |
||
54 | 'varchar' => self::TYPE_STRING, |
||
55 | 'text' => self::TYPE_TEXT, |
||
56 | // unicode character strings |
||
57 | 'nchar' => self::TYPE_CHAR, |
||
58 | 'nvarchar' => self::TYPE_STRING, |
||
59 | 'ntext' => self::TYPE_TEXT, |
||
60 | // binary strings |
||
61 | 'binary' => self::TYPE_BINARY, |
||
62 | 'varbinary' => self::TYPE_BINARY, |
||
63 | 'image' => self::TYPE_BINARY, |
||
64 | // other data types |
||
65 | // 'cursor' type cannot be used with tables |
||
66 | 'timestamp' => self::TYPE_TIMESTAMP, |
||
67 | 'hierarchyid' => self::TYPE_STRING, |
||
68 | 'uniqueidentifier' => self::TYPE_STRING, |
||
69 | 'sql_variant' => self::TYPE_STRING, |
||
70 | 'xml' => self::TYPE_STRING, |
||
71 | 'table' => self::TYPE_STRING, |
||
72 | ]; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function createSavepoint($name) |
||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | public function releaseSavepoint($name) |
||
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | public function rollBackSavepoint($name) |
||
98 | |||
99 | /** |
||
100 | * Quotes a table name for use in a query. |
||
101 | * A simple table name has no schema prefix. |
||
102 | * @param string $name table name. |
||
103 | * @return string the properly quoted table name. |
||
104 | */ |
||
105 | public function quoteSimpleTableName($name) |
||
109 | |||
110 | /** |
||
111 | * Quotes a column name for use in a query. |
||
112 | * A simple column name has no prefix. |
||
113 | * @param string $name column name. |
||
114 | * @return string the properly quoted column name. |
||
115 | */ |
||
116 | public function quoteSimpleColumnName($name) |
||
120 | |||
121 | /** |
||
122 | * Creates a query builder for the MSSQL database. |
||
123 | * @return QueryBuilder query builder interface. |
||
124 | */ |
||
125 | public function createQueryBuilder() |
||
129 | |||
130 | /** |
||
131 | * Loads the metadata for the specified table. |
||
132 | * @param string $name table name |
||
133 | * @return TableSchema|null driver dependent table metadata. Null if the table does not exist. |
||
134 | */ |
||
135 | public function loadTableSchema($name) |
||
148 | |||
149 | /** |
||
150 | * Resolves the table name and schema name (if any). |
||
151 | * @param TableSchema $table the table metadata object |
||
152 | * @param string $name the table name |
||
153 | */ |
||
154 | protected function resolveTableNames($table, $name) |
||
181 | |||
182 | /** |
||
183 | * Loads the column information into a [[ColumnSchema]] object. |
||
184 | * @param array $info column information |
||
185 | * @return ColumnSchema the column schema object |
||
186 | */ |
||
187 | protected function loadColumnSchema($info) |
||
235 | |||
236 | /** |
||
237 | * Collects the metadata of table columns. |
||
238 | * @param TableSchema $table the table metadata |
||
239 | * @return bool whether the table exists in the database |
||
240 | */ |
||
241 | protected function findColumns($table) |
||
293 | |||
294 | /** |
||
295 | * Collects the constraint details for the given table and constraint type. |
||
296 | * @param TableSchema $table |
||
297 | * @param string $type either PRIMARY KEY or UNIQUE |
||
298 | * @return array each entry contains index_name and field_name |
||
299 | * @since 2.0.4 |
||
300 | */ |
||
301 | protected function findTableConstraints($table, $type) |
||
335 | |||
336 | /** |
||
337 | * Collects the primary key column details for the given table. |
||
338 | * @param TableSchema $table the table metadata |
||
339 | */ |
||
340 | protected function findPrimaryKeys($table) |
||
348 | |||
349 | /** |
||
350 | * Collects the foreign key column details for the given table. |
||
351 | * @param TableSchema $table the table metadata |
||
352 | */ |
||
353 | protected function findForeignKeys($table) |
||
395 | |||
396 | /** |
||
397 | * Returns all table names in the database. |
||
398 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
399 | * @return array all table names in the database. The names have NO schema name prefix. |
||
400 | */ |
||
401 | protected function findTableNames($schema = '') |
||
416 | |||
417 | /** |
||
418 | * @inheritdoc |
||
419 | */ |
||
420 | protected function findViewNames($schema = '') |
||
435 | |||
436 | /** |
||
437 | * Returns all unique indexes for the given table. |
||
438 | * Each array element is of the following structure: |
||
439 | * |
||
440 | * ```php |
||
441 | * [ |
||
442 | * 'IndexName1' => ['col1' [, ...]], |
||
443 | * 'IndexName2' => ['col2' [, ...]], |
||
444 | * ] |
||
445 | * ``` |
||
446 | * |
||
447 | * @param TableSchema $table the table metadata |
||
448 | * @return array all unique indexes for the given table. |
||
449 | * @since 2.0.4 |
||
450 | */ |
||
451 | public function findUniqueIndexes($table) |
||
459 | } |
||
460 |