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 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
193 | * |
||
194 | * @param int $newRevision |
||
195 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
196 | * |
||
197 | * @return OperationResultList |
||
198 | * @throws Exception |
||
199 | */ |
||
200 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
272 | |||
273 | /** |
||
274 | * Restores the local state at the given revision from the vault. |
||
275 | * |
||
276 | * @param int $revision |
||
277 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
278 | * |
||
279 | * @return OperationResultList |
||
280 | * @throws Exception |
||
281 | */ |
||
282 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
286 | |||
287 | /** |
||
288 | * @param string $targetPath |
||
289 | * @param int $revision |
||
290 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
291 | * |
||
292 | * @return OperationResultList |
||
293 | * @throws \Exception |
||
294 | */ |
||
295 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
299 | |||
300 | /** |
||
301 | * Returns a hash that is the same for any vault referencing the same physical storage location. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getHash(): string |
||
309 | |||
310 | /** |
||
311 | * Returns an identifier usable for UI. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getIdentifier(): string |
||
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 |
||
433 | |||
434 | /** |
||
435 | * Transforms an IndexObject instance into a scalar array suitable for fputcsv(). |
||
436 | * |
||
437 | * @param IndexObject $indexObject |
||
438 | * @return array |
||
439 | */ |
||
440 | protected function indexObjectToScalarArray(IndexObject $indexObject): array |
||
455 | |||
456 | /** |
||
457 | * Reconstructs an IndexObject instance from a scalar array read by fgetcsv(). |
||
458 | * |
||
459 | * @param array $array |
||
460 | * @return IndexObject |
||
461 | */ |
||
462 | protected function createIndexObjectFromScalarArray(array $array): IndexObject |
||
477 | |||
478 | protected function getLastLocalIndexFilePath(): string |
||
482 | |||
483 | /** |
||
484 | * Returns the service container with this vault as its context. |
||
485 | * |
||
486 | * @return Container |
||
487 | */ |
||
488 | protected function getContainer(): Container |
||
492 | } |
||
493 |
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: