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 |
||
32 | class Schema extends \yii\db\Schema |
||
33 | { |
||
34 | use ConstraintFinderTrait; |
||
35 | |||
36 | /** |
||
37 | * @var array mapping from physical column types (keys) to abstract column types (values) |
||
38 | */ |
||
39 | public $typeMap = [ |
||
40 | 'tinyint' => self::TYPE_SMALLINT, |
||
41 | 'bit' => self::TYPE_SMALLINT, |
||
42 | 'boolean' => self::TYPE_BOOLEAN, |
||
43 | 'bool' => self::TYPE_BOOLEAN, |
||
44 | 'smallint' => self::TYPE_SMALLINT, |
||
45 | 'mediumint' => self::TYPE_INTEGER, |
||
46 | 'int' => self::TYPE_INTEGER, |
||
47 | 'integer' => self::TYPE_INTEGER, |
||
48 | 'bigint' => self::TYPE_BIGINT, |
||
49 | 'float' => self::TYPE_FLOAT, |
||
50 | 'double' => self::TYPE_DOUBLE, |
||
51 | 'real' => self::TYPE_FLOAT, |
||
52 | 'decimal' => self::TYPE_DECIMAL, |
||
53 | 'numeric' => self::TYPE_DECIMAL, |
||
54 | 'tinytext' => self::TYPE_TEXT, |
||
55 | 'mediumtext' => self::TYPE_TEXT, |
||
56 | 'longtext' => self::TYPE_TEXT, |
||
57 | 'text' => self::TYPE_TEXT, |
||
58 | 'varchar' => self::TYPE_STRING, |
||
59 | 'string' => self::TYPE_STRING, |
||
60 | 'char' => self::TYPE_CHAR, |
||
61 | 'blob' => self::TYPE_BINARY, |
||
62 | 'datetime' => self::TYPE_DATETIME, |
||
63 | 'year' => self::TYPE_DATE, |
||
64 | 'date' => self::TYPE_DATE, |
||
65 | 'time' => self::TYPE_TIME, |
||
66 | 'timestamp' => self::TYPE_TIMESTAMP, |
||
67 | 'enum' => self::TYPE_STRING, |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * @inheritDoc |
||
72 | */ |
||
73 | 5 | protected function findTableNames($schema = '') |
|
74 | { |
||
75 | 5 | $sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name"; |
|
76 | 5 | return $this->db->createCommand($sql)->queryColumn(); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @inheritDoc |
||
81 | */ |
||
82 | 209 | protected function loadTableSchema($name) |
|
83 | { |
||
84 | 209 | $table = new TableSchema(); |
|
85 | 209 | $table->name = $name; |
|
86 | 209 | $table->fullName = $name; |
|
87 | |||
88 | 209 | if ($this->findColumns($table)) { |
|
89 | 198 | $this->findConstraints($table); |
|
90 | 198 | return $table; |
|
91 | } |
||
92 | |||
93 | 36 | return null; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @inheritDoc |
||
98 | */ |
||
99 | 12 | protected function loadTablePrimaryKey($tableName) |
|
103 | |||
104 | /** |
||
105 | * @inheritDoc |
||
106 | */ |
||
107 | 3 | protected function loadTableForeignKeys($tableName) |
|
125 | |||
126 | /** |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | 10 | protected function loadTableIndexes($tableName) |
|
133 | |||
134 | /** |
||
135 | * @inheritDoc |
||
136 | */ |
||
137 | 12 | protected function loadTableUniques($tableName) |
|
141 | |||
142 | /** |
||
143 | * @inheritDoc |
||
144 | */ |
||
145 | 12 | protected function loadTableChecks($tableName) |
|
179 | |||
180 | /** |
||
181 | * @inheritDoc |
||
182 | * @throws NotSupportedException if this method is called. |
||
183 | */ |
||
184 | 12 | protected function loadTableDefaultValues($tableName) |
|
188 | |||
189 | /** |
||
190 | * Quotes a table name for use in a query. |
||
191 | * A simple table name has no schema prefix. |
||
192 | * @param string $name table name |
||
193 | * @return string the properly quoted table name |
||
194 | */ |
||
195 | 303 | public function quoteSimpleTableName($name) |
|
199 | |||
200 | /** |
||
201 | * Quotes a column name for use in a query. |
||
202 | * A simple column name has no prefix. |
||
203 | * @param string $name column name |
||
204 | * @return string the properly quoted column name |
||
205 | */ |
||
206 | 300 | public function quoteSimpleColumnName($name) |
|
210 | |||
211 | /** |
||
212 | * Creates a query builder for the MySQL database. |
||
213 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
214 | * @return QueryBuilder query builder instance |
||
215 | */ |
||
216 | 216 | public function createQueryBuilder() |
|
220 | |||
221 | /** |
||
222 | * @inheritdoc |
||
223 | * @return ColumnSchemaBuilder column schema builder instance |
||
224 | */ |
||
225 | 4 | public function createColumnSchemaBuilder($type, $length = null) |
|
229 | |||
230 | /** |
||
231 | * Collects the table column metadata. |
||
232 | * @param TableSchema $table the table metadata |
||
233 | * @return bool whether the table exists in the database |
||
234 | */ |
||
235 | 209 | protected function findColumns($table) |
|
257 | |||
258 | /** |
||
259 | * Collects the foreign key column details for the given table. |
||
260 | * @param TableSchema $table the table metadata |
||
261 | */ |
||
262 | 198 | protected function findConstraints($table) |
|
276 | |||
277 | /** |
||
278 | * Returns all unique indexes for the given table. |
||
279 | * Each array element is of the following structure: |
||
280 | * |
||
281 | * ```php |
||
282 | * [ |
||
283 | * 'IndexName1' => ['col1' [, ...]], |
||
284 | * 'IndexName2' => ['col2' [, ...]], |
||
285 | * ] |
||
286 | * ``` |
||
287 | * |
||
288 | * @param TableSchema $table the table metadata |
||
289 | * @return array all unique indexes for the given table. |
||
290 | */ |
||
291 | 1 | public function findUniqueIndexes($table) |
|
311 | |||
312 | /** |
||
313 | * Loads the column information into a [[ColumnSchema]] object. |
||
314 | * @param array $info column information |
||
315 | * @return ColumnSchema the column schema object |
||
316 | */ |
||
317 | 198 | protected function loadColumnSchema($info) |
|
366 | |||
367 | /** |
||
368 | * Sets the isolation level of the current transaction. |
||
369 | * @param string $level The transaction isolation level to use for this transaction. |
||
370 | * This can be either [[Transaction::READ_UNCOMMITTED]] or [[Transaction::SERIALIZABLE]]. |
||
371 | * @throws NotSupportedException when unsupported isolation levels are used. |
||
372 | * SQLite only supports SERIALIZABLE and READ UNCOMMITTED. |
||
373 | * @see http://www.sqlite.org/pragma.html#pragma_read_uncommitted |
||
374 | */ |
||
375 | 2 | public function setTransactionIsolationLevel($level) |
|
388 | |||
389 | /** |
||
390 | * Loads multiple types of constraints and returns the specified ones. |
||
391 | * @param string $tableName table name. |
||
392 | * @param string $returnType return type: |
||
393 | * - primaryKey |
||
394 | * - indexes |
||
395 | * - uniques |
||
396 | * @return mixed constraints. |
||
397 | */ |
||
398 | 34 | private function loadTableConstraints($tableName, $returnType) |
|
452 | |||
453 | /** |
||
454 | * Return whether the specified identifier is a SQLite system identifier. |
||
455 | * @param string $identifier |
||
456 | * @return bool |
||
457 | * @see https://www.sqlite.org/src/artifact/74108007d286232f |
||
458 | */ |
||
459 | private function isSystemIdentifier($identifier) |
||
463 | } |
||
464 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.