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 |
||
29 | class Schema extends \yii\db\Schema implements ConstraintFinderInterface |
||
30 | { |
||
31 | use ViewFinderTrait; |
||
32 | use ConstraintFinderTrait; |
||
33 | |||
34 | const TYPE_JSONB = 'jsonb'; |
||
35 | |||
36 | /** |
||
37 | * @var string the default schema used for the current session. |
||
38 | */ |
||
39 | public $defaultSchema = 'public'; |
||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public $columnSchemaClass = 'yii\db\pgsql\ColumnSchema'; |
||
44 | /** |
||
45 | * @var array mapping from physical column types (keys) to abstract |
||
46 | * column types (values) |
||
47 | * @see http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-TABLE |
||
48 | */ |
||
49 | public $typeMap = [ |
||
50 | 'bit' => self::TYPE_INTEGER, |
||
51 | 'bit varying' => self::TYPE_INTEGER, |
||
52 | 'varbit' => self::TYPE_INTEGER, |
||
53 | |||
54 | 'bool' => self::TYPE_BOOLEAN, |
||
55 | 'boolean' => self::TYPE_BOOLEAN, |
||
56 | |||
57 | 'box' => self::TYPE_STRING, |
||
58 | 'circle' => self::TYPE_STRING, |
||
59 | 'point' => self::TYPE_STRING, |
||
60 | 'line' => self::TYPE_STRING, |
||
61 | 'lseg' => self::TYPE_STRING, |
||
62 | 'polygon' => self::TYPE_STRING, |
||
63 | 'path' => self::TYPE_STRING, |
||
64 | |||
65 | 'character' => self::TYPE_CHAR, |
||
66 | 'char' => self::TYPE_CHAR, |
||
67 | 'bpchar' => self::TYPE_CHAR, |
||
68 | 'character varying' => self::TYPE_STRING, |
||
69 | 'varchar' => self::TYPE_STRING, |
||
70 | 'text' => self::TYPE_TEXT, |
||
71 | |||
72 | 'bytea' => self::TYPE_BINARY, |
||
73 | |||
74 | 'cidr' => self::TYPE_STRING, |
||
75 | 'inet' => self::TYPE_STRING, |
||
76 | 'macaddr' => self::TYPE_STRING, |
||
77 | |||
78 | 'real' => self::TYPE_FLOAT, |
||
79 | 'float4' => self::TYPE_FLOAT, |
||
80 | 'double precision' => self::TYPE_DOUBLE, |
||
81 | 'float8' => self::TYPE_DOUBLE, |
||
82 | 'decimal' => self::TYPE_DECIMAL, |
||
83 | 'numeric' => self::TYPE_DECIMAL, |
||
84 | |||
85 | 'money' => self::TYPE_MONEY, |
||
86 | |||
87 | 'smallint' => self::TYPE_SMALLINT, |
||
88 | 'int2' => self::TYPE_SMALLINT, |
||
89 | 'int4' => self::TYPE_INTEGER, |
||
90 | 'int' => self::TYPE_INTEGER, |
||
91 | 'integer' => self::TYPE_INTEGER, |
||
92 | 'bigint' => self::TYPE_BIGINT, |
||
93 | 'int8' => self::TYPE_BIGINT, |
||
94 | 'oid' => self::TYPE_BIGINT, // should not be used. it's pg internal! |
||
95 | |||
96 | 'smallserial' => self::TYPE_SMALLINT, |
||
97 | 'serial2' => self::TYPE_SMALLINT, |
||
98 | 'serial4' => self::TYPE_INTEGER, |
||
99 | 'serial' => self::TYPE_INTEGER, |
||
100 | 'bigserial' => self::TYPE_BIGINT, |
||
101 | 'serial8' => self::TYPE_BIGINT, |
||
102 | 'pg_lsn' => self::TYPE_BIGINT, |
||
103 | |||
104 | 'date' => self::TYPE_DATE, |
||
105 | 'interval' => self::TYPE_STRING, |
||
106 | 'time without time zone' => self::TYPE_TIME, |
||
107 | 'time' => self::TYPE_TIME, |
||
108 | 'time with time zone' => self::TYPE_TIME, |
||
109 | 'timetz' => self::TYPE_TIME, |
||
110 | 'timestamp without time zone' => self::TYPE_TIMESTAMP, |
||
111 | 'timestamp' => self::TYPE_TIMESTAMP, |
||
112 | 'timestamp with time zone' => self::TYPE_TIMESTAMP, |
||
113 | 'timestamptz' => self::TYPE_TIMESTAMP, |
||
114 | 'abstime' => self::TYPE_TIMESTAMP, |
||
115 | |||
116 | 'tsquery' => self::TYPE_STRING, |
||
117 | 'tsvector' => self::TYPE_STRING, |
||
118 | 'txid_snapshot' => self::TYPE_STRING, |
||
119 | |||
120 | 'unknown' => self::TYPE_STRING, |
||
121 | |||
122 | 'uuid' => self::TYPE_STRING, |
||
123 | 'json' => self::TYPE_JSON, |
||
124 | 'jsonb' => self::TYPE_JSON, |
||
125 | 'xml' => self::TYPE_STRING, |
||
126 | ]; |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | protected $tableQuoteCharacter = '"'; |
||
132 | |||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 75 | protected function resolveTableName($name) |
|
138 | { |
||
139 | 75 | $resolvedName = new TableSchema(); |
|
140 | 75 | $parts = explode('.', str_replace('"', '', $name)); |
|
141 | 75 | if (isset($parts[1])) { |
|
142 | $resolvedName->schemaName = $parts[0]; |
||
143 | $resolvedName->name = $parts[1]; |
||
144 | } else { |
||
145 | 75 | $resolvedName->schemaName = $this->defaultSchema; |
|
146 | 75 | $resolvedName->name = $name; |
|
147 | } |
||
148 | 75 | $resolvedName->fullName = ($resolvedName->schemaName !== $this->defaultSchema ? $resolvedName->schemaName . '.' : '') . $resolvedName->name; |
|
149 | 75 | return $resolvedName; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | 2 | protected function findSchemaNames() |
|
156 | { |
||
157 | 2 | static $sql = <<<'SQL' |
|
158 | SELECT "ns"."nspname" |
||
159 | FROM "pg_namespace" AS "ns" |
||
160 | WHERE "ns"."nspname" != 'information_schema' AND "ns"."nspname" NOT LIKE 'pg_%' |
||
161 | ORDER BY "ns"."nspname" ASC |
||
162 | SQL; |
||
163 | |||
164 | 2 | return $this->db->createCommand($sql)->queryColumn(); |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 5 | protected function findTableNames($schema = '') |
|
171 | { |
||
172 | 5 | if ($schema === '') { |
|
173 | 5 | $schema = $this->defaultSchema; |
|
174 | } |
||
175 | $sql = <<<'SQL' |
||
176 | 5 | SELECT c.relname AS table_name |
|
177 | FROM pg_class c |
||
178 | INNER JOIN pg_namespace ns ON ns.oid = c.relnamespace |
||
179 | WHERE ns.nspname = :schemaName AND c.relkind IN ('r','v','m','f') |
||
180 | ORDER BY c.relname |
||
181 | SQL; |
||
182 | 5 | return $this->db->createCommand($sql, [':schemaName' => $schema])->queryColumn(); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | 340 | protected function loadTableSchema($name) |
|
189 | { |
||
190 | 340 | $table = new TableSchema(); |
|
191 | 340 | $this->resolveTableNames($table, $name); |
|
192 | 340 | if ($this->findColumns($table)) { |
|
193 | 334 | $this->findConstraints($table); |
|
194 | 334 | return $table; |
|
195 | } |
||
196 | |||
197 | 18 | return null; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | 35 | protected function loadTablePrimaryKey($tableName) |
|
204 | { |
||
205 | 35 | return $this->loadTableConstraints($tableName, 'primaryKey'); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | 4 | protected function loadTableForeignKeys($tableName) |
|
212 | { |
||
213 | 4 | return $this->loadTableConstraints($tableName, 'foreignKeys'); |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 32 | protected function loadTableIndexes($tableName) |
|
220 | { |
||
221 | 32 | static $sql = <<<'SQL' |
|
222 | SELECT |
||
223 | "ic"."relname" AS "name", |
||
224 | "ia"."attname" AS "column_name", |
||
225 | "i"."indisunique" AS "index_is_unique", |
||
226 | "i"."indisprimary" AS "index_is_primary" |
||
227 | FROM "pg_class" AS "tc" |
||
228 | INNER JOIN "pg_namespace" AS "tcns" |
||
229 | ON "tcns"."oid" = "tc"."relnamespace" |
||
230 | INNER JOIN "pg_index" AS "i" |
||
231 | ON "i"."indrelid" = "tc"."oid" |
||
232 | INNER JOIN "pg_class" AS "ic" |
||
233 | ON "ic"."oid" = "i"."indexrelid" |
||
234 | INNER JOIN "pg_attribute" AS "ia" |
||
235 | ON "ia"."attrelid" = "i"."indrelid" AND "ia"."attnum" = ANY ("i"."indkey") |
||
236 | WHERE "tcns"."nspname" = :schemaName AND "tc"."relname" = :tableName |
||
237 | ORDER BY "ia"."attnum" ASC |
||
238 | SQL; |
||
239 | |||
240 | 32 | $resolvedName = $this->resolveTableName($tableName); |
|
241 | 32 | $indexes = $this->db->createCommand($sql, [ |
|
242 | 32 | ':schemaName' => $resolvedName->schemaName, |
|
243 | 32 | ':tableName' => $resolvedName->name, |
|
244 | 32 | ])->queryAll(); |
|
245 | 32 | $indexes = $this->normalizePdoRowKeyCase($indexes, true); |
|
246 | 32 | $indexes = ArrayHelper::index($indexes, null, 'name'); |
|
247 | 32 | $result = []; |
|
248 | 32 | foreach ($indexes as $name => $index) { |
|
249 | 29 | $result[] = new IndexConstraint([ |
|
250 | 29 | 'isPrimary' => (bool) $index[0]['index_is_primary'], |
|
251 | 29 | 'isUnique' => (bool) $index[0]['index_is_unique'], |
|
252 | 29 | 'name' => $name, |
|
253 | 29 | 'columnNames' => ArrayHelper::getColumn($index, 'column_name'), |
|
254 | ]); |
||
255 | } |
||
256 | |||
257 | 32 | return $result; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | 13 | protected function loadTableUniques($tableName) |
|
264 | { |
||
265 | 13 | return $this->loadTableConstraints($tableName, 'uniques'); |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | 13 | protected function loadTableChecks($tableName) |
|
272 | { |
||
273 | 13 | return $this->loadTableConstraints($tableName, 'checks'); |
|
274 | } |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | * @throws NotSupportedException if this method is called. |
||
279 | */ |
||
280 | 12 | protected function loadTableDefaultValues($tableName) |
|
|
|||
281 | { |
||
282 | 12 | throw new NotSupportedException('PostgreSQL does not support default value constraints.'); |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * Creates a query builder for the PostgreSQL database. |
||
287 | * @return QueryBuilder query builder instance |
||
288 | */ |
||
289 | 336 | public function createQueryBuilder() |
|
290 | { |
||
291 | 336 | return new QueryBuilder($this->db); |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Resolves the table name and schema name (if any). |
||
296 | * @param TableSchema $table the table metadata object |
||
297 | * @param string $name the table name |
||
298 | */ |
||
299 | 340 | protected function resolveTableNames($table, $name) |
|
300 | { |
||
301 | 340 | $parts = explode('.', str_replace('"', '', $name)); |
|
302 | |||
303 | 340 | if (isset($parts[1])) { |
|
304 | $table->schemaName = $parts[0]; |
||
305 | $table->name = $parts[1]; |
||
306 | } else { |
||
307 | 340 | $table->schemaName = $this->defaultSchema; |
|
308 | 340 | $table->name = $parts[0]; |
|
309 | } |
||
310 | |||
311 | 340 | $table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name; |
|
312 | 340 | } |
|
313 | |||
314 | /** |
||
315 | * {@inheritdoc] |
||
316 | */ |
||
317 | protected function findViewNames($schema = '') |
||
331 | |||
332 | /** |
||
333 | * Collects the foreign key column details for the given table. |
||
334 | * @param TableSchema $table the table metadata |
||
335 | */ |
||
336 | 334 | protected function findConstraints($table) |
|
392 | |||
393 | /** |
||
394 | * Gets information about given table unique indexes. |
||
395 | * @param TableSchema $table the table metadata |
||
396 | * @return array with index and column names |
||
397 | */ |
||
398 | 1 | protected function getUniqueIndexInformation($table) |
|
421 | |||
422 | /** |
||
423 | * Returns all unique indexes for the given table. |
||
424 | * |
||
425 | * Each array element is of the following structure: |
||
426 | * |
||
427 | * ```php |
||
428 | * [ |
||
429 | * 'IndexName1' => ['col1' [, ...]], |
||
430 | * 'IndexName2' => ['col2' [, ...]], |
||
431 | * ] |
||
432 | * ``` |
||
433 | * |
||
434 | * @param TableSchema $table the table metadata |
||
435 | * @return array all unique indexes for the given table. |
||
436 | */ |
||
437 | 1 | public function findUniqueIndexes($table) |
|
456 | |||
457 | /** |
||
458 | * Collects the metadata of table columns. |
||
459 | * @param TableSchema $table the table metadata |
||
460 | * @return bool whether the table exists in the database |
||
461 | */ |
||
462 | 340 | protected function findColumns($table) |
|
566 | |||
567 | /** |
||
568 | * Loads the column information into a [[ColumnSchema]] object. |
||
569 | * @param array $info column information |
||
570 | * @return ColumnSchema the column schema object |
||
571 | */ |
||
572 | 334 | protected function loadColumnSchema($info) |
|
598 | |||
599 | /** |
||
600 | * {@inheritdoc} |
||
601 | */ |
||
602 | 38 | public function insert($table, $columns) |
|
621 | |||
622 | /** |
||
623 | * Loads multiple types of constraints and returns the specified ones. |
||
624 | * @param string $tableName table name. |
||
625 | * @param string $returnType return type: |
||
626 | * - primaryKey |
||
627 | * - foreignKeys |
||
628 | * - uniques |
||
629 | * - checks |
||
630 | * @return mixed constraints. |
||
631 | */ |
||
632 | 65 | private function loadTableConstraints($tableName, $returnType) |
|
724 | } |
||
725 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.