Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MigrationService 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 MigrationService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class MigrationService { |
||
| 37 | |||
| 38 | /** @var boolean */ |
||
| 39 | private $migrationTableCreated; |
||
| 40 | /** @var array */ |
||
| 41 | private $migrations; |
||
| 42 | /** @var IOutput */ |
||
| 43 | private $output; |
||
| 44 | /** @var Connection */ |
||
| 45 | private $connection; |
||
| 46 | /** @var string */ |
||
| 47 | private $appName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * MigrationService constructor. |
||
| 51 | * |
||
| 52 | * @param $appName |
||
| 53 | * @param IDBConnection $connection |
||
| 54 | * @param AppLocator $appLocator |
||
| 55 | * @param IOutput|null $output |
||
| 56 | * @throws \Exception |
||
| 57 | */ |
||
| 58 | function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
||
| 59 | $this->appName = $appName; |
||
| 60 | $this->connection = $connection; |
||
|
|
|||
| 61 | $this->output = $output; |
||
| 62 | if (is_null($this->output)) { |
||
| 63 | $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($appName === 'core') { |
||
| 67 | $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
||
| 68 | $this->migrationsNamespace = 'OC\\Migrations'; |
||
| 69 | } else { |
||
| 70 | if (is_null($appLocator)) { |
||
| 71 | $appLocator = new AppLocator(); |
||
| 72 | } |
||
| 73 | $appPath = $appLocator->getAppPath($appName); |
||
| 74 | $this->migrationsPath = "$appPath/appinfo/Migrations"; |
||
| 75 | $this->migrationsNamespace = "OCA\\$appName\\Migrations"; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (!is_dir($this->migrationsPath)) { |
||
| 79 | if (!mkdir($this->migrationsPath)) { |
||
| 80 | throw new \Exception("Could not create migration folder \"{$this->migrationsPath}\""); |
||
| 81 | }; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | private static function requireOnce($file) { |
||
| 86 | require_once $file; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the name of the app for which this migration is executed |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getApp() { |
||
| 95 | return $this->appName; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return bool |
||
| 100 | * @codeCoverageIgnore - this will implicitly tested on installation |
||
| 101 | */ |
||
| 102 | private function createMigrationTable() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns all versions which have already been applied |
||
| 132 | * |
||
| 133 | * @return string[] |
||
| 134 | * @codeCoverageIgnore - no need to test this |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function getMigratedVersions() { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Returns all versions which are available in the migration folder |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getAvailableVersions() { |
||
| 161 | |||
| 162 | protected function findMigrations() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $to |
||
| 196 | */ |
||
| 197 | private function getMigrationsToExecute($to) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string[] $knownMigrations |
||
| 216 | */ |
||
| 217 | private function shallBeExecuted($m, $knownMigrations) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $version |
||
| 227 | */ |
||
| 228 | private function markAsExecuted($version) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the name of the table which holds the already applied versions |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getMigrationsTableName() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the namespace of the version classes |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getMigrationsNamespace() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Returns the directory which holds the versions |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getMigrationsDirectory() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return the explicit version for the aliases; current, next, prev, latest |
||
| 264 | * |
||
| 265 | * @param string $alias |
||
| 266 | * @return mixed|null|string |
||
| 267 | */ |
||
| 268 | public function getMigration($alias) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $version |
||
| 286 | * @param int $delta |
||
| 287 | * @return null|string |
||
| 288 | */ |
||
| 289 | private function getRelativeVersion($version, $delta) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | private function getCurrentVersion() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function getClass($version) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Allows to set an IOutput implementation which is used for logging progress and messages |
||
| 329 | * |
||
| 330 | * @param IOutput $output |
||
| 331 | */ |
||
| 332 | public function setOutput(IOutput $output) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Applies all not yet applied versions up to $to |
||
| 338 | * |
||
| 339 | * @param string $to |
||
| 340 | */ |
||
| 341 | public function migrate($to = 'latest') { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $version |
||
| 351 | */ |
||
| 352 | View Code Duplication | protected function createInstance($version) { |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Executes one explicit version |
||
| 369 | * |
||
| 370 | * @param string $version |
||
| 371 | */ |
||
| 372 | public function executeStep($version) { |
||
| 391 | |||
| 392 | private function ensureMigrationsAreLoaded() { |
||
| 397 | } |
||
| 398 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.