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 |
||
| 20 | class Vault |
||
| 21 | { |
||
| 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 IndexMergerInterface |
||
| 52 | */ |
||
| 53 | protected $indexMerger; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var ConflictHandlerInterface |
||
| 57 | */ |
||
| 58 | protected $conflictHandler; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var OperationListBuilderInterface |
||
| 62 | */ |
||
| 63 | protected $operationListBuilder; |
||
| 64 | |||
| 65 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
| 70 | |||
| 71 | public function getStoreman(): Storeman |
||
| 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 getIndexMerger(): IndexMergerInterface |
||
| 100 | |||
| 101 | public function getConflictHandler(): ConflictHandlerInterface |
||
| 105 | |||
| 106 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Reads and returns the index representing the local state on the last synchronization. |
||
| 113 | * |
||
| 114 | * @return Index |
||
| 115 | * @throws Exception |
||
| 116 | */ |
||
| 117 | public function loadLastLocalIndex(): ?Index |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Reads and returns the current remote index. |
||
| 140 | * |
||
| 141 | * @param int $revision Revision to load. Defaults to the last revision. |
||
| 142 | * |
||
| 143 | * @return Index |
||
| 144 | */ |
||
| 145 | public function loadRemoteIndex(int $revision = null): ?Index |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
| 156 | * |
||
| 157 | * @return Index |
||
| 158 | */ |
||
| 159 | public function buildMergedIndex(): Index |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns ordered list of operations required to synchronize the vault with the local path. |
||
| 166 | * In addition to the object specific operations contained in the returned OperationList additional operations |
||
| 167 | * might be necessary like index updates that do not belong to specific index objects. |
||
| 168 | * |
||
| 169 | * @return OperationList |
||
| 170 | */ |
||
| 171 | public function getOperationList(): OperationList |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
| 184 | * |
||
| 185 | * @param int $newRevision |
||
| 186 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 187 | * |
||
| 188 | * @return OperationResultList |
||
| 189 | * @throws Exception |
||
| 190 | */ |
||
| 191 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Loads and returns the list of synchronizations from the vault. |
||
| 267 | * |
||
| 268 | * @return SynchronizationList |
||
| 269 | */ |
||
| 270 | public function loadSynchronizationList(): SynchronizationList |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Restores the local state at the given revision from the vault. |
||
| 277 | * |
||
| 278 | * @param int $revision |
||
| 279 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 280 | * |
||
| 281 | * @return OperationResultList |
||
| 282 | * @throws Exception |
||
| 283 | */ |
||
| 284 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $targetPath |
||
| 291 | * @param int $revision |
||
| 292 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
| 293 | * |
||
| 294 | * @return OperationResultList |
||
| 295 | * @throws \Exception |
||
| 296 | */ |
||
| 297 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
| 301 | |||
| 302 | protected function doBuildMergedIndex(Index $localIndex = null, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
| 315 | |||
| 316 | protected function doRestore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null, bool $skipLastLocalIndexUpdate = false, string $targetPath = null): OperationResultList |
||
| 390 | |||
| 391 | protected function writeLastLocalIndex(Index $index): void |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Transforms an IndexObject instance into a scalar array suitable for fputcsv(). |
||
| 410 | * |
||
| 411 | * @param IndexObject $indexObject |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected function indexObjectToScalarArray(IndexObject $indexObject): array |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Reconstructs an IndexObject instance from a scalar array read by fgetcsv(). |
||
| 432 | * |
||
| 433 | * @param array $array |
||
| 434 | * @return IndexObject |
||
| 435 | */ |
||
| 436 | protected function createIndexObjectFromScalarArray(array $array): IndexObject |
||
| 451 | |||
| 452 | protected function getLastLocalIndexFilePath(): string |
||
| 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 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: