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 |
||
21 | class Vault |
||
22 | { |
||
23 | public const CONFIG_FILE_NAME = 'storeman.json'; |
||
24 | public const METADATA_DIRECTORY_NAME = '.storeman'; |
||
25 | public const LOCK_SYNC = 'sync'; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * @var Storeman |
||
30 | */ |
||
31 | protected $storeman; |
||
32 | |||
33 | /** |
||
34 | * @var VaultConfiguration |
||
35 | */ |
||
36 | protected $vaultConfiguration; |
||
37 | |||
38 | /** |
||
39 | * @var VaultLayoutInterface |
||
40 | */ |
||
41 | protected $vaultLayout; |
||
42 | |||
43 | /** |
||
44 | * @var FileReader |
||
45 | */ |
||
46 | protected $fileReader; |
||
47 | |||
48 | /** |
||
49 | * @var StorageAdapterInterface |
||
50 | */ |
||
51 | protected $storageAdapter; |
||
52 | |||
53 | /** |
||
54 | * @var LockAdapterInterface |
||
55 | */ |
||
56 | protected $lockAdapter; |
||
57 | |||
58 | /** |
||
59 | * @var IndexBuilderInterface |
||
60 | */ |
||
61 | protected $indexBuilder; |
||
62 | |||
63 | /** |
||
64 | * @var IndexMergerInterface |
||
65 | */ |
||
66 | protected $indexMerger; |
||
67 | |||
68 | /** |
||
69 | * @var ConflictHandlerInterface |
||
70 | */ |
||
71 | protected $conflictHandler; |
||
72 | |||
73 | /** |
||
74 | * @var OperationListBuilderInterface |
||
75 | */ |
||
76 | protected $operationListBuilder; |
||
77 | |||
78 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
83 | |||
84 | public function getVaultConfiguration(): VaultConfiguration |
||
88 | |||
89 | public function getVaultLayout(): VaultLayoutInterface |
||
93 | |||
94 | public function getFileReader(): FileReader |
||
98 | |||
99 | public function getStorageAdapter(): StorageAdapterInterface |
||
103 | |||
104 | public function getLockAdapter(): LockAdapterInterface |
||
108 | |||
109 | public function getIndexBuilder(): IndexBuilderInterface |
||
113 | |||
114 | public function getIndexMerger(): IndexMergerInterface |
||
118 | |||
119 | public function getConflictHandler(): ConflictHandlerInterface |
||
123 | |||
124 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
128 | |||
129 | /** |
||
130 | * Builds and returns an index representing the current local state. |
||
131 | * |
||
132 | * @return Index |
||
133 | */ |
||
134 | public function buildLocalIndex(): Index |
||
141 | |||
142 | /** |
||
143 | * Reads and returns the index representing the local state on the last synchronization. |
||
144 | * |
||
145 | * @return Index |
||
146 | * @throws Exception |
||
147 | */ |
||
148 | public function loadLastLocalIndex(): ?Index |
||
168 | |||
169 | /** |
||
170 | * Reads and returns the current remote index. |
||
171 | * |
||
172 | * @param int $revision Revision to load. Defaults to the last revision. |
||
173 | * |
||
174 | * @return Index |
||
175 | */ |
||
176 | public function loadRemoteIndex(int $revision = null): ?Index |
||
184 | |||
185 | /** |
||
186 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
187 | * |
||
188 | * @return Index |
||
189 | */ |
||
190 | public function buildMergedIndex(): Index |
||
194 | |||
195 | /** |
||
196 | * Returns ordered list of operations required to synchronize the vault with the local path. |
||
197 | * In addition to the object specific operations contained in the returned OperationList additional operations |
||
198 | * might be necessary like index updates that do not belong to specific index objects. |
||
199 | * |
||
200 | * @return OperationList |
||
201 | */ |
||
202 | public function getOperationList(): OperationList |
||
212 | |||
213 | /** |
||
214 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
215 | * |
||
216 | * @param int $newRevision |
||
217 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
218 | * |
||
219 | * @return OperationResultList |
||
220 | * @throws Exception |
||
221 | */ |
||
222 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
295 | |||
296 | /** |
||
297 | * Loads and returns the list of synchronizations from the vault. |
||
298 | * |
||
299 | * @return SynchronizationList |
||
300 | */ |
||
301 | public function loadSynchronizationList(): SynchronizationList |
||
305 | |||
306 | /** |
||
307 | * Restores the local state at the given revision from the vault. |
||
308 | * |
||
309 | * @param int $revision |
||
310 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
311 | * |
||
312 | * @return OperationResultList |
||
313 | * @throws Exception |
||
314 | */ |
||
315 | public function restore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
319 | |||
320 | /** |
||
321 | * @param string $targetPath |
||
322 | * @param int $revision |
||
323 | * @param SynchronizationProgressListenerInterface|null $progressListener |
||
324 | * |
||
325 | * @return OperationResultList |
||
326 | * @throws \Exception |
||
327 | */ |
||
328 | public function dump(string $targetPath, int $revision = null, SynchronizationProgressListenerInterface $progressListener = null): OperationResultList |
||
332 | |||
333 | protected function doBuildMergedIndex(Index $localIndex = null, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
346 | |||
347 | protected function doRestore(int $revision = null, SynchronizationProgressListenerInterface $progressionListener = null, bool $skipLastLocalIndexUpdate = false, string $targetPath = null): OperationResultList |
||
421 | |||
422 | protected function writeLastLocalIndex(Index $index): void |
||
438 | |||
439 | /** |
||
440 | * Transforms an IndexObject instance into a scalar array suitable for fputcsv(). |
||
441 | * |
||
442 | * @param IndexObject $indexObject |
||
443 | * @return array |
||
444 | */ |
||
445 | protected function indexObjectToScalarArray(IndexObject $indexObject): array |
||
460 | |||
461 | /** |
||
462 | * Reconstructs an IndexObject instance from a scalar array read by fgetcsv(). |
||
463 | * |
||
464 | * @param array $array |
||
465 | * @return IndexObject |
||
466 | */ |
||
467 | protected function createIndexObjectFromScalarArray(array $array): IndexObject |
||
482 | |||
483 | protected function initMetadataDirectory(): string |
||
497 | |||
498 | protected function getLastLocalIndexFilePath(): string |
||
503 | |||
504 | /** |
||
505 | * @return string[] |
||
506 | */ |
||
507 | protected function getLocalIndexExclusionPatterns() |
||
514 | |||
515 | /** |
||
516 | * Returns the service container with this vault as its context. |
||
517 | * |
||
518 | * @return Container |
||
519 | */ |
||
520 | protected function getContainer(): Container |
||
524 | } |
||
525 |
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: