Complex classes like MigrationsLoader 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 MigrationsLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 26 | class MigrationsLoader  | 
            ||
| 27 | { | 
            ||
| 28 | const DEFAULT_MIGRATIONS_PATH = 'Migrations/Schema';  | 
            ||
| 29 | |||
| 30 | /**  | 
            ||
| 31 | * @var KernelInterface  | 
            ||
| 32 | *  | 
            ||
| 33 | */  | 
            ||
| 34 | protected $kernel;  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * @var Connection  | 
            ||
| 38 | */  | 
            ||
| 39 | protected $connection;  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * @var ContainerInterface  | 
            ||
| 43 | */  | 
            ||
| 44 | protected $container;  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * @var EventDispatcherInterface  | 
            ||
| 48 | */  | 
            ||
| 49 | protected $eventDispatcher;  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * @var array An array with already loaded bundle migration versions  | 
            ||
| 53 | * key = bundle name  | 
            ||
| 54 | * value = latest loaded version  | 
            ||
| 55 | */  | 
            ||
| 56 | protected $loadedVersions;  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * @var array An array with bundles we must work from  | 
            ||
| 60 | */  | 
            ||
| 61 | protected $bundles;  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * @var array An array with excluded bundles  | 
            ||
| 65 | */  | 
            ||
| 66 | protected $excludeBundles;  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * @var string  | 
            ||
| 70 | */  | 
            ||
| 71 | protected $migrationPath = self::DEFAULT_MIGRATIONS_PATH;  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * @param KernelInterface $kernel  | 
            ||
| 75 | * @param Connection $connection  | 
            ||
| 76 | * @param ContainerInterface $container  | 
            ||
| 77 | * @param EventDispatcherInterface $eventDispatcher  | 
            ||
| 78 | */  | 
            ||
| 79 | public function __construct(  | 
            ||
| 90 | |||
| 91 | /**  | 
            ||
| 92 | * @param array $bundles  | 
            ||
| 93 | */  | 
            ||
| 94 | public function setBundles($bundles)  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * @param array $excludeBundles  | 
            ||
| 101 | */  | 
            ||
| 102 | public function setExcludeBundles($excludeBundles)  | 
            ||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * @param string $migrationPath  | 
            ||
| 109 | * @return $this  | 
            ||
| 110 | */  | 
            ||
| 111 | public function setMigrationPath($migrationPath)  | 
            ||
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * @return MigrationState[]  | 
            ||
| 120 | */  | 
            ||
| 121 | public function getMigrations()  | 
            ||
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * Gets a list of all directories contain migration scripts  | 
            ||
| 157 | *  | 
            ||
| 158 | * @return array  | 
            ||
| 159 | * key = bundle name  | 
            ||
| 160 | * value = array  | 
            ||
| 161 | * . key = a migration version (actually it equals the name of migration directory)  | 
            ||
| 162 | * . or empty string for root migration directory  | 
            ||
| 163 | * . value = full path to a migration directory  | 
            ||
| 164 | */  | 
            ||
| 165 | protected function getMigrationDirectories()  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * @param string $bundlePath  | 
            ||
| 204 | * @return string  | 
            ||
| 205 | */  | 
            ||
| 206 | protected function getBundleMigrationPath($bundlePath)  | 
            ||
| 210 | |||
| 211 | |||
| 212 | /**  | 
            ||
| 213 | * Finds migration files and call "include_once" for each file  | 
            ||
| 214 | *  | 
            ||
| 215 | * @param array $migrationDirectories  | 
            ||
| 216 | * key = bundle name  | 
            ||
| 217 | * value = array  | 
            ||
| 218 | * . key = a migration version or empty string for root migration directory  | 
            ||
| 219 | * . value = full path to a migration directory  | 
            ||
| 220 | *  | 
            ||
| 221 | * @return array loaded files  | 
            ||
| 222 | * 'migrations' => array  | 
            ||
| 223 | * . key = full file path  | 
            ||
| 224 | * . value = array  | 
            ||
| 225 | * . 'bundleName' => bundle name  | 
            ||
| 226 | * . 'version' => migration version  | 
            ||
| 227 | * 'installers' => array  | 
            ||
| 228 | * . key = full file path  | 
            ||
| 229 | * . value = bundle name  | 
            ||
| 230 | * 'bundles' => string[] names of bundles  | 
            ||
| 231 | */  | 
            ||
| 232 | protected function loadMigrationScripts(array $migrationDirectories)  | 
            ||
| 260 | |||
| 261 | /**  | 
            ||
| 262 | * Creates an instances of all classes implement migration scripts  | 
            ||
| 263 | *  | 
            ||
| 264 | * @param MigrationState[] $result  | 
            ||
| 265 | * @param array $files Files contain migration scripts  | 
            ||
| 266 | * 'migrations' => array  | 
            ||
| 267 | * . key = full file path  | 
            ||
| 268 | * . value = array  | 
            ||
| 269 | * . 'bundleName' => bundle name  | 
            ||
| 270 | * . 'version' => migration version  | 
            ||
| 271 | * 'installers' => array  | 
            ||
| 272 | * . key = full file path  | 
            ||
| 273 | * . value = bundle name  | 
            ||
| 274 | * 'bundles' => string[] names of bundles  | 
            ||
| 275 | *  | 
            ||
| 276 | * @throws \RuntimeException if a migration script contains more than one class  | 
            ||
| 277 | */  | 
            ||
| 278 | protected function createMigrationObjects(&$result, $files)  | 
            ||
| 327 | |||
| 328 | /**  | 
            ||
| 329 | * Groups migrations by bundle and version  | 
            ||
| 330 | * Sorts grouped migrations within the same version  | 
            ||
| 331 | *  | 
            ||
| 332 | * @param array $files  | 
            ||
| 333 | * @param array $migrations  | 
            ||
| 334 | *  | 
            ||
| 335 | * @return array  | 
            ||
| 336 | */  | 
            ||
| 337 | protected function groupAndSortMigrations($files, $migrations)  | 
            ||
| 385 | |||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * Loads migration objects  | 
            ||
| 389 | *  | 
            ||
| 390 | * @param $files  | 
            ||
| 391 | *  | 
            ||
| 392 | * @return array  | 
            ||
| 393 | * @throws \RuntimeException  | 
            ||
| 394 | */  | 
            ||
| 395 | protected function loadMigrationObjects($files)  | 
            ||
| 439 | |||
| 440 | |||
| 441 | /**  | 
            ||
| 442 | * Removes already installed migrations  | 
            ||
| 443 | *  | 
            ||
| 444 | * @param array $migrationDirectories  | 
            ||
| 445 | * key = bundle name  | 
            ||
| 446 | * value = array  | 
            ||
| 447 | * . key = a migration version or empty string for root migration directory  | 
            ||
| 448 | * . value = full path to a migration directory  | 
            ||
| 449 | */  | 
            ||
| 450 | protected function filterMigrations(array &$migrationDirectories)  | 
            ||
| 467 | |||
| 468 | /**  | 
            ||
| 469 | * @return BundleInterface[] key = bundle name  | 
            ||
| 470 | */  | 
            ||
| 471 | protected function getBundleList()  | 
            ||
| 491 | }  | 
            ||
| 492 |