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 |
||
| 23 | class Vault implements LoggerAwareInterface |
||
| 24 | { |
||
| 25 | use LoggerAwareTrait; |
||
| 26 | |||
| 27 | |||
| 28 | public const LOCK_SYNC = 'sync'; |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Storeman |
||
| 33 | */ |
||
| 34 | protected $storeman; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var VaultConfiguration |
||
| 38 | */ |
||
| 39 | protected $vaultConfiguration; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var VaultLayoutInterface |
||
| 43 | */ |
||
| 44 | protected $vaultLayout; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var StorageAdapterInterface |
||
| 48 | */ |
||
| 49 | protected $storageAdapter; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var LockAdapterInterface |
||
| 53 | */ |
||
| 54 | protected $lockAdapter; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var IndexMergerInterface |
||
| 58 | */ |
||
| 59 | protected $indexMerger; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ConflictHandlerInterface |
||
| 63 | */ |
||
| 64 | protected $conflictHandler; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var OperationListBuilderInterface |
||
| 68 | */ |
||
| 69 | protected $operationListBuilder; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Index |
||
| 73 | */ |
||
| 74 | protected $lastLocalIndex; |
||
| 75 | |||
| 76 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
| 82 | |||
| 83 | public function getStoreman(): Storeman |
||
| 87 | |||
| 88 | public function getVaultConfiguration(): VaultConfiguration |
||
| 92 | |||
| 93 | public function getVaultLayout(): VaultLayoutInterface |
||
| 97 | |||
| 98 | public function getStorageAdapter(): StorageAdapterInterface |
||
| 102 | |||
| 103 | public function getLockAdapter(): LockAdapterInterface |
||
| 107 | |||
| 108 | public function getIndexMerger(): IndexMergerInterface |
||
| 112 | |||
| 113 | public function getConflictHandler(): ConflictHandlerInterface |
||
| 117 | |||
| 118 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Reads and returns the index representing the local state on the last synchronization. |
||
| 125 | * |
||
| 126 | * @return Index |
||
| 127 | * @throws Exception |
||
| 128 | */ |
||
| 129 | public function getLastLocalIndex(): ?Index |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Reads and returns the current remote index. |
||
| 165 | * |
||
| 166 | * @param int $revision Revision to load. Defaults to the last revision. |
||
| 167 | * |
||
| 168 | * @return Index |
||
| 169 | */ |
||
| 170 | public function getRemoteIndex(int $revision = null): ?Index |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
| 183 | * |
||
| 184 | * @return Index |
||
| 185 | */ |
||
| 186 | public function getMergedIndex(): Index |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns ordered list of operations required to synchronize the vault with the local path. |
||
| 193 | * In addition to the object specific operations contained in the returned OperationList additional operations |
||
| 194 | * might be necessary like index updates that do not belong to specific index objects. |
||
| 195 | * |
||
| 196 | * @return OperationList |
||
| 197 | */ |
||
| 198 | public function getOperationList(): OperationList |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
| 211 | * |
||
| 212 | * @param int $newRevision |
||
| 213 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 214 | * |
||
| 215 | * @return OperationResultList |
||
| 216 | * @throws Exception |
||
| 217 | */ |
||
| 218 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Loads and returns the list of synchronizations from the vault. |
||
| 294 | * |
||
| 295 | * @return SynchronizationList |
||
| 296 | */ |
||
| 297 | public function loadSynchronizationList(): SynchronizationList |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Restores the local state at the given revision from the vault. |
||
| 304 | * |
||
| 305 | * @param int $revision |
||
| 306 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 307 | * |
||
| 308 | * @return OperationResultList |
||
| 309 | * @throws Exception |
||
| 310 | */ |
||
| 311 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $targetPath |
||
| 318 | * @param int $revision |
||
| 319 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
| 320 | * |
||
| 321 | * @return OperationResultList |
||
| 322 | * @throws \Exception |
||
| 323 | */ |
||
| 324 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
| 328 | |||
| 329 | protected function doBuildMergedIndex(Index $localIndex = null, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
| 342 | |||
| 343 | protected function doRestore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null, bool $skipLastLocalIndexUpdate = false, string $targetPath = null): OperationResultList |
||
| 417 | |||
| 418 | protected function writeLastLocalIndex(Index $index): void |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Transforms an IndexObject instance into a scalar array suitable for fputcsv(). |
||
| 445 | * |
||
| 446 | * @param IndexObject $indexObject |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | protected function indexObjectToScalarArray(IndexObject $indexObject): array |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Reconstructs an IndexObject instance from a scalar array read by fgetcsv(). |
||
| 467 | * |
||
| 468 | * @param array $array |
||
| 469 | * @return IndexObject |
||
| 470 | */ |
||
| 471 | protected function createIndexObjectFromScalarArray(array $array): IndexObject |
||
| 486 | |||
| 487 | protected function getLastLocalIndexFilePath(): string |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Returns the service container with this vault as its context. |
||
| 495 | * |
||
| 496 | * @return Container |
||
| 497 | */ |
||
| 498 | protected function getContainer(): Container |
||
| 502 | } |
||
| 503 |
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: