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 |
||
35 | abstract class AbstractSchemaManager |
||
36 | { |
||
37 | /** |
||
38 | * Holds instance of the Doctrine connection for this schema manager. |
||
39 | * |
||
40 | * @var Connection |
||
41 | */ |
||
42 | protected $_conn; |
||
43 | |||
44 | /** |
||
45 | * Holds instance of the database platform used for this schema manager. |
||
46 | * |
||
47 | * @var AbstractPlatform |
||
48 | */ |
||
49 | protected $_platform; |
||
50 | 6048 | ||
51 | /** |
||
52 | 6048 | * Constructor. Accepts the Connection instance to manage the schema for. |
|
53 | 6048 | */ |
|
54 | 6048 | public function __construct(Connection $conn, ?AbstractPlatform $platform = null) |
|
59 | |||
60 | /** |
||
61 | 3750 | * Returns the associated platform. |
|
62 | */ |
||
63 | 3750 | public function getDatabasePlatform() : AbstractPlatform |
|
67 | |||
68 | /** |
||
69 | * Tries any method on the schema manager. Normally a method throws an |
||
70 | * exception when your DBMS doesn't support it or if an error occurs. |
||
71 | * This method allows you to try and method on your SchemaManager |
||
72 | * instance and will return false if it does not work or is not supported. |
||
73 | * |
||
74 | * <code> |
||
75 | * $result = $sm->tryMethod('dropView', 'view_name'); |
||
76 | * </code> |
||
77 | * |
||
78 | 5861 | * @return mixed |
|
79 | */ |
||
80 | 5861 | public function tryMethod() |
|
96 | |||
97 | /** |
||
98 | * Lists the available databases for this connection. |
||
99 | * |
||
100 | 3331 | * @return array<int, string> |
|
|
|||
101 | */ |
||
102 | 3331 | public function listDatabases() : array |
|
110 | |||
111 | /** |
||
112 | * Returns a list of all namespaces in the current database. |
||
113 | * |
||
114 | 1242 | * @return array<int, string> |
|
115 | */ |
||
116 | 1242 | public function listNamespaceNames() : array |
|
124 | |||
125 | /** |
||
126 | * Lists the available sequences for this connection. |
||
127 | * |
||
128 | * @return array<int, Sequence> |
||
129 | */ |
||
130 | 1278 | public function listSequences(?string $database = null) : array |
|
143 | |||
144 | /** |
||
145 | * Lists the columns for a given table. |
||
146 | * |
||
147 | * In contrast to other libraries and to the old version of Doctrine, |
||
148 | * this column definition does try to contain the 'primary' field for |
||
149 | * the reason that it is not portable across different RDBMS. Use |
||
150 | * {@see listTableIndexes($tableName)} to retrieve the primary key |
||
151 | * of a table. Where a RDBMS specifies more details, these are held |
||
152 | * in the platformDetails array. |
||
153 | * |
||
154 | * @return array<string, Column> |
||
155 | */ |
||
156 | public function listTableColumns(string $table, ?string $database = null) : array |
||
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 | * @return array<string, Index> |
||
176 | */ |
||
177 | public function listTableIndexes(string $table) : array |
||
185 | 4253 | ||
186 | /** |
||
187 | * Returns true if all the given tables exist. |
||
188 | * |
||
189 | * @param array<int, string> $tableNames |
||
190 | */ |
||
191 | public function tablesExist(array $tableNames) : bool |
||
197 | 5063 | ||
198 | public function tableExists(string $tableName) : bool |
||
202 | |||
203 | /** |
||
204 | * Returns a list of all tables in the current database. |
||
205 | * |
||
206 | * @return array<int, string> |
||
207 | */ |
||
208 | public function listTableNames() : array |
||
217 | |||
218 | /** |
||
219 | * Filters asset names if they are configured to return only a subset of all |
||
220 | * the found elements. |
||
221 | * |
||
222 | * @param array<int, mixed> $assetNames |
||
223 | * |
||
224 | * @return array<int, mixed> |
||
225 | */ |
||
226 | protected function filterAssetNames(array $assetNames) : array |
||
235 | |||
236 | /** |
||
237 | * Lists the tables for this connection. |
||
238 | * |
||
239 | * @return array<int, Table> |
||
240 | */ |
||
241 | public function listTables() : array |
||
252 | 4300 | ||
253 | public function listTableDetails(string $tableName) : Table |
||
266 | |||
267 | /** |
||
268 | * Lists the views this connection has. |
||
269 | 4547 | * |
|
270 | * @return array<string, View> |
||
271 | 4547 | */ |
|
272 | 4547 | public function listViews() : array |
|
284 | |||
285 | /** |
||
286 | 3079 | * Lists the foreign keys for the given table. |
|
287 | * |
||
288 | 3079 | * @return array<int|string, ForeignKeyConstraint> |
|
289 | 3079 | */ |
|
290 | 3079 | public function listTableForeignKeys(string $table, ?string $database = null) : array |
|
300 | |||
301 | /* drop*() Methods */ |
||
302 | |||
303 | 4436 | /** |
|
304 | * Drops a database. |
||
305 | 4436 | * |
|
306 | 4436 | * NOTE: You can not drop the database this SchemaManager is currently connected to. |
|
307 | */ |
||
308 | 4436 | public function dropDatabase(string $database) : void |
|
312 | |||
313 | /** |
||
314 | * Drops the given table. |
||
315 | */ |
||
316 | public function dropTable(string $tableName) : void |
||
320 | |||
321 | /** |
||
322 | * Drops the index from the given table. |
||
323 | * |
||
324 | * @param Index|string $index The name of the index. |
||
325 | 5271 | * @param Table|string $table The name of the table. |
|
326 | */ |
||
327 | 5271 | public function dropIndex($index, $table) : void |
|
335 | |||
336 | /** |
||
337 | 5640 | * Drops the constraint from the given table. |
|
338 | * |
||
339 | 5640 | * @param Table|string $table The name of the table. |
|
340 | 5519 | */ |
|
341 | public function dropConstraint(Constraint $constraint, $table) : void |
||
345 | |||
346 | /** |
||
347 | * Drops a foreign key from a table. |
||
348 | * |
||
349 | * @param ForeignKeyConstraint|string $foreignKey The name of the foreign key. |
||
350 | 3204 | * @param Table|string $table The name of the table with the foreign key. |
|
351 | */ |
||
352 | 3204 | public function dropForeignKey($foreignKey, $table) : void |
|
356 | 3204 | ||
357 | 3204 | /** |
|
358 | * Drops a sequence with a given name. |
||
359 | */ |
||
360 | public function dropSequence(string $name) : void |
||
364 | |||
365 | /** |
||
366 | * Drops a view. |
||
367 | */ |
||
368 | public function dropView(string $name) : void |
||
372 | |||
373 | /* create*() Methods */ |
||
374 | |||
375 | /** |
||
376 | * Creates a new database. |
||
377 | */ |
||
378 | public function createDatabase(string $database) : void |
||
382 | |||
383 | /** |
||
384 | * Creates a new table. |
||
385 | */ |
||
386 | public function createTable(Table $table) : void |
||
391 | 1273 | ||
392 | /** |
||
393 | 1273 | * Creates a new sequence. |
|
394 | * |
||
395 | * @throws ConnectionException If something fails at database level. |
||
396 | */ |
||
397 | public function createSequence(Sequence $sequence) : void |
||
401 | |||
402 | /** |
||
403 | 3271 | * Creates a constraint on a table. |
|
404 | * |
||
405 | 3271 | * @param Table|string $table |
|
406 | */ |
||
407 | public function createConstraint(Constraint $constraint, $table) : void |
||
411 | |||
412 | /** |
||
413 | * Creates a new index on a table. |
||
414 | * |
||
415 | * @param Table|string $table The name of the table on which the index is to be created. |
||
416 | */ |
||
417 | 5267 | public function createIndex(Index $index, $table) : void |
|
421 | |||
422 | /** |
||
423 | * Creates a new foreign key. |
||
424 | * |
||
425 | * @param ForeignKeyConstraint $foreignKey The ForeignKey instance. |
||
426 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
427 | 5674 | */ |
|
428 | public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) : void |
||
432 | |||
433 | /** |
||
434 | * Creates a new view. |
||
435 | */ |
||
436 | public function createView(View $view) : void |
||
440 | |||
441 | /* dropAndCreate*() Methods */ |
||
442 | 1276 | ||
443 | /** |
||
444 | 1276 | * Drops and creates a constraint. |
|
445 | 1276 | * |
|
446 | * @see dropConstraint() |
||
447 | * @see createConstraint() |
||
448 | * |
||
449 | * @param Table|string $table |
||
450 | */ |
||
451 | public function dropAndCreateConstraint(Constraint $constraint, $table) : void |
||
456 | 2 | ||
457 | 2 | /** |
|
458 | * Drops and 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 | public function dropAndCreateIndex(Index $index, $table) : void |
||
467 | |||
468 | 3204 | /** |
|
469 | 3204 | * Drops and creates a new foreign key. |
|
470 | * |
||
471 | * @param ForeignKeyConstraint $foreignKey An associative array that defines properties of the foreign key to be created. |
||
472 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
473 | */ |
||
474 | public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) : void |
||
479 | 3245 | ||
480 | /** |
||
481 | 3245 | * Drops and create a new sequence. |
|
482 | 3245 | * |
|
483 | * @throws ConnectionException If something fails at database level. |
||
484 | */ |
||
485 | public function dropAndCreateSequence(Sequence $sequence) : void |
||
490 | |||
491 | 3271 | /** |
|
492 | 3271 | * Drops and creates a new table. |
|
493 | */ |
||
494 | public function dropAndCreateTable(Table $table) : void |
||
499 | |||
500 | /** |
||
501 | * Drops and creates a new database. |
||
502 | */ |
||
503 | public function dropAndCreateDatabase(string $database) : void |
||
508 | |||
509 | /** |
||
510 | * Drops and creates a new view. |
||
511 | */ |
||
512 | public function dropAndCreateView(View $view) : void |
||
517 | |||
518 | /* alterTable() Methods */ |
||
519 | 3204 | ||
520 | /** |
||
521 | 3204 | * Alters an existing tables schema. |
|
522 | 3204 | */ |
|
523 | 3204 | public function alterTable(TableDiff $tableDiff) : void |
|
535 | |||
536 | /** |
||
537 | * Renames a given table to another name. |
||
538 | */ |
||
539 | public function renameTable(string $name, string $newName) : void |
||
545 | |||
546 | 1273 | /** |
|
547 | * Methods for filtering return values of list*() methods to convert |
||
548 | 1273 | * the native DBMS data definition to a portable Doctrine definition |
|
549 | 1273 | */ |
|
550 | 1273 | ||
551 | /** |
||
552 | * @param array<int, mixed> $databases |
||
553 | * |
||
554 | * @return array<int, string> |
||
555 | */ |
||
556 | protected function _getPortableDatabasesList(array $databases) : array |
||
571 | |||
572 | 5411 | /** |
|
573 | 5411 | * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition. |
|
574 | 5411 | * |
|
575 | * @param array<int, array<int, mixed>> $namespaces The list of namespace names in the native DBMS data definition. |
||
576 | * |
||
577 | * @return array<int, string> |
||
578 | */ |
||
579 | protected function getPortableNamespacesList(array $namespaces) : array |
||
589 | |||
590 | /** |
||
591 | * @param array<string, string> $database |
||
592 | */ |
||
593 | protected function _getPortableDatabaseDefinition(array $database) : string |
||
597 | 3947 | ||
598 | 1515 | /** |
|
599 | * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition. |
||
600 | * |
||
601 | 3947 | * @param array<string|int, mixed> $namespace The native DBMS namespace definition. |
|
602 | 3947 | */ |
|
603 | protected function getPortableNamespaceDefinition(array $namespace) : string |
||
607 | |||
608 | /** |
||
609 | * @param array<int, array<string, mixed>> $sequences |
||
610 | * |
||
611 | * @return array<int, Sequence> |
||
612 | */ |
||
613 | protected function _getPortableSequencesList(array $sequences) : array |
||
623 | |||
624 | /** |
||
625 | * @param array<string, mixed> $sequence |
||
626 | * |
||
627 | * @throws DBALException |
||
628 | */ |
||
629 | protected function _getPortableSequenceDefinition(array $sequence) : Sequence |
||
633 | 3189 | ||
634 | 3189 | /** |
|
635 | 3189 | * Independent of the database the keys of the column list result are lowercased. |
|
636 | * |
||
637 | 3189 | * The name of the created column instance however is kept in its case. |
|
638 | * |
||
639 | * @param array<int, array<string, mixed>> $tableColumns |
||
640 | * |
||
641 | 3189 | * @return array<string, Column> |
|
642 | */ |
||
643 | protected function _getPortableTableColumnList(string $table, string $database, array $tableColumns) : array |
||
674 | |||
675 | /** |
||
676 | * Gets Table Column Definition. |
||
677 | * |
||
678 | * @param array<string, mixed> $tableColumn |
||
679 | */ |
||
680 | abstract protected function _getPortableTableColumnDefinition(array $tableColumn) : Column; |
||
681 | |||
682 | /** |
||
683 | * Aggregates and groups the index results according to the required data result. |
||
684 | * |
||
685 | * @param array<int, array<string, mixed>> $tableIndexRows |
||
686 | * |
||
687 | * @return array<string, Index> |
||
688 | */ |
||
689 | protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array |
||
750 | |||
751 | /** |
||
752 | * @param array<int, array<string, mixed>> $tables |
||
753 | * |
||
754 | * @return array<int, string> |
||
755 | */ |
||
756 | protected function _getPortableTablesList(array $tables) : array |
||
771 | |||
772 | /** |
||
773 | * @param array<string, string> $table |
||
774 | */ |
||
775 | protected function _getPortableTableDefinition(array $table) : string |
||
779 | |||
780 | /** |
||
781 | * @param array<int, array<string, mixed>> $users |
||
782 | * |
||
783 | * @return array<int, array<string, mixed>> |
||
784 | */ |
||
785 | protected function _getPortableUsersList(array $users) : array |
||
800 | |||
801 | 4571 | /** |
|
802 | 3304 | * @param array<string, mixed> $user |
|
803 | 3304 | * |
|
804 | * @return array<string, mixed> |
||
805 | 3304 | */ |
|
806 | 3304 | protected function _getPortableUserDefinition(array $user) : array |
|
810 | 4571 | ||
811 | /** |
||
812 | * @param array<int, array<string, mixed>> $views |
||
813 | 4571 | * |
|
814 | * @return array<string, View> |
||
815 | */ |
||
816 | protected function _getPortableViewsList(array $views) : array |
||
827 | |||
828 | /** |
||
829 | * @param array<string, mixed> $view |
||
830 | */ |
||
831 | protected function _getPortableViewDefinition(array $view) : View |
||
835 | |||
836 | /** |
||
837 | * @param array<int|string, array<string, mixed>> $tableForeignKeys |
||
838 | * |
||
839 | * @return array<int, ForeignKeyConstraint> |
||
840 | */ |
||
841 | 4557 | protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array |
|
851 | 4382 | ||
852 | /** |
||
853 | 4382 | * @param array<string, mixed> $tableForeignKey |
|
854 | */ |
||
855 | protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey) : ForeignKeyConstraint |
||
859 | |||
860 | 4382 | /** |
|
861 | 4382 | * @param array<int, string>|string $sql |
|
862 | */ |
||
863 | 4382 | protected function _execSql($sql) : void |
|
869 | |||
870 | 4382 | /** |
|
871 | 4382 | * Creates a schema instance for the current database. |
|
872 | */ |
||
873 | public function createSchema() : Schema |
||
891 | |||
892 | /** |
||
893 | 4382 | * Creates the configuration for this schema. |
|
894 | */ |
||
895 | public function createSchemaConfig() : SchemaConfig |
||
916 | |||
917 | /** |
||
918 | 4898 | * The search path for namespaces in the currently connected database. |
|
919 | * |
||
920 | * The first entry is usually the default namespace in the Schema. All |
||
921 | 4901 | * further namespaces contain tables/sequences which can also be addressed |
|
922 | * with a short, not full-qualified name. |
||
923 | * |
||
924 | * For databases that don't support subschema/namespaces this method |
||
925 | * returns the name of the currently connected database. |
||
926 | * |
||
927 | * @return array<int, string> |
||
928 | */ |
||
929 | public function getSchemaSearchPaths() : array |
||
939 | |||
940 | /** |
||
941 | * Given a table comment this method tries to extract a type hint for Doctrine Type. If the type hint is found, |
||
942 | * it's removed from the comment. |
||
943 | * |
||
944 | * @return string|null The extracted Doctrine type or NULL of the type hint was not found. |
||
945 | */ |
||
946 | final protected function extractDoctrineTypeFromComment(?string &$comment) : ?string |
||
956 | |||
957 | /** |
||
958 | * @throws DatabaseRequired |
||
959 | */ |
||
960 | private function ensureDatabase(?string $database, string $methodName) : string |
||
968 | } |
||
969 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.