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 |
||
| 11 | class Storeman |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Configuration |
||
| 15 | */ |
||
| 16 | protected $configuration; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Container |
||
| 20 | */ |
||
| 21 | protected $container; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Vault[] |
||
| 25 | */ |
||
| 26 | protected $vaults = []; |
||
| 27 | |||
| 28 | public function __construct(Configuration $configuration, Container $container = null) |
||
| 33 | |||
| 34 | public function getConfiguration(): Configuration |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Returns the DI container of this storeman instance. |
||
| 41 | * Some service names resolve to different implementations depending on the current vault which can be set as a context. |
||
| 42 | * E.g. "storageAdapter" resolves to the storage adapter implementation configured for the current vault. |
||
| 43 | * |
||
| 44 | * @param Vault $vault |
||
| 45 | * @return Container |
||
| 46 | */ |
||
| 47 | public function getContainer(Vault $vault = null): Container |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Returns a specific vault by title. |
||
| 54 | * |
||
| 55 | * @param string $vaultTitle |
||
| 56 | * @return Vault |
||
| 57 | */ |
||
| 58 | public function getVault(string $vaultTitle): Vault |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns all vaults. |
||
| 72 | * |
||
| 73 | * @return Vault[] |
||
| 74 | */ |
||
| 75 | public function getVaults(): array |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Builds and returns an OperationList instance containing all operations required to a full synchronization. |
||
| 86 | * |
||
| 87 | * @return OperationList |
||
| 88 | */ |
||
| 89 | public function buildOperationList(): OperationList |
||
| 100 | |||
| 101 | public function synchronize(array $vaultTitles = [], SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
| 143 | |||
| 144 | View Code Duplication | public function restore(int $toRevision = null, string $fromVault = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
|
| 157 | |||
| 158 | View Code Duplication | public function dump(string $targetPath, int $revision = null, string $fromVault = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Builds and returns a history of all synchronizations on record for this archive. |
||
| 174 | * |
||
| 175 | * @return Synchronization[][] |
||
| 176 | */ |
||
| 177 | public function buildSynchronizationHistory(): array |
||
| 198 | |||
| 199 | protected function getAnyVault(): Vault |
||
| 210 | } |
||
| 211 |
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.