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 |
||
| 33 | class Schema extends \yii\db\Schema implements ConstraintFinderInterface |
||
| 34 | { |
||
| 35 | use ConstraintFinderTrait; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array mapping from physical column types (keys) to abstract column types (values) |
||
| 39 | */ |
||
| 40 | public $typeMap = [ |
||
| 41 | 'tinyint' => self::TYPE_TINYINT, |
||
| 42 | 'bit' => self::TYPE_SMALLINT, |
||
| 43 | 'boolean' => self::TYPE_BOOLEAN, |
||
| 44 | 'bool' => self::TYPE_BOOLEAN, |
||
| 45 | 'smallint' => self::TYPE_SMALLINT, |
||
| 46 | 'mediumint' => self::TYPE_INTEGER, |
||
| 47 | 'int' => self::TYPE_INTEGER, |
||
| 48 | 'integer' => self::TYPE_INTEGER, |
||
| 49 | 'bigint' => self::TYPE_BIGINT, |
||
| 50 | 'float' => self::TYPE_FLOAT, |
||
| 51 | 'double' => self::TYPE_DOUBLE, |
||
| 52 | 'real' => self::TYPE_FLOAT, |
||
| 53 | 'decimal' => self::TYPE_DECIMAL, |
||
| 54 | 'numeric' => self::TYPE_DECIMAL, |
||
| 55 | 'tinytext' => self::TYPE_TEXT, |
||
| 56 | 'mediumtext' => self::TYPE_TEXT, |
||
| 57 | 'longtext' => self::TYPE_TEXT, |
||
| 58 | 'text' => self::TYPE_TEXT, |
||
| 59 | 'varchar' => self::TYPE_STRING, |
||
| 60 | 'string' => self::TYPE_STRING, |
||
| 61 | 'char' => self::TYPE_CHAR, |
||
| 62 | 'blob' => self::TYPE_BINARY, |
||
| 63 | 'datetime' => self::TYPE_DATETIME, |
||
| 64 | 'year' => self::TYPE_DATE, |
||
| 65 | 'date' => self::TYPE_DATE, |
||
| 66 | 'time' => self::TYPE_TIME, |
||
| 67 | 'timestamp' => self::TYPE_TIMESTAMP, |
||
| 68 | 'enum' => self::TYPE_STRING, |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | protected $tableQuoteCharacter = '`'; |
||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | protected $columnQuoteCharacter = '`'; |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 6 | protected function findTableNames($schema = '') |
|
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | 293 | protected function loadTableSchema($name) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 37 | protected function loadTablePrimaryKey($tableName) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | 3 | protected function loadTableForeignKeys($tableName) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | 10 | protected function loadTableIndexes($tableName) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 12 | protected function loadTableUniques($tableName) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | 12 | protected function loadTableChecks($tableName) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | * @throws NotSupportedException if this method is called. |
||
| 196 | */ |
||
| 197 | 12 | protected function loadTableDefaultValues($tableName) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Creates a query builder for the MySQL database. |
||
| 204 | * This method may be overridden by child classes to create a DBMS-specific query builder. |
||
| 205 | * @return QueryBuilder query builder instance |
||
| 206 | */ |
||
| 207 | 270 | public function createQueryBuilder() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | * @return ColumnSchemaBuilder column schema builder instance |
||
| 215 | */ |
||
| 216 | 10 | public function createColumnSchemaBuilder($type, $length = null) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Collects the table column metadata. |
||
| 223 | * @param TableSchema $table the table metadata |
||
| 224 | * @return bool whether the table exists in the database |
||
| 225 | */ |
||
| 226 | 293 | protected function findColumns($table) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Collects the foreign key column details for the given table. |
||
| 251 | * @param TableSchema $table the table metadata |
||
| 252 | */ |
||
| 253 | 271 | protected function findConstraints($table) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Returns all unique indexes for the given table. |
||
| 270 | * |
||
| 271 | * Each array element is of the following structure: |
||
| 272 | * |
||
| 273 | * ```php |
||
| 274 | * [ |
||
| 275 | * 'IndexName1' => ['col1' [, ...]], |
||
| 276 | * 'IndexName2' => ['col2' [, ...]], |
||
| 277 | * ] |
||
| 278 | * ``` |
||
| 279 | * |
||
| 280 | * @param TableSchema $table the table metadata |
||
| 281 | * @return array all unique indexes for the given table. |
||
| 282 | */ |
||
| 283 | 1 | public function findUniqueIndexes($table) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Loads the column information into a [[ColumnSchema]] object. |
||
| 306 | * @param array $info column information |
||
| 307 | * @return ColumnSchema the column schema object |
||
| 308 | */ |
||
| 309 | 271 | protected function loadColumnSchema($info) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Sets the isolation level of the current transaction. |
||
| 361 | * @param string $level The transaction isolation level to use for this transaction. |
||
| 362 | * This can be either [[Transaction::READ_UNCOMMITTED]] or [[Transaction::SERIALIZABLE]]. |
||
| 363 | * @throws NotSupportedException when unsupported isolation levels are used. |
||
| 364 | * SQLite only supports SERIALIZABLE and READ UNCOMMITTED. |
||
| 365 | * @see http://www.sqlite.org/pragma.html#pragma_read_uncommitted |
||
| 366 | */ |
||
| 367 | 2 | public function setTransactionIsolationLevel($level) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Returns table columns info. |
||
| 383 | * @param string $tableName table name |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | 30 | private function loadTableColumnsInfo($tableName) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Loads multiple types of constraints and returns the specified ones. |
||
| 396 | * @param string $tableName table name. |
||
| 397 | * @param string $returnType return type: |
||
| 398 | * - primaryKey |
||
| 399 | * - indexes |
||
| 400 | * - uniques |
||
| 401 | * @return mixed constraints. |
||
| 402 | */ |
||
| 403 | 59 | private function loadTableConstraints($tableName, $returnType) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Return whether the specified identifier is a SQLite system identifier. |
||
| 478 | * @param string $identifier |
||
| 479 | * @return bool |
||
| 480 | * @see https://www.sqlite.org/src/artifact/74108007d286232f |
||
| 481 | */ |
||
| 482 | private function isSystemIdentifier($identifier) |
||
| 486 | } |
||
| 487 |
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.