Complex classes like Vault 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 Vault, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Vault |
||
| 19 | { |
||
| 20 | public const CONFIG_FILE_NAME = 'storeman.json'; |
||
| 21 | public const METADATA_DIRECTORY_NAME = '.storeman'; |
||
| 22 | public const LOCK_SYNC = 'sync'; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Storeman |
||
| 27 | */ |
||
| 28 | protected $storeman; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var VaultConfiguration |
||
| 32 | */ |
||
| 33 | protected $vaultConfiguration; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var VaultLayoutInterface |
||
| 37 | */ |
||
| 38 | protected $vaultLayout; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var StorageAdapterInterface |
||
| 42 | */ |
||
| 43 | protected $storageAdapter; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var LockAdapterInterface |
||
| 47 | */ |
||
| 48 | protected $lockAdapter; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var IndexBuilderInterface |
||
| 52 | */ |
||
| 53 | protected $indexBuilder; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var IndexMergerInterface |
||
| 57 | */ |
||
| 58 | protected $indexMerger; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ConflictHandlerInterface |
||
| 62 | */ |
||
| 63 | protected $conflictHandler; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var OperationListBuilderInterface |
||
| 67 | */ |
||
| 68 | protected $operationListBuilder; |
||
| 69 | |||
| 70 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
| 75 | |||
| 76 | public function getVaultConfiguration(): VaultConfiguration |
||
| 80 | |||
| 81 | public function getVaultLayout(): VaultLayoutInterface |
||
| 85 | |||
| 86 | public function getStorageAdapter(): StorageAdapterInterface |
||
| 90 | |||
| 91 | public function getLockAdapter(): LockAdapterInterface |
||
| 95 | |||
| 96 | public function getIndexBuilder(): IndexBuilderInterface |
||
| 100 | |||
| 101 | public function getIndexMerger(): IndexMergerInterface |
||
| 105 | |||
| 106 | public function getConflictHandler(): ConflictHandlerInterface |
||
| 110 | |||
| 111 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Builds and returns an index representing the current local state. |
||
| 118 | * |
||
| 119 | * @return Index |
||
| 120 | */ |
||
| 121 | public function buildLocalIndex(): Index |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Reads and returns the index representing the local state on the last synchronization. |
||
| 131 | * |
||
| 132 | * @return Index |
||
| 133 | * @throws Exception |
||
| 134 | */ |
||
| 135 | public function loadLastLocalIndex(): ?Index |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Reads and returns the current remote index. |
||
| 158 | * |
||
| 159 | * @param int $revision Revision to load. Defaults to the last revision. |
||
| 160 | * |
||
| 161 | * @return Index |
||
| 162 | */ |
||
| 163 | public function loadRemoteIndex(int $revision = null): ?Index |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
| 174 | * |
||
| 175 | * @return Index |
||
| 176 | */ |
||
| 177 | public function buildMergedIndex(): Index |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns ordered list of operations required to synchronize the vault with the local path. |
||
| 184 | * In addition to the object specific operations contained in the returned OperationList additional operations |
||
| 185 | * might be necessary like index updates that do not belong to specific index objects. |
||
| 186 | * |
||
| 187 | * @return OperationList |
||
| 188 | */ |
||
| 189 | public function getOperationList(): OperationList |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
| 202 | * |
||
| 203 | * @param int $newRevision |
||
| 204 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 205 | * |
||
| 206 | * @return OperationResultList |
||
| 207 | * @throws Exception |
||
| 208 | */ |
||
| 209 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Loads and returns the list of synchronizations from the vault. |
||
| 285 | * |
||
| 286 | * @return SynchronizationList |
||
| 287 | */ |
||
| 288 | public function loadSynchronizationList(): SynchronizationList |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Restores the local state at the given revision from the vault. |
||
| 295 | * |
||
| 296 | * @param int $revision |
||
| 297 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 298 | * |
||
| 299 | * @return OperationResultList |
||
| 300 | * @throws Exception |
||
| 301 | */ |
||
| 302 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $targetPath |
||
| 309 | * @param int $revision |
||
| 310 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
| 311 | * |
||
| 312 | * @return OperationResultList |
||
| 313 | * @throws \Exception |
||
| 314 | */ |
||
| 315 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
| 319 | |||
| 320 | protected function doBuildMergedIndex(Index $localIndex = null, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
| 333 | |||
| 334 | protected function doRestore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null, bool $skipLastLocalIndexUpdate = false, string $targetPath = null): OperationResultList |
||
| 408 | |||
| 409 | protected function writeLastLocalIndex(Index $index): void |
||
| 425 | |||
| 426 | protected function initMetadataDirectory(): string |
||
| 440 | |||
| 441 | protected function getLastLocalIndexFilePath(): string |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return string[] |
||
| 449 | */ |
||
| 450 | protected function getLocalIndexExclusionPatterns() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Returns the service container with this vault as its context. |
||
| 460 | * |
||
| 461 | * @return Container |
||
| 462 | */ |
||
| 463 | protected function getContainer(): Container |
||
| 467 | } |
||
| 468 |