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:
| 1 | <?php |
||
| 44 | class StorageMigrator { |
||
| 45 | /** |
||
| 46 | * @var BackendService |
||
| 47 | */ |
||
| 48 | private $backendService; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var DBConfigService |
||
| 52 | */ |
||
| 53 | private $dbConfig; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var IConfig |
||
| 57 | */ |
||
| 58 | private $config; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var IDBConnection |
||
| 62 | */ |
||
| 63 | private $connection; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ILogger |
||
| 67 | */ |
||
| 68 | private $logger; |
||
| 69 | |||
| 70 | /** @var IUserMountCache */ |
||
| 71 | private $userMountCache; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * StorageMigrator constructor. |
||
| 75 | * |
||
| 76 | * @param BackendService $backendService |
||
| 77 | * @param DBConfigService $dbConfig |
||
| 78 | * @param IConfig $config |
||
| 79 | * @param IDBConnection $connection |
||
| 80 | * @param ILogger $logger |
||
| 81 | * @param IUserMountCache $userMountCache |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function __construct( |
|
|
|
|||
| 84 | BackendService $backendService, |
||
| 85 | DBConfigService $dbConfig, |
||
| 86 | IConfig $config, |
||
| 87 | IDBConnection $connection, |
||
| 88 | ILogger $logger, |
||
| 89 | IUserMountCache $userMountCache |
||
| 90 | ) { |
||
| 91 | $this->backendService = $backendService; |
||
| 92 | $this->dbConfig = $dbConfig; |
||
| 93 | $this->config = $config; |
||
| 94 | $this->connection = $connection; |
||
| 95 | $this->logger = $logger; |
||
| 96 | $this->userMountCache = $userMountCache; |
||
| 97 | } |
||
| 98 | |||
| 99 | private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Migrate personal storages configured by the current user |
||
| 122 | * |
||
| 123 | * @param IUser $user |
||
| 124 | */ |
||
| 125 | public function migrateUser(IUser $user) { |
||
| 138 | } |
||
| 139 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.