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 |
||
| 40 | class MigrationService { |
||
| 41 | |||
| 42 | /** @var boolean */ |
||
| 43 | private $migrationTableCreated; |
||
| 44 | /** @var array */ |
||
| 45 | private $migrations; |
||
| 46 | /** @var IOutput */ |
||
| 47 | private $output; |
||
| 48 | /** @var Connection */ |
||
| 49 | private $connection; |
||
| 50 | /** @var string */ |
||
| 51 | private $appName; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * MigrationService constructor. |
||
| 55 | * |
||
| 56 | * @param $appName |
||
| 57 | * @param IDBConnection $connection |
||
| 58 | * @param AppLocator $appLocator |
||
| 59 | * @param IOutput|null $output |
||
| 60 | * @throws \Exception |
||
| 61 | */ |
||
| 62 | public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the name of the app for which this migration is executed |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getApp() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return bool |
||
| 103 | * @codeCoverageIgnore - this will implicitly tested on installation |
||
| 104 | */ |
||
| 105 | private function createMigrationTable() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns all versions which have already been applied |
||
| 135 | * |
||
| 136 | * @return string[] |
||
| 137 | * @codeCoverageIgnore - no need to test this |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function getMigratedVersions() { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Returns all versions which are available in the migration folder |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function getAvailableVersions() { |
||
| 164 | |||
| 165 | protected function findMigrations() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $to |
||
| 206 | * @return string[] |
||
| 207 | */ |
||
| 208 | private function getMigrationsToExecute($to) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $m |
||
| 227 | * @param string[] $knownMigrations |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | private function shallBeExecuted($m, $knownMigrations) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $version |
||
| 240 | */ |
||
| 241 | private function markAsExecuted($version) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the name of the table which holds the already applied versions |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getMigrationsTableName() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns the namespace of the version classes |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getMigrationsNamespace() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the directory which holds the versions |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getMigrationsDirectory() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return the explicit version for the aliases; current, next, prev, latest |
||
| 277 | * |
||
| 278 | * @param string $alias |
||
| 279 | * @return mixed|null|string |
||
| 280 | */ |
||
| 281 | public function getMigration($alias) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $version |
||
| 299 | * @param int $delta |
||
| 300 | * @return null|string |
||
| 301 | */ |
||
| 302 | private function getRelativeVersion($version, $delta) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | private function getCurrentVersion() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $version |
||
| 329 | * @return string |
||
| 330 | * @throws \InvalidArgumentException |
||
| 331 | */ |
||
| 332 | private function getClass($version) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Allows to set an IOutput implementation which is used for logging progress and messages |
||
| 344 | * |
||
| 345 | * @param IOutput $output |
||
| 346 | */ |
||
| 347 | public function setOutput(IOutput $output) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Applies all not yet applied versions up to $to |
||
| 353 | * |
||
| 354 | * @param string $to |
||
| 355 | * @throws \InvalidArgumentException |
||
| 356 | */ |
||
| 357 | public function migrate($to = 'latest') { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $version |
||
| 367 | * @return mixed |
||
| 368 | * @throws \InvalidArgumentException |
||
| 369 | */ |
||
| 370 | View Code Duplication | protected function createInstance($version) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Executes one explicit version |
||
| 387 | * |
||
| 388 | * @param string $version |
||
| 389 | * @throws \InvalidArgumentException |
||
| 390 | */ |
||
| 391 | public function executeStep($version) { |
||
| 416 | |||
| 417 | private function ensureMigrationsAreLoaded() { |
||
| 422 | } |
||
| 423 |
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.