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 |
||
| 22 | class Vault implements LoggerAwareInterface |
||
| 23 | { |
||
| 24 | use LoggerAwareTrait; |
||
| 25 | |||
| 26 | |||
| 27 | public const LOCK_SYNC = 'sync'; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Storeman |
||
| 32 | */ |
||
| 33 | protected $storeman; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var VaultConfiguration |
||
| 37 | */ |
||
| 38 | protected $vaultConfiguration; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var VaultLayoutInterface |
||
| 42 | */ |
||
| 43 | protected $vaultLayout; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var StorageAdapterInterface |
||
| 47 | */ |
||
| 48 | protected $storageAdapter; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var LockAdapterInterface |
||
| 52 | */ |
||
| 53 | protected $lockAdapter; |
||
| 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 | /** |
||
| 71 | * @var Index |
||
| 72 | */ |
||
| 73 | protected $lastLocalIndex; |
||
| 74 | |||
| 75 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
| 81 | |||
| 82 | public function getStoreman(): Storeman |
||
| 86 | |||
| 87 | public function getVaultConfiguration(): VaultConfiguration |
||
| 91 | |||
| 92 | public function getVaultLayout(): VaultLayoutInterface |
||
| 96 | |||
| 97 | public function getStorageAdapter(): StorageAdapterInterface |
||
| 101 | |||
| 102 | public function getLockAdapter(): LockAdapterInterface |
||
| 106 | |||
| 107 | public function getIndexMerger(): IndexMergerInterface |
||
| 111 | |||
| 112 | public function getConflictHandler(): ConflictHandlerInterface |
||
| 116 | |||
| 117 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Reads and returns the index representing the local state on the last synchronization. |
||
| 124 | * |
||
| 125 | * @return Index |
||
| 126 | * @throws Exception |
||
| 127 | */ |
||
| 128 | public function getLastLocalIndex(): ?Index |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Reads and returns the current remote index. |
||
| 164 | * |
||
| 165 | * @param int $revision Revision to load. Defaults to the last revision. |
||
| 166 | * |
||
| 167 | * @return Index |
||
| 168 | */ |
||
| 169 | public function getRemoteIndex(int $revision = null): ?Index |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
| 182 | * |
||
| 183 | * @return Index |
||
| 184 | */ |
||
| 185 | public function getMergedIndex(): Index |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
| 192 | * |
||
| 193 | * @param int $newRevision |
||
| 194 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 195 | * |
||
| 196 | * @return OperationResultList |
||
| 197 | * @throws Exception |
||
| 198 | */ |
||
| 199 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Restores the local state at the given revision from the vault. |
||
| 253 | * |
||
| 254 | * @param int $revision |
||
| 255 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
| 256 | * |
||
| 257 | * @return OperationResultList |
||
| 258 | * @throws Exception |
||
| 259 | */ |
||
| 260 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $targetPath |
||
| 267 | * @param int $revision |
||
| 268 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
| 269 | * |
||
| 270 | * @return OperationResultList |
||
| 271 | * @throws \Exception |
||
| 272 | */ |
||
| 273 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns a hash that is the same for any vault referencing the same physical storage location. |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getHash(): string |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns an identifier usable for UI. |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getIdentifier(): string |
||
| 300 | |||
| 301 | protected function doBuildMergedIndex(Index $localIndex = null, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
| 314 | |||
| 315 | protected function doRestore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null, bool $skipLastLocalIndexUpdate = false, string $targetPath = null): OperationResultList |
||
| 367 | |||
| 368 | protected function executeOperationList(OperationList $operationList, string $basePath, SynchronizationProgressListenerInterface $progressionListener): OperationResultList |
||
| 402 | |||
| 403 | protected function writeLastLocalIndex(Index $index): void |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Transforms an IndexObject instance into a scalar array suitable for fputcsv(). |
||
| 430 | * |
||
| 431 | * @param IndexObject $indexObject |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | protected function indexObjectToScalarArray(IndexObject $indexObject): array |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Reconstructs an IndexObject instance from a scalar array read by fgetcsv(). |
||
| 452 | * |
||
| 453 | * @param array $array |
||
| 454 | * @return IndexObject |
||
| 455 | */ |
||
| 456 | protected function createIndexObjectFromScalarArray(array $array): IndexObject |
||
| 471 | |||
| 472 | protected function getLastLocalIndexFilePath(): string |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the service container with this vault as its context. |
||
| 479 | * |
||
| 480 | * @return Container |
||
| 481 | */ |
||
| 482 | protected function getContainer(): Container |
||
| 486 | } |
||
| 487 |
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: