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 | 5907 | public function __construct(Connection $conn, ?AbstractPlatform $platform = null) |
|
55 | |||
56 | /** |
||
57 | * Returns the associated platform. |
||
58 | * |
||
59 | * @return AbstractPlatform |
||
60 | */ |
||
61 | 3718 | 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 | 5718 | public function tryMethod() |
|
94 | |||
95 | /** |
||
96 | * Lists the available databases for this connection. |
||
97 | * |
||
98 | * @return string[] |
||
99 | */ |
||
100 | 3300 | public function listDatabases() |
|
108 | |||
109 | /** |
||
110 | * Returns a list of all namespaces in the current database. |
||
111 | * |
||
112 | * @return string[] |
||
113 | */ |
||
114 | 1225 | 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 | 1261 | public function listSequences($database = null) |
|
141 | |||
142 | /** |
||
143 | * Lists the columns for a given table. |
||
144 | * |
||
145 | * In contrast to other libraries and to the old version of Doctrine, |
||
146 | * this column definition does try to contain the 'primary' field for |
||
147 | * the reason that it is not portable across different RDBMS. Use |
||
148 | * {@see listTableIndexes($tableName)} to retrieve the primary key |
||
149 | * of a table. We're a RDBMS specifies more details these are held |
||
150 | * in the platformDetails array. |
||
151 | * |
||
152 | * @param string $table The name of the table. |
||
153 | * @param string|null $database |
||
154 | * |
||
155 | * @return Column[] |
||
156 | */ |
||
157 | 4442 | public function listTableColumns($table, $database = null) |
|
169 | |||
170 | /** |
||
171 | * Lists the indexes for a given table returning an array of Index instances. |
||
172 | * |
||
173 | * Keys of the portable indexes list are all lower-cased. |
||
174 | * |
||
175 | * @param string $table The name of the table. |
||
176 | * |
||
177 | * @return Index[] |
||
178 | */ |
||
179 | 4129 | public function listTableIndexes($table) |
|
187 | |||
188 | /** |
||
189 | * Returns true if all the given tables exist. |
||
190 | * |
||
191 | * @param string|string[] $tableNames |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 4922 | public function tablesExist($tableNames) |
|
201 | |||
202 | /** |
||
203 | * Returns a list of all tables in the current database. |
||
204 | * |
||
205 | * @return string[] |
||
206 | */ |
||
207 | 4762 | public function listTableNames() |
|
216 | |||
217 | /** |
||
218 | * Filters asset names if they are configured to return only a subset of all |
||
219 | * the found elements. |
||
220 | * |
||
221 | * @param mixed[] $assetNames |
||
222 | * |
||
223 | * @return mixed[] |
||
224 | */ |
||
225 | 4946 | protected function filterAssetNames($assetNames) |
|
234 | |||
235 | /** |
||
236 | * @deprecated Use Configuration::getSchemaAssetsFilter() instead |
||
237 | * |
||
238 | * @return string|null |
||
239 | */ |
||
240 | protected function getFilterSchemaAssetsExpression() |
||
244 | |||
245 | /** |
||
246 | * Lists the tables for this connection. |
||
247 | * |
||
248 | * @return Table[] |
||
249 | */ |
||
250 | 4206 | public function listTables() |
|
261 | |||
262 | /** |
||
263 | * @param string $tableName |
||
264 | * |
||
265 | * @return Table |
||
266 | */ |
||
267 | 4418 | public function listTableDetails($tableName) |
|
278 | |||
279 | /** |
||
280 | * Lists the views this connection has. |
||
281 | * |
||
282 | * @return View[] |
||
283 | */ |
||
284 | 3047 | public function listViews() |
|
292 | |||
293 | /** |
||
294 | * Lists the foreign keys for the given table. |
||
295 | * |
||
296 | * @param string $table The name of the table. |
||
297 | * @param string|null $database |
||
298 | * |
||
299 | * @return ForeignKeyConstraint[] |
||
300 | */ |
||
301 | 4310 | public function listTableForeignKeys($table, $database = null) |
|
311 | |||
312 | /* drop*() Methods */ |
||
313 | |||
314 | /** |
||
315 | * Drops a database. |
||
316 | * |
||
317 | * NOTE: You can not drop the database this SchemaManager is currently connected to. |
||
318 | * |
||
319 | * @param string $database The name of the database to drop. |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | 5138 | public function dropDatabase($database) |
|
327 | |||
328 | /** |
||
329 | * Drops the given table. |
||
330 | * |
||
331 | * @param string $tableName The name of the table to drop. |
||
332 | * |
||
333 | * @return void |
||
334 | */ |
||
335 | 5497 | public function dropTable($tableName) |
|
339 | |||
340 | /** |
||
341 | * Drops the index from the given table. |
||
342 | * |
||
343 | * @param Index|string $index The name of the index. |
||
344 | * @param Table|string $table The name of the table. |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | 3172 | public function dropIndex($index, $table) |
|
356 | |||
357 | /** |
||
358 | * Drops the constraint from the given table. |
||
359 | * |
||
360 | * @param Table|string $table The name of the table. |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | public function dropConstraint(Constraint $constraint, $table) |
||
368 | |||
369 | /** |
||
370 | * Drops a foreign key from a table. |
||
371 | * |
||
372 | * @param ForeignKeyConstraint|string $foreignKey The name of the foreign key. |
||
373 | * @param Table|string $table The name of the table with the foreign key. |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | public function dropForeignKey($foreignKey, $table) |
||
381 | |||
382 | /** |
||
383 | * Drops a sequence with a given name. |
||
384 | * |
||
385 | * @param string $name The name of the sequence to drop. |
||
386 | * |
||
387 | * @return void |
||
388 | */ |
||
389 | 1256 | public function dropSequence($name) |
|
393 | |||
394 | /** |
||
395 | * Drops a view. |
||
396 | * |
||
397 | * @param string $name The name of the view. |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | 3239 | public function dropView($name) |
|
405 | |||
406 | /* create*() Methods */ |
||
407 | |||
408 | /** |
||
409 | * Creates a new database. |
||
410 | * |
||
411 | * @param string $database The name of the database to create. |
||
412 | * |
||
413 | * @return void |
||
414 | */ |
||
415 | 5134 | public function createDatabase($database) |
|
419 | |||
420 | /** |
||
421 | * Creates a new table. |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | 5531 | public function createTable(Table $table) |
|
430 | |||
431 | /** |
||
432 | * Creates a new sequence. |
||
433 | * |
||
434 | * @param Sequence $sequence |
||
435 | * |
||
436 | * @return void |
||
437 | * |
||
438 | * @throws ConnectionException If something fails at database level. |
||
439 | */ |
||
440 | 1259 | public function createSequence($sequence) |
|
444 | |||
445 | /** |
||
446 | * Creates a constraint on a table. |
||
447 | * |
||
448 | * @param Table|string $table |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | 2 | public function createConstraint(Constraint $constraint, $table) |
|
456 | |||
457 | /** |
||
458 | * Creates a new index on a table. |
||
459 | * |
||
460 | * @param Table|string $table The name of the table on which the index is to be created. |
||
461 | * |
||
462 | * @return void |
||
463 | */ |
||
464 | 3172 | public function createIndex(Index $index, $table) |
|
468 | |||
469 | /** |
||
470 | * Creates a new foreign key. |
||
471 | * |
||
472 | * @param ForeignKeyConstraint $foreignKey The ForeignKey instance. |
||
473 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
474 | * |
||
475 | * @return void |
||
476 | */ |
||
477 | 3216 | public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) |
|
481 | |||
482 | /** |
||
483 | * Creates a new view. |
||
484 | * |
||
485 | * @return void |
||
486 | */ |
||
487 | 3239 | public function createView(View $view) |
|
491 | |||
492 | /* dropAndCreate*() Methods */ |
||
493 | |||
494 | /** |
||
495 | * Drops and creates a constraint. |
||
496 | * |
||
497 | * @see dropConstraint() |
||
498 | * @see createConstraint() |
||
499 | * |
||
500 | * @param Table|string $table |
||
501 | * |
||
502 | * @return void |
||
503 | */ |
||
504 | public function dropAndCreateConstraint(Constraint $constraint, $table) |
||
509 | |||
510 | /** |
||
511 | * Drops and creates a new index on a table. |
||
512 | * |
||
513 | * @param Table|string $table The name of the table on which the index is to be created. |
||
514 | * |
||
515 | * @return void |
||
516 | */ |
||
517 | 3172 | public function dropAndCreateIndex(Index $index, $table) |
|
522 | |||
523 | /** |
||
524 | * Drops and creates a new foreign key. |
||
525 | * |
||
526 | * @param ForeignKeyConstraint $foreignKey An associative array that defines properties of the foreign key to be created. |
||
527 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
528 | * |
||
529 | * @return void |
||
530 | */ |
||
531 | public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) |
||
536 | |||
537 | /** |
||
538 | * Drops and create a new sequence. |
||
539 | * |
||
540 | * @return void |
||
541 | * |
||
542 | * @throws ConnectionException If something fails at database level. |
||
543 | */ |
||
544 | 1256 | public function dropAndCreateSequence(Sequence $sequence) |
|
549 | |||
550 | /** |
||
551 | * Drops and creates a new table. |
||
552 | * |
||
553 | * @return void |
||
554 | */ |
||
555 | 5420 | public function dropAndCreateTable(Table $table) |
|
560 | |||
561 | /** |
||
562 | * Drops and creates a new database. |
||
563 | * |
||
564 | * @param string $database The name of the database to create. |
||
565 | * |
||
566 | * @return void |
||
567 | */ |
||
568 | 5275 | public function dropAndCreateDatabase($database) |
|
573 | |||
574 | /** |
||
575 | * Drops and creates a new view. |
||
576 | * |
||
577 | * @return void |
||
578 | */ |
||
579 | 3239 | public function dropAndCreateView(View $view) |
|
584 | |||
585 | /* alterTable() Methods */ |
||
586 | |||
587 | /** |
||
588 | * Alters an existing tables schema. |
||
589 | * |
||
590 | * @return void |
||
591 | */ |
||
592 | 3915 | public function alterTable(TableDiff $tableDiff) |
|
603 | |||
604 | /** |
||
605 | * Renames a given table to another name. |
||
606 | * |
||
607 | * @param string $name The current name of the table. |
||
608 | * @param string $newName The new name of the table. |
||
609 | * |
||
610 | * @return void |
||
611 | */ |
||
612 | 2 | public function renameTable($name, $newName) |
|
618 | |||
619 | /** |
||
620 | * Methods for filtering return values of list*() methods to convert |
||
621 | * the native DBMS data definition to a portable Doctrine definition |
||
622 | */ |
||
623 | |||
624 | /** |
||
625 | * @param mixed[] $databases |
||
626 | * |
||
627 | * @return string[] |
||
628 | */ |
||
629 | 3161 | protected function _getPortableDatabasesList($databases) |
|
644 | |||
645 | /** |
||
646 | * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition. |
||
647 | * |
||
648 | * @param mixed[][] $namespaces The list of namespace names in the native DBMS data definition. |
||
649 | * |
||
650 | * @return string[] |
||
651 | */ |
||
652 | 1225 | protected function getPortableNamespacesList(array $namespaces) |
|
662 | |||
663 | /** |
||
664 | * @param mixed $database |
||
665 | * |
||
666 | * @return mixed |
||
667 | */ |
||
668 | protected function _getPortableDatabaseDefinition($database) |
||
672 | |||
673 | /** |
||
674 | * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition. |
||
675 | * |
||
676 | * @param mixed[] $namespace The native DBMS namespace definition. |
||
677 | * |
||
678 | * @return mixed |
||
679 | */ |
||
680 | protected function getPortableNamespaceDefinition(array $namespace) |
||
684 | |||
685 | /** |
||
686 | * @param mixed[][] $functions |
||
687 | * |
||
688 | * @return mixed[][] |
||
689 | */ |
||
690 | protected function _getPortableFunctionsList($functions) |
||
705 | |||
706 | /** |
||
707 | * @param mixed[] $function |
||
708 | * |
||
709 | * @return mixed |
||
710 | */ |
||
711 | protected function _getPortableFunctionDefinition($function) |
||
715 | |||
716 | /** |
||
717 | * @param mixed[][] $triggers |
||
718 | * |
||
719 | * @return mixed[][] |
||
720 | */ |
||
721 | protected function _getPortableTriggersList($triggers) |
||
736 | |||
737 | /** |
||
738 | * @param mixed[] $trigger |
||
739 | * |
||
740 | * @return mixed |
||
741 | */ |
||
742 | protected function _getPortableTriggerDefinition($trigger) |
||
746 | |||
747 | /** |
||
748 | * @param mixed[][] $sequences |
||
749 | * |
||
750 | * @return Sequence[] |
||
751 | */ |
||
752 | 288 | protected function _getPortableSequencesList($sequences) |
|
762 | |||
763 | /** |
||
764 | * @param mixed[] $sequence |
||
765 | * |
||
766 | * @return Sequence |
||
767 | * |
||
768 | * @throws DBALException |
||
769 | */ |
||
770 | protected function _getPortableSequenceDefinition($sequence) |
||
774 | |||
775 | /** |
||
776 | * Independent of the database the keys of the column list result are lowercased. |
||
777 | * |
||
778 | * The name of the created column instance however is kept in its case. |
||
779 | * |
||
780 | * @param string $table The name of the table. |
||
781 | * @param string $database |
||
782 | * @param mixed[][] $tableColumns |
||
783 | * |
||
784 | * @return Column[] |
||
785 | */ |
||
786 | 4442 | protected function _getPortableTableColumnList($table, $database, $tableColumns) |
|
817 | |||
818 | /** |
||
819 | * Gets Table Column Definition. |
||
820 | * |
||
821 | * @param mixed[] $tableColumn |
||
822 | * |
||
823 | * @return Column |
||
824 | */ |
||
825 | abstract protected function _getPortableTableColumnDefinition($tableColumn); |
||
826 | |||
827 | /** |
||
828 | * Aggregates and groups the index results according to the required data result. |
||
829 | * |
||
830 | * @param mixed[][] $tableIndexRows |
||
831 | * @param string|null $tableName |
||
832 | * |
||
833 | * @return Index[] |
||
834 | */ |
||
835 | 4428 | protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null) |
|
896 | |||
897 | /** |
||
898 | * @param mixed[][] $tables |
||
899 | * |
||
900 | * @return string[] |
||
901 | */ |
||
902 | 4762 | protected function _getPortableTablesList($tables) |
|
917 | |||
918 | /** |
||
919 | * @param mixed $table |
||
920 | * |
||
921 | * @return string |
||
922 | */ |
||
923 | protected function _getPortableTableDefinition($table) |
||
927 | |||
928 | /** |
||
929 | * @param mixed[][] $users |
||
930 | * |
||
931 | * @return string[][] |
||
932 | */ |
||
933 | protected function _getPortableUsersList($users) |
||
948 | |||
949 | /** |
||
950 | * @param string[] $user |
||
951 | * |
||
952 | * @return string[] |
||
953 | */ |
||
954 | protected function _getPortableUserDefinition($user) |
||
958 | |||
959 | /** |
||
960 | * @param mixed[][] $views |
||
961 | * |
||
962 | * @return View[] |
||
963 | */ |
||
964 | 3047 | protected function _getPortableViewsList($views) |
|
980 | |||
981 | /** |
||
982 | * @param mixed[] $view |
||
983 | * |
||
984 | * @return View|false |
||
985 | */ |
||
986 | protected function _getPortableViewDefinition($view) |
||
990 | |||
991 | /** |
||
992 | * @param mixed[][] $tableForeignKeys |
||
993 | * |
||
994 | * @return ForeignKeyConstraint[] |
||
995 | */ |
||
996 | 1630 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
|
1006 | |||
1007 | /** |
||
1008 | * @param mixed $tableForeignKey |
||
1009 | * |
||
1010 | * @return ForeignKeyConstraint |
||
1011 | */ |
||
1012 | protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
||
1016 | |||
1017 | /** |
||
1018 | * @param string[]|string $sql |
||
1019 | * |
||
1020 | * @return void |
||
1021 | */ |
||
1022 | 5774 | protected function _execSql($sql) |
|
1028 | |||
1029 | /** |
||
1030 | * Creates a schema instance for the current database. |
||
1031 | * |
||
1032 | * @return Schema |
||
1033 | */ |
||
1034 | 4003 | public function createSchema() |
|
1052 | |||
1053 | /** |
||
1054 | * Creates the configuration for this schema. |
||
1055 | * |
||
1056 | * @return SchemaConfig |
||
1057 | */ |
||
1058 | 4276 | public function createSchemaConfig() |
|
1079 | |||
1080 | /** |
||
1081 | * The search path for namespaces in the currently connected database. |
||
1082 | * |
||
1083 | * The first entry is usually the default namespace in the Schema. All |
||
1084 | * further namespaces contain tables/sequences which can also be addressed |
||
1085 | * with a short, not full-qualified name. |
||
1086 | * |
||
1087 | * For databases that don't support subschema/namespaces this method |
||
1088 | * returns the name of the currently connected database. |
||
1089 | * |
||
1090 | * @return string[] |
||
1091 | */ |
||
1092 | 3426 | public function getSchemaSearchPaths() |
|
1096 | |||
1097 | /** |
||
1098 | * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns |
||
1099 | * the type given as default. |
||
1100 | * |
||
1101 | * @param string|null $comment |
||
1102 | * @param string $currentType |
||
1103 | * |
||
1104 | * @return string |
||
1105 | */ |
||
1106 | 4449 | public function extractDoctrineTypeFromComment($comment, $currentType) |
|
1114 | |||
1115 | /** |
||
1116 | * @param string|null $comment |
||
1117 | * @param string|null $type |
||
1118 | * |
||
1119 | * @return string|null |
||
1120 | */ |
||
1121 | 4405 | public function removeDoctrineTypeFromComment($comment, $type) |
|
1129 | } |
||
1130 |
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: