Complex classes like AbstractSchemaManager 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 AbstractSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | abstract class AbstractSchemaManager |
||
32 | { |
||
33 | /** |
||
34 | * Holds instance of the Doctrine connection for this schema manager. |
||
35 | * |
||
36 | * @var Connection |
||
37 | */ |
||
38 | protected $_conn; |
||
39 | |||
40 | /** |
||
41 | * Holds instance of the database platform used for this schema manager. |
||
42 | * |
||
43 | * @var AbstractPlatform |
||
44 | */ |
||
45 | protected $_platform; |
||
46 | |||
47 | /** |
||
48 | * Constructor. Accepts the Connection instance to manage the schema for. |
||
49 | */ |
||
50 | 5454 | public function __construct(Connection $conn, ?AbstractPlatform $platform = null) |
|
55 | |||
56 | /** |
||
57 | * Returns the associated platform. |
||
58 | * |
||
59 | * @return AbstractPlatform |
||
60 | */ |
||
61 | 3465 | public function getDatabasePlatform() |
|
65 | |||
66 | /** |
||
67 | * Tries any method on the schema manager. Normally a method throws an |
||
68 | * exception when your DBMS doesn't support it or if an error occurs. |
||
69 | * This method allows you to try and method on your SchemaManager |
||
70 | * instance and will return false if it does not work or is not supported. |
||
71 | * |
||
72 | * <code> |
||
73 | * $result = $sm->tryMethod('dropView', 'view_name'); |
||
74 | * </code> |
||
75 | * |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 5252 | public function tryMethod() |
|
94 | |||
95 | /** |
||
96 | * Lists the available databases for this connection. |
||
97 | * |
||
98 | * @return string[] |
||
99 | */ |
||
100 | 3085 | public function listDatabases() |
|
108 | |||
109 | /** |
||
110 | * Returns a list of all namespaces in the current database. |
||
111 | * |
||
112 | * @return string[] |
||
113 | */ |
||
114 | 1247 | public function listNamespaceNames() |
|
122 | |||
123 | /** |
||
124 | * Lists the available sequences for this connection. |
||
125 | * |
||
126 | * @param string|null $database |
||
127 | * |
||
128 | * @return Sequence[] |
||
129 | */ |
||
130 | 1279 | public function listSequences($database = null) |
|
142 | |||
143 | /** |
||
144 | * Lists the columns for a given table. |
||
145 | * |
||
146 | * In contrast to other libraries and to the old version of Doctrine, |
||
147 | * this column definition does try to contain the 'primary' field for |
||
148 | * the reason that it is not portable across different RDBMS. Use |
||
149 | * {@see listTableIndexes($tableName)} to retrieve the primary key |
||
150 | * of a table. Where a RDBMS specifies more details, these are held |
||
151 | * in the platformDetails array. |
||
152 | * |
||
153 | * @param string $table The name of the table. |
||
154 | * @param string|null $database |
||
155 | * |
||
156 | * @return Column[] |
||
157 | */ |
||
158 | 4102 | public function listTableColumns($table, $database = null) |
|
170 | |||
171 | /** |
||
172 | * Lists the indexes for a given table returning an array of Index instances. |
||
173 | * |
||
174 | * Keys of the portable indexes list are all lower-cased. |
||
175 | * |
||
176 | * @param string $table The name of the table. |
||
177 | * |
||
178 | * @return Index[] |
||
179 | */ |
||
180 | 3783 | public function listTableIndexes($table) |
|
188 | |||
189 | /** |
||
190 | * Returns true if all the given tables exist. |
||
191 | * |
||
192 | * The usage of a string $tableNames is deprecated. Pass a one-element array instead. |
||
193 | * |
||
194 | * @param string|string[] $tableNames |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 4564 | public function tablesExist($tableNames) |
|
204 | |||
205 | /** |
||
206 | * Returns a list of all tables in the current database. |
||
207 | * |
||
208 | * @return string[] |
||
209 | */ |
||
210 | 4398 | public function listTableNames() |
|
219 | |||
220 | /** |
||
221 | * Filters asset names if they are configured to return only a subset of all |
||
222 | * the found elements. |
||
223 | * |
||
224 | * @param mixed[] $assetNames |
||
225 | * |
||
226 | * @return mixed[] |
||
227 | */ |
||
228 | 4584 | protected function filterAssetNames($assetNames) |
|
237 | |||
238 | /** |
||
239 | * @deprecated Use Configuration::getSchemaAssetsFilter() instead |
||
240 | * |
||
241 | * @return string|null |
||
242 | */ |
||
243 | protected function getFilterSchemaAssetsExpression() |
||
247 | |||
248 | /** |
||
249 | * Lists the tables for this connection. |
||
250 | * |
||
251 | * @return Table[] |
||
252 | */ |
||
253 | 3879 | public function listTables() |
|
264 | |||
265 | /** |
||
266 | * @param string $tableName |
||
267 | * |
||
268 | * @return Table |
||
269 | */ |
||
270 | 4090 | public function listTableDetails($tableName) |
|
282 | |||
283 | /** |
||
284 | * Lists the views this connection has. |
||
285 | * |
||
286 | * @return View[] |
||
287 | */ |
||
288 | 2860 | public function listViews() |
|
296 | |||
297 | /** |
||
298 | * Lists the foreign keys for the given table. |
||
299 | * |
||
300 | * @param string $table The name of the table. |
||
301 | * @param string|null $database |
||
302 | * |
||
303 | * @return ForeignKeyConstraint[] |
||
304 | */ |
||
305 | 3967 | public function listTableForeignKeys($table, $database = null) |
|
316 | |||
317 | /* drop*() Methods */ |
||
318 | |||
319 | /** |
||
320 | * Drops a database. |
||
321 | * |
||
322 | * NOTE: You can not drop the database this SchemaManager is currently connected to. |
||
323 | * |
||
324 | * @param string $database The name of the database to drop. |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | 4752 | public function dropDatabase($database) |
|
332 | |||
333 | /** |
||
334 | * Drops the given table. |
||
335 | * |
||
336 | * @param string $tableName The name of the table to drop. |
||
337 | * |
||
338 | * @return void |
||
339 | */ |
||
340 | 5051 | public function dropTable($tableName) |
|
344 | |||
345 | /** |
||
346 | * Drops the index from the given table. |
||
347 | * |
||
348 | * @param Index|string $index The name of the index. |
||
349 | * @param Table|string $table The name of the table. |
||
350 | * |
||
351 | * @return void |
||
352 | */ |
||
353 | 2977 | public function dropIndex($index, $table) |
|
361 | |||
362 | /** |
||
363 | * Drops the constraint from the given table. |
||
364 | * |
||
365 | * @param Table|string $table The name of the table. |
||
366 | * |
||
367 | * @return void |
||
368 | */ |
||
369 | public function dropConstraint(Constraint $constraint, $table) |
||
373 | |||
374 | /** |
||
375 | * Drops a foreign key from a table. |
||
376 | * |
||
377 | * @param ForeignKeyConstraint|string $foreignKey The name of the foreign key. |
||
378 | * @param Table|string $table The name of the table with the foreign key. |
||
379 | * |
||
380 | * @return void |
||
381 | */ |
||
382 | public function dropForeignKey($foreignKey, $table) |
||
386 | |||
387 | /** |
||
388 | * Drops a sequence with a given name. |
||
389 | * |
||
390 | * @param string $name The name of the sequence to drop. |
||
391 | * |
||
392 | * @return void |
||
393 | */ |
||
394 | 1276 | public function dropSequence($name) |
|
398 | |||
399 | /** |
||
400 | * Drops a view. |
||
401 | * |
||
402 | * @param string $name The name of the view. |
||
403 | * |
||
404 | * @return void |
||
405 | */ |
||
406 | 3052 | public function dropView($name) |
|
410 | |||
411 | /* create*() Methods */ |
||
412 | |||
413 | /** |
||
414 | * Creates a new database. |
||
415 | * |
||
416 | * @param string $database The name of the database to create. |
||
417 | * |
||
418 | * @return void |
||
419 | */ |
||
420 | 4750 | public function createDatabase($database) |
|
424 | |||
425 | /** |
||
426 | * Creates a new table. |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | 5068 | public function createTable(Table $table) |
|
435 | |||
436 | /** |
||
437 | * Creates a new sequence. |
||
438 | * |
||
439 | * @param Sequence $sequence |
||
440 | * |
||
441 | * @return void |
||
442 | * |
||
443 | * @throws ConnectionException If something fails at database level. |
||
444 | */ |
||
445 | 1278 | public function createSequence($sequence) |
|
449 | |||
450 | /** |
||
451 | * Creates a constraint on a table. |
||
452 | * |
||
453 | * @param Table|string $table |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | 1 | public function createConstraint(Constraint $constraint, $table) |
|
461 | |||
462 | /** |
||
463 | * Creates a new index on a table. |
||
464 | * |
||
465 | * @param Table|string $table The name of the table on which the index is to be created. |
||
466 | * |
||
467 | * @return void |
||
468 | */ |
||
469 | 2977 | public function createIndex(Index $index, $table) |
|
473 | |||
474 | /** |
||
475 | * Creates a new foreign key. |
||
476 | * |
||
477 | * @param ForeignKeyConstraint $foreignKey The ForeignKey instance. |
||
478 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
479 | * |
||
480 | * @return void |
||
481 | */ |
||
482 | 3021 | public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) |
|
486 | |||
487 | /** |
||
488 | * Creates a new view. |
||
489 | * |
||
490 | * @return void |
||
491 | */ |
||
492 | 3052 | public function createView(View $view) |
|
496 | |||
497 | /* dropAndCreate*() Methods */ |
||
498 | |||
499 | /** |
||
500 | * Drops and creates a constraint. |
||
501 | * |
||
502 | * @see dropConstraint() |
||
503 | * @see createConstraint() |
||
504 | * |
||
505 | * @param Table|string $table |
||
506 | * |
||
507 | * @return void |
||
508 | */ |
||
509 | public function dropAndCreateConstraint(Constraint $constraint, $table) |
||
514 | |||
515 | /** |
||
516 | * Drops and creates a new index on a table. |
||
517 | * |
||
518 | * @param Table|string $table The name of the table on which the index is to be created. |
||
519 | * |
||
520 | * @return void |
||
521 | */ |
||
522 | 2977 | public function dropAndCreateIndex(Index $index, $table) |
|
527 | |||
528 | /** |
||
529 | * Drops and creates a new foreign key. |
||
530 | * |
||
531 | * @param ForeignKeyConstraint $foreignKey An associative array that defines properties of the foreign key to be created. |
||
532 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
533 | * |
||
534 | * @return void |
||
535 | */ |
||
536 | public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) |
||
541 | |||
542 | /** |
||
543 | * Drops and create a new sequence. |
||
544 | * |
||
545 | * @return void |
||
546 | * |
||
547 | * @throws ConnectionException If something fails at database level. |
||
548 | */ |
||
549 | 1276 | public function dropAndCreateSequence(Sequence $sequence) |
|
554 | |||
555 | /** |
||
556 | * Drops and creates a new table. |
||
557 | * |
||
558 | * @return void |
||
559 | */ |
||
560 | 5010 | public function dropAndCreateTable(Table $table) |
|
565 | |||
566 | /** |
||
567 | * Drops and creates a new database. |
||
568 | * |
||
569 | * @param string $database The name of the database to create. |
||
570 | * |
||
571 | * @return void |
||
572 | */ |
||
573 | 4900 | public function dropAndCreateDatabase($database) |
|
578 | |||
579 | /** |
||
580 | * Drops and creates a new view. |
||
581 | * |
||
582 | * @return void |
||
583 | */ |
||
584 | 3052 | public function dropAndCreateView(View $view) |
|
589 | |||
590 | /* alterTable() Methods */ |
||
591 | |||
592 | /** |
||
593 | * Alters an existing tables schema. |
||
594 | * |
||
595 | * @return void |
||
596 | */ |
||
597 | 3650 | public function alterTable(TableDiff $tableDiff) |
|
608 | |||
609 | /** |
||
610 | * Renames a given table to another name. |
||
611 | * |
||
612 | * @param string $name The current name of the table. |
||
613 | * @param string $newName The new name of the table. |
||
614 | * |
||
615 | * @return void |
||
616 | */ |
||
617 | 1 | public function renameTable($name, $newName) |
|
623 | |||
624 | /** |
||
625 | * Methods for filtering return values of list*() methods to convert |
||
626 | * the native DBMS data definition to a portable Doctrine definition |
||
627 | */ |
||
628 | |||
629 | /** |
||
630 | * @param mixed[] $databases |
||
631 | * |
||
632 | * @return string[] |
||
633 | */ |
||
634 | 2935 | protected function _getPortableDatabasesList($databases) |
|
649 | |||
650 | /** |
||
651 | * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition. |
||
652 | * |
||
653 | * @param mixed[][] $namespaces The list of namespace names in the native DBMS data definition. |
||
654 | * |
||
655 | * @return string[] |
||
656 | */ |
||
657 | 1247 | protected function getPortableNamespacesList(array $namespaces) |
|
667 | |||
668 | /** |
||
669 | * @param mixed $database |
||
670 | * |
||
671 | * @return mixed |
||
672 | */ |
||
673 | protected function _getPortableDatabaseDefinition($database) |
||
677 | |||
678 | /** |
||
679 | * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition. |
||
680 | * |
||
681 | * @param mixed[] $namespace The native DBMS namespace definition. |
||
682 | * |
||
683 | * @return mixed |
||
684 | */ |
||
685 | protected function getPortableNamespaceDefinition(array $namespace) |
||
689 | |||
690 | /** |
||
691 | * @deprecated |
||
692 | * |
||
693 | * @param mixed[][] $functions |
||
694 | * |
||
695 | * @return mixed[][] |
||
696 | */ |
||
697 | protected function _getPortableFunctionsList($functions) |
||
712 | |||
713 | /** |
||
714 | * @deprecated |
||
715 | * |
||
716 | * @param mixed[] $function |
||
717 | * |
||
718 | * @return mixed |
||
719 | */ |
||
720 | protected function _getPortableFunctionDefinition($function) |
||
724 | |||
725 | /** |
||
726 | * @param mixed[][] $triggers |
||
727 | * |
||
728 | * @return mixed[][] |
||
729 | */ |
||
730 | protected function _getPortableTriggersList($triggers) |
||
745 | |||
746 | /** |
||
747 | * @param mixed[] $trigger |
||
748 | * |
||
749 | * @return mixed |
||
750 | */ |
||
751 | protected function _getPortableTriggerDefinition($trigger) |
||
755 | |||
756 | /** |
||
757 | * @param mixed[][] $sequences |
||
758 | * |
||
759 | * @return Sequence[] |
||
760 | */ |
||
761 | 288 | protected function _getPortableSequencesList($sequences) |
|
771 | |||
772 | /** |
||
773 | * @param mixed[] $sequence |
||
774 | * |
||
775 | * @return Sequence |
||
776 | * |
||
777 | * @throws DBALException |
||
778 | */ |
||
779 | protected function _getPortableSequenceDefinition($sequence) |
||
783 | |||
784 | /** |
||
785 | * Independent of the database the keys of the column list result are lowercased. |
||
786 | * |
||
787 | * The name of the created column instance however is kept in its case. |
||
788 | * |
||
789 | * @param string $table The name of the table. |
||
790 | * @param string $database |
||
791 | * @param mixed[][] $tableColumns |
||
792 | * |
||
793 | * @return Column[] |
||
794 | */ |
||
795 | 4102 | protected function _getPortableTableColumnList($table, $database, $tableColumns) |
|
826 | |||
827 | /** |
||
828 | * Gets Table Column Definition. |
||
829 | * |
||
830 | * @param mixed[] $tableColumn |
||
831 | * |
||
832 | * @return Column |
||
833 | */ |
||
834 | abstract protected function _getPortableTableColumnDefinition($tableColumn); |
||
835 | |||
836 | /** |
||
837 | * Aggregates and groups the index results according to the required data result. |
||
838 | * |
||
839 | * @param mixed[][] $tableIndexRows |
||
840 | * @param string|null $tableName |
||
841 | * |
||
842 | * @return Index[] |
||
843 | */ |
||
844 | 4095 | protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null) |
|
906 | |||
907 | /** |
||
908 | * @param mixed[][] $tables |
||
909 | * |
||
910 | * @return string[] |
||
911 | */ |
||
912 | 4398 | protected function _getPortableTablesList($tables) |
|
927 | |||
928 | /** |
||
929 | * @param mixed $table |
||
930 | * |
||
931 | * @return string |
||
932 | */ |
||
933 | protected function _getPortableTableDefinition($table) |
||
937 | |||
938 | /** |
||
939 | * @param mixed[][] $users |
||
940 | * |
||
941 | * @return string[][] |
||
942 | */ |
||
943 | protected function _getPortableUsersList($users) |
||
958 | |||
959 | /** |
||
960 | * @param string[] $user |
||
961 | * |
||
962 | * @return string[] |
||
963 | */ |
||
964 | protected function _getPortableUserDefinition($user) |
||
968 | |||
969 | /** |
||
970 | * @param mixed[][] $views |
||
971 | * |
||
972 | * @return View[] |
||
973 | */ |
||
974 | 2860 | protected function _getPortableViewsList($views) |
|
990 | |||
991 | /** |
||
992 | * @param mixed[] $view |
||
993 | * |
||
994 | * @return View|false |
||
995 | */ |
||
996 | protected function _getPortableViewDefinition($view) |
||
1000 | |||
1001 | /** |
||
1002 | * @param mixed[][] $tableForeignKeys |
||
1003 | * |
||
1004 | * @return ForeignKeyConstraint[] |
||
1005 | */ |
||
1006 | 1682 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
|
1016 | |||
1017 | /** |
||
1018 | * @param mixed $tableForeignKey |
||
1019 | * |
||
1020 | * @return ForeignKeyConstraint |
||
1021 | */ |
||
1022 | protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
||
1026 | |||
1027 | /** |
||
1028 | * @param string[]|string $sql |
||
1029 | * |
||
1030 | * @return void |
||
1031 | */ |
||
1032 | 5281 | protected function _execSql($sql) |
|
1038 | |||
1039 | /** |
||
1040 | * Creates a schema instance for the current database. |
||
1041 | * |
||
1042 | * @return Schema |
||
1043 | */ |
||
1044 | 3676 | public function createSchema() |
|
1062 | |||
1063 | /** |
||
1064 | * Creates the configuration for this schema. |
||
1065 | * |
||
1066 | * @return SchemaConfig |
||
1067 | */ |
||
1068 | 3946 | public function createSchemaConfig() |
|
1091 | |||
1092 | /** |
||
1093 | * The search path for namespaces in the currently connected database. |
||
1094 | * |
||
1095 | * The first entry is usually the default namespace in the Schema. All |
||
1096 | * further namespaces contain tables/sequences which can also be addressed |
||
1097 | * with a short, not full-qualified name. |
||
1098 | * |
||
1099 | * For databases that don't support subschema/namespaces this method |
||
1100 | * returns the name of the currently connected database. |
||
1101 | * |
||
1102 | * @return string[] |
||
1103 | */ |
||
1104 | 3071 | public function getSchemaSearchPaths() |
|
1108 | |||
1109 | /** |
||
1110 | * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns |
||
1111 | * the type given as default. |
||
1112 | * |
||
1113 | * @param string|null $comment |
||
1114 | * @param string $currentType |
||
1115 | * |
||
1116 | * @return string |
||
1117 | */ |
||
1118 | 4104 | public function extractDoctrineTypeFromComment($comment, $currentType) |
|
1126 | |||
1127 | /** |
||
1128 | * @param string|null $comment |
||
1129 | * @param string|null $type |
||
1130 | * |
||
1131 | * @return string|null |
||
1132 | */ |
||
1133 | 4057 | public function removeDoctrineTypeFromComment($comment, $type) |
|
1141 | } |
||
1142 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: