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 |
||
19 | class Vault |
||
20 | { |
||
21 | const METADATA_DIRECTORY_NAME = '.storeman'; |
||
22 | const LOCK_SYNC = 'sync'; |
||
23 | |||
24 | /** |
||
25 | * @var Storeman |
||
26 | */ |
||
27 | protected $storeman; |
||
28 | |||
29 | /** |
||
30 | * @var VaultConfiguration |
||
31 | */ |
||
32 | protected $vaultConfiguration; |
||
33 | |||
34 | /** |
||
35 | * @var VaultLayoutInterface |
||
36 | */ |
||
37 | protected $vaultLayout; |
||
38 | |||
39 | /** |
||
40 | * @var StorageAdapterInterface |
||
41 | */ |
||
42 | protected $storageAdapter; |
||
43 | |||
44 | /** |
||
45 | * @var LockAdapterInterface |
||
46 | */ |
||
47 | protected $lockAdapter; |
||
48 | |||
49 | /** |
||
50 | * @var IndexMergerInterface |
||
51 | */ |
||
52 | protected $indexMerger; |
||
53 | |||
54 | /** |
||
55 | * @var ConflictHandlerInterface |
||
56 | */ |
||
57 | protected $conflictHandler; |
||
58 | |||
59 | /** |
||
60 | * @var OperationListBuilderInterface |
||
61 | */ |
||
62 | protected $operationListBuilder; |
||
63 | |||
64 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
69 | |||
70 | public function getVaultConfiguration(): VaultConfiguration |
||
74 | |||
75 | public function getVaultLayout(): VaultLayoutInterface |
||
79 | |||
80 | public function getStorageAdapter(): StorageAdapterInterface |
||
84 | |||
85 | public function getLockAdapter(): LockAdapterInterface |
||
89 | |||
90 | public function getIndexMerger(): IndexMergerInterface |
||
94 | |||
95 | public function getConflictHandler(): ConflictHandlerInterface |
||
99 | |||
100 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
104 | |||
105 | /** |
||
106 | * Builds and returns an index representing the current local state. |
||
107 | * |
||
108 | * @return Index |
||
109 | */ |
||
110 | public function buildLocalIndex(): Index |
||
114 | |||
115 | /** |
||
116 | * Reads and returns the index representing the local state on the last synchronization. |
||
117 | * |
||
118 | * @return Index |
||
119 | * @throws Exception |
||
120 | */ |
||
121 | public function loadLastLocalIndex(): ?Index |
||
141 | |||
142 | /** |
||
143 | * Reads and returns the current remote index. |
||
144 | * |
||
145 | * @param int $revision Revision to load. Defaults to the last revision. |
||
146 | * |
||
147 | * @return Index |
||
148 | */ |
||
149 | public function loadRemoteIndex(int $revision = null): ?Index |
||
157 | |||
158 | /** |
||
159 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
160 | * |
||
161 | * @return Index |
||
162 | */ |
||
163 | public function buildMergedIndex(): Index |
||
167 | |||
168 | /** |
||
169 | * Returns ordered list of operations required to synchronize the vault with the local path. |
||
170 | * In addition to the object specific operations contained in the returned OperationList additional operations |
||
171 | * might be necessary like index updates that do not belong to specific index objects. |
||
172 | * |
||
173 | * @return OperationList |
||
174 | */ |
||
175 | public function getOperationList(): OperationList |
||
185 | |||
186 | /** |
||
187 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
188 | * |
||
189 | * @param int $newRevision |
||
190 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
191 | * |
||
192 | * @return OperationResultList |
||
193 | * @throws Exception |
||
194 | */ |
||
195 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
268 | |||
269 | /** |
||
270 | * Loads and returns the list of synchronizations from the vault. |
||
271 | * |
||
272 | * @return SynchronizationList |
||
273 | */ |
||
274 | public function loadSynchronizationList(): SynchronizationList |
||
278 | |||
279 | /** |
||
280 | * Restores the local state at the given revision from the vault. |
||
281 | * |
||
282 | * @param int $revision |
||
283 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
284 | * |
||
285 | * @return OperationResultList |
||
286 | * @throws Exception |
||
287 | */ |
||
288 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
292 | |||
293 | /** |
||
294 | * @param string $targetPath |
||
295 | * @param int $revision |
||
296 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
297 | * |
||
298 | * @return OperationResultList |
||
299 | * @throws \Exception |
||
300 | */ |
||
301 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
305 | |||
306 | protected function doBuildLocalIndex(string $path = null): Index |
||
338 | |||
339 | protected function doBuildMergedIndex(Index $localIndex = null, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
352 | |||
353 | protected function doRestore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null, bool $skipLastLocalIndexUpdate = false, string $targetPath = null): OperationResultList |
||
426 | |||
427 | protected function writeLastLocalIndex(Index $index): void |
||
443 | |||
444 | protected function initMetadataDirectory(): string |
||
458 | |||
459 | protected function getLastLocalIndexFilePath(): string |
||
463 | |||
464 | /** |
||
465 | * Returns the service container with this vault as its context. |
||
466 | * |
||
467 | * @return Container |
||
468 | */ |
||
469 | protected function getContainer(): Container |
||
473 | } |
||
474 |