Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Configuration |
||
48 | { |
||
49 | /** |
||
50 | * Configure versions to be organized by year. |
||
51 | */ |
||
52 | const VERSIONS_ORGANIZATION_BY_YEAR = 'year'; |
||
53 | |||
54 | /** |
||
55 | * Configure versions to be organized by year and month. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | const VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH = 'year_and_month'; |
||
60 | |||
61 | /** |
||
62 | * The date format for new version numbers |
||
63 | */ |
||
64 | const VERSION_FORMAT = 'YmdHis'; |
||
65 | |||
66 | /** |
||
67 | * Name of this set of migrations |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $name; |
||
72 | |||
73 | /** |
||
74 | * Flag for whether or not the migration table has been created |
||
75 | * |
||
76 | * @var boolean |
||
77 | */ |
||
78 | private $migrationTableCreated = false; |
||
79 | |||
80 | /** |
||
81 | * Connection instance to use for migrations |
||
82 | * |
||
83 | * @var Connection |
||
84 | */ |
||
85 | private $connection; |
||
86 | |||
87 | /** |
||
88 | * OutputWriter instance for writing output during migrations |
||
89 | * |
||
90 | * @var OutputWriter |
||
91 | */ |
||
92 | private $outputWriter; |
||
93 | |||
94 | /** |
||
95 | * The migration finder implementation -- used to load migrations from a |
||
96 | * directory. |
||
97 | * |
||
98 | * @var MigrationFinderInterface |
||
99 | */ |
||
100 | private $migrationFinder; |
||
101 | |||
102 | /** |
||
103 | * @var QueryWriter |
||
104 | */ |
||
105 | private $queryWriter; |
||
106 | |||
107 | /** |
||
108 | * The migration table name to track versions in |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | private $migrationsTableName = 'doctrine_migration_versions'; |
||
113 | |||
114 | /** |
||
115 | * The migration column name to track versions in |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | private $migrationsColumnName = 'version'; |
||
120 | |||
121 | /** |
||
122 | * The path to a directory where new migration classes will be written |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | private $migrationsDirectory; |
||
127 | |||
128 | /** |
||
129 | * Namespace the migration classes live in |
||
130 | * |
||
131 | * @var string |
||
132 | */ |
||
133 | private $migrationsNamespace; |
||
134 | |||
135 | /** |
||
136 | * Array of the registered migrations |
||
137 | * |
||
138 | * @var Version[] |
||
139 | */ |
||
140 | private $migrations = []; |
||
141 | |||
142 | /** |
||
143 | * Versions are organized by year. |
||
144 | * |
||
145 | * @var boolean |
||
146 | */ |
||
147 | private $migrationsAreOrganizedByYear = false; |
||
148 | |||
149 | /** |
||
150 | * Versions are organized by year and month. |
||
151 | * |
||
152 | * @var boolean |
||
153 | */ |
||
154 | private $migrationsAreOrganizedByYearAndMonth = false; |
||
155 | |||
156 | /** |
||
157 | * The custom template path to be used in generate command |
||
158 | * |
||
159 | * @var string |
||
160 | */ |
||
161 | private $customTemplate; |
||
162 | |||
163 | /** |
||
164 | * Construct a migration configuration object. |
||
165 | * |
||
166 | * @param Connection $connection A Connection instance |
||
167 | * @param OutputWriter $outputWriter A OutputWriter instance |
||
168 | * @param MigrationFinderInterface $finder Migration files finder |
||
169 | * @param QueryWriter|null $queryWriter |
||
170 | */ |
||
171 | 275 | public function __construct( |
|
182 | |||
183 | /** |
||
184 | * @return bool |
||
185 | */ |
||
186 | 22 | public function areMigrationsOrganizedByYear() |
|
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | 22 | public function areMigrationsOrganizedByYearAndMonth() |
|
198 | |||
199 | /** |
||
200 | * Validation that this instance has all the required properties configured |
||
201 | * |
||
202 | * @throws MigrationException |
||
203 | */ |
||
204 | 146 | public function validate() |
|
213 | |||
214 | /** |
||
215 | * Set the name of this set of migrations |
||
216 | * |
||
217 | * @param string $name The name of this set of migrations |
||
218 | */ |
||
219 | 63 | public function setName($name) |
|
223 | |||
224 | /** |
||
225 | * Returns the name of this set of migrations |
||
226 | * |
||
227 | * @return string $name The name of this set of migrations |
||
228 | */ |
||
229 | 16 | public function getName() |
|
233 | |||
234 | /** |
||
235 | * Sets the output writer. |
||
236 | * |
||
237 | * @param OutputWriter $outputWriter |
||
238 | */ |
||
239 | 3 | public function setOutputWriter(OutputWriter $outputWriter) |
|
243 | |||
244 | /** |
||
245 | * Returns the OutputWriter instance |
||
246 | * |
||
247 | * @return OutputWriter $outputWriter The OutputWriter instance |
||
248 | */ |
||
249 | 117 | public function getOutputWriter() |
|
253 | |||
254 | /** |
||
255 | * Returns a timestamp version as a formatted date |
||
256 | * |
||
257 | * @param string $version |
||
258 | * |
||
259 | * @return string The formatted version |
||
260 | * @deprecated |
||
261 | */ |
||
262 | 14 | public function formatVersion($version) |
|
266 | |||
267 | /** |
||
268 | * Returns the datetime of a migration |
||
269 | * |
||
270 | * @param $version |
||
271 | * @return string |
||
272 | */ |
||
273 | 22 | public function getDateTime($version) |
|
284 | |||
285 | /** |
||
286 | * Returns the Connection instance |
||
287 | * |
||
288 | * @return Connection $connection The Connection instance |
||
289 | */ |
||
290 | 120 | public function getConnection() |
|
294 | |||
295 | /** |
||
296 | * Set the migration table name |
||
297 | * |
||
298 | * @param string $tableName The migration table name |
||
299 | */ |
||
300 | 85 | public function setMigrationsTableName($tableName) |
|
304 | |||
305 | /** |
||
306 | * Returns the migration table name |
||
307 | * |
||
308 | * @return string $migrationsTableName The migration table name |
||
309 | */ |
||
310 | 60 | public function getMigrationsTableName() |
|
314 | |||
315 | /** |
||
316 | * Set the migration column name |
||
317 | * |
||
318 | * @param string $columnName The migration column name |
||
319 | */ |
||
320 | 56 | public function setMigrationsColumnName($columnName) |
|
324 | |||
325 | /** |
||
326 | * Returns the migration column name |
||
327 | * |
||
328 | * @return string $migrationsColumnName The migration column name |
||
329 | */ |
||
330 | 54 | public function getMigrationsColumnName() |
|
334 | |||
335 | /** |
||
336 | * Set the new migrations directory where new migration classes are generated |
||
337 | * |
||
338 | * @param string $migrationsDirectory The new migrations directory |
||
339 | */ |
||
340 | 183 | public function setMigrationsDirectory($migrationsDirectory) |
|
344 | |||
345 | /** |
||
346 | * Returns the new migrations directory where new migration classes are generated |
||
347 | * |
||
348 | * @return string $migrationsDirectory The new migrations directory |
||
349 | */ |
||
350 | 65 | public function getMigrationsDirectory() |
|
354 | |||
355 | /** |
||
356 | * Set the migrations namespace |
||
357 | * |
||
358 | * @param string $migrationsNamespace The migrations namespace |
||
359 | */ |
||
360 | 195 | public function setMigrationsNamespace($migrationsNamespace) |
|
364 | |||
365 | /** |
||
366 | * Returns the migrations namespace |
||
367 | * |
||
368 | * @return string $migrationsNamespace The migrations namespace |
||
369 | */ |
||
370 | 92 | public function getMigrationsNamespace() |
|
374 | |||
375 | /** |
||
376 | * Returns the custom template path |
||
377 | * |
||
378 | * @return string $customTemplate The custom template path |
||
379 | */ |
||
380 | 2 | public function getCustomTemplate() : ?string |
|
384 | |||
385 | /** |
||
386 | * Set the custom template path |
||
387 | * |
||
388 | * @param string $customTemplate The custom template path |
||
389 | */ |
||
390 | 3 | public function setCustomTemplate(string $customTemplate) : void |
|
394 | |||
395 | /** |
||
396 | * Set the implementation of the migration finder. |
||
397 | * |
||
398 | * @param MigrationFinderInterface $finder The new migration finder |
||
399 | * @throws MigrationException |
||
400 | */ |
||
401 | 8 | public function setMigrationsFinder(MigrationFinderInterface $finder) |
|
413 | |||
414 | /** |
||
415 | * Register migrations from a given directory. Recursively finds all files |
||
416 | * with the pattern VersionYYYYMMDDHHMMSS.php as the filename and registers |
||
417 | * them as migrations. |
||
418 | * |
||
419 | * @param string $path The root directory to where some migration classes live. |
||
420 | * |
||
421 | * @return Version[] The array of migrations registered. |
||
422 | */ |
||
423 | 103 | public function registerMigrationsFromDirectory($path) |
|
429 | |||
430 | /** |
||
431 | * Register a single migration version to be executed by a AbstractMigration |
||
432 | * class. |
||
433 | * |
||
434 | * @param string $version The version of the migration in the format YYYYMMDDHHMMSS. |
||
435 | * @param string $class The migration class to execute for the version. |
||
436 | * |
||
437 | * @return Version |
||
438 | * |
||
439 | * @throws MigrationException |
||
440 | */ |
||
441 | 69 | public function registerMigration($version, $class) |
|
456 | |||
457 | /** |
||
458 | * Register an array of migrations. Each key of the array is the version and |
||
459 | * the value is the migration class name. |
||
460 | * |
||
461 | * |
||
462 | * @param array $migrations |
||
463 | * |
||
464 | * @return Version[] |
||
465 | */ |
||
466 | 91 | public function registerMigrations(array $migrations) |
|
475 | |||
476 | /** |
||
477 | * Get the array of registered migration versions. |
||
478 | * |
||
479 | * @return Version[] $migrations |
||
480 | */ |
||
481 | 35 | public function getMigrations() |
|
485 | |||
486 | /** |
||
487 | * Returns the Version instance for a given version in the format YYYYMMDDHHMMSS. |
||
488 | * |
||
489 | * @param string $version The version string in the format YYYYMMDDHHMMSS. |
||
490 | * |
||
491 | * @return Version |
||
492 | * |
||
493 | * @throws MigrationException Throws exception if migration version does not exist. |
||
494 | */ |
||
495 | 20 | public function getVersion($version) |
|
507 | |||
508 | /** |
||
509 | * Check if a version exists. |
||
510 | * |
||
511 | * @param string $version |
||
512 | * |
||
513 | * @return boolean |
||
514 | */ |
||
515 | 22 | public function hasVersion($version) |
|
523 | |||
524 | /** |
||
525 | * Check if a version has been migrated or not yet |
||
526 | * |
||
527 | * @param Version $version |
||
528 | * |
||
529 | * @return boolean |
||
530 | */ |
||
531 | 21 | public function hasVersionMigrated(Version $version) |
|
543 | |||
544 | /** |
||
545 | * Returns all migrated versions from the versions table, in an array. |
||
546 | * |
||
547 | * @return Version[] |
||
548 | */ |
||
549 | 41 | public function getMigratedVersions() |
|
550 | { |
||
551 | 41 | $this->connect(); |
|
552 | 41 | $this->createMigrationTable(); |
|
553 | |||
554 | 41 | $ret = $this->connection->fetchAll("SELECT " . $this->migrationsColumnName . " FROM " . $this->migrationsTableName); |
|
555 | |||
556 | 41 | return array_map('current', $ret); |
|
557 | } |
||
558 | |||
559 | /** |
||
560 | * Returns an array of available migration version numbers. |
||
561 | * |
||
562 | * @return array |
||
563 | */ |
||
564 | 16 | public function getAvailableVersions() |
|
578 | |||
579 | /** |
||
580 | * Returns the current migrated version from the versions table. |
||
581 | * |
||
582 | * @return string |
||
583 | */ |
||
584 | 43 | public function getCurrentVersion() |
|
615 | |||
616 | /** |
||
617 | * Returns the version prior to the current version. |
||
618 | * |
||
619 | * @return string|null A version string, or null if the current version is |
||
620 | * the first. |
||
621 | */ |
||
622 | 10 | public function getPrevVersion() |
|
626 | |||
627 | /** |
||
628 | * Returns the version following the current version. |
||
629 | * |
||
630 | * @return string|null A version string, or null if the current version is |
||
631 | * the latest. |
||
632 | */ |
||
633 | 10 | public function getNextVersion() |
|
637 | |||
638 | /** |
||
639 | * Returns the version with the specified offset to the specified version. |
||
640 | * |
||
641 | * @return string|null A version string, or null if the specified version |
||
642 | * is unknown or the specified delta is not within the |
||
643 | * list of available versions. |
||
644 | */ |
||
645 | 16 | public function getRelativeVersion($version, $delta) |
|
661 | |||
662 | /** |
||
663 | * Returns the version with the specified to the current version. |
||
664 | * |
||
665 | * @return string|null A version string, or null if the specified delta is |
||
666 | * not within the list of available versions. |
||
667 | */ |
||
668 | 1 | public function getDeltaVersion($delta) |
|
683 | |||
684 | /** |
||
685 | * Returns the version number from an alias. |
||
686 | * |
||
687 | * Supported aliases are: |
||
688 | * - first: The very first version before any migrations have been run. |
||
689 | * - current: The current version. |
||
690 | * - prev: The version prior to the current version. |
||
691 | * - next: The version following the current version. |
||
692 | * - latest: The latest available version. |
||
693 | * |
||
694 | * If an existing version number is specified, it is returned verbatimly. |
||
695 | * |
||
696 | * @return string|null A version number, or null if the specified alias |
||
697 | * does not map to an existing version, e.g. if "next" |
||
698 | * is passed but the current version is already the |
||
699 | * latest. |
||
700 | */ |
||
701 | 9 | public function resolveVersionAlias($alias) |
|
724 | |||
725 | /** |
||
726 | * Returns the total number of executed migration versions |
||
727 | * |
||
728 | * @return integer |
||
729 | */ |
||
730 | 1 | public function getNumberOfExecutedMigrations() |
|
731 | { |
||
732 | 1 | $this->connect(); |
|
733 | 1 | $this->createMigrationTable(); |
|
734 | |||
735 | 1 | $result = $this->connection->fetchColumn("SELECT COUNT(" . $this->migrationsColumnName . ") FROM " . $this->migrationsTableName); |
|
736 | |||
737 | 1 | return $result !== false ? $result : 0; |
|
738 | } |
||
739 | |||
740 | /** |
||
741 | * Returns the total number of available migration versions |
||
742 | * |
||
743 | * @return integer |
||
744 | */ |
||
745 | 5 | public function getNumberOfAvailableMigrations() |
|
753 | |||
754 | /** |
||
755 | * Returns the latest available migration version. |
||
756 | * |
||
757 | * @return string The version string in the format YYYYMMDDHHMMSS. |
||
758 | */ |
||
759 | 28 | public function getLatestVersion() |
|
770 | |||
771 | /** |
||
772 | * Create the migration table to track migrations with. |
||
773 | * |
||
774 | * @return boolean Whether or not the table was created. |
||
775 | */ |
||
776 | 64 | public function createMigrationTable() |
|
802 | |||
803 | /** |
||
804 | * Returns the array of migrations to executed based on the given direction |
||
805 | * and target version number. |
||
806 | * |
||
807 | * @param string $direction The direction we are migrating. |
||
808 | * @param string $to The version to migrate to. |
||
809 | * |
||
810 | * @return Version[] $migrations The array of migrations we can execute. |
||
811 | */ |
||
812 | 39 | public function getMigrationsToExecute($direction, $to) |
|
839 | |||
840 | /** |
||
841 | * Use the connection's event manager to emit an event. |
||
842 | * |
||
843 | * @param string $eventName The event to emit. |
||
844 | * @param $args The event args instance to emit. |
||
845 | */ |
||
846 | 45 | public function dispatchEvent($eventName, EventArgs $args = null) |
|
850 | |||
851 | /** |
||
852 | * Find all the migrations in a given directory. |
||
853 | * |
||
854 | * @param string $path the directory to search. |
||
855 | * @return array |
||
856 | */ |
||
857 | 87 | protected function findMigrations($path) |
|
861 | |||
862 | /** |
||
863 | * @param bool $migrationsAreOrganizedByYear |
||
864 | * @throws MigrationException |
||
865 | */ |
||
866 | 9 | public function setMigrationsAreOrganizedByYear($migrationsAreOrganizedByYear = true) |
|
872 | |||
873 | /** |
||
874 | * @param bool $migrationsAreOrganizedByYearAndMonth |
||
875 | * @throws MigrationException |
||
876 | */ |
||
877 | 10 | public function setMigrationsAreOrganizedByYearAndMonth($migrationsAreOrganizedByYearAndMonth = true) |
|
884 | |||
885 | /** |
||
886 | * Generate a new migration version. A version is (usually) a datetime string. |
||
887 | * |
||
888 | * @param \DateTimeInterface|null $now Defaults to the current time in UTC |
||
889 | * @return string The newly generated version |
||
890 | */ |
||
891 | 9 | public function generateVersionNumber(\DateTimeInterface $now = null) |
|
897 | |||
898 | /** |
||
899 | * Explicitely opens the database connection. This is done to play nice |
||
900 | * with DBAL's MasterSlaveConnection. Which, in some cases, connects to a |
||
901 | * follower when fetching the executed migrations. If a follower is lagging |
||
902 | * significantly behind that means the migrations system may see unexecuted |
||
903 | * migrations that were actually executed earlier. |
||
904 | * |
||
905 | * @return bool The same value returned from the `connect` method |
||
906 | */ |
||
907 | 64 | protected function connect() |
|
915 | |||
916 | /** |
||
917 | * @throws MigrationException |
||
918 | */ |
||
919 | 19 | private function ensureOrganizeMigrationsIsCompatibleWithFinder() |
|
928 | |||
929 | /** |
||
930 | * Check if we should execute a migration for a given direction and target |
||
931 | * migration version. |
||
932 | * |
||
933 | * @param string $direction The direction we are migrating. |
||
934 | * @param Version $version The Version instance to check. |
||
935 | * @param string $to The version we are migrating to. |
||
936 | * @param array $migrated Migrated versions array. |
||
937 | * |
||
938 | * @return boolean |
||
939 | */ |
||
940 | 31 | private function shouldExecuteMigration($direction, Version $version, $to, $migrated) |
|
941 | { |
||
942 | 31 | if ($direction === Version::DIRECTION_DOWN) { |
|
943 | 7 | if ( ! in_array($version->getVersion(), $migrated)) { |
|
944 | 4 | return false; |
|
945 | } |
||
946 | |||
947 | 5 | return $version->getVersion() > $to; |
|
948 | } |
||
949 | |||
950 | 29 | if ($direction === Version::DIRECTION_UP) { |
|
951 | 29 | if (in_array($version->getVersion(), $migrated)) { |
|
952 | 8 | return false; |
|
953 | } |
||
954 | |||
955 | 28 | return $version->getVersion() <= $to; |
|
956 | } |
||
957 | } |
||
958 | |||
959 | /** |
||
960 | * @param string $class |
||
961 | */ |
||
962 | 69 | private function ensureMigrationClassExists($class) |
|
968 | |||
969 | 8 | public function getQueryWriter() : QueryWriter |
|
981 | } |
||
982 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.