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 |
||
25 | class Vault implements LoggerAwareInterface |
||
26 | { |
||
27 | use LoggerAwareTrait; |
||
28 | |||
29 | |||
30 | public const LOCK_SYNC = 'sync'; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * @var Storeman |
||
35 | */ |
||
36 | protected $storeman; |
||
37 | |||
38 | /** |
||
39 | * @var VaultConfiguration |
||
40 | */ |
||
41 | protected $vaultConfiguration; |
||
42 | |||
43 | /** |
||
44 | * @var VaultLayoutInterface |
||
45 | */ |
||
46 | protected $vaultLayout; |
||
47 | |||
48 | /** |
||
49 | * @var StorageAdapterInterface |
||
50 | */ |
||
51 | protected $storageAdapter; |
||
52 | |||
53 | /** |
||
54 | * @var LockAdapterInterface |
||
55 | */ |
||
56 | protected $lockAdapter; |
||
57 | |||
58 | /** |
||
59 | * @var IndexMergerInterface |
||
60 | */ |
||
61 | protected $indexMerger; |
||
62 | |||
63 | /** |
||
64 | * @var ConflictHandlerInterface |
||
65 | */ |
||
66 | protected $conflictHandler; |
||
67 | |||
68 | /** |
||
69 | * @var OperationListBuilderInterface |
||
70 | */ |
||
71 | protected $operationListBuilder; |
||
72 | |||
73 | /** |
||
74 | * @var Index |
||
75 | */ |
||
76 | protected $lastLocalIndex; |
||
77 | |||
78 | public function __construct(Storeman $storeman, VaultConfiguration $vaultConfiguration) |
||
79 | { |
||
80 | $this->storeman = $storeman; |
||
81 | $this->vaultConfiguration = $vaultConfiguration; |
||
82 | $this->logger = new NullLogger(); |
||
83 | } |
||
84 | |||
85 | public function getStoreman(): Storeman |
||
86 | { |
||
87 | return $this->storeman; |
||
88 | } |
||
89 | |||
90 | public function getVaultConfiguration(): VaultConfiguration |
||
91 | { |
||
92 | return $this->vaultConfiguration; |
||
93 | } |
||
94 | |||
95 | public function getVaultLayout(): VaultLayoutInterface |
||
96 | { |
||
97 | return $this->vaultLayout ?: ($this->vaultLayout = $this->getContainer()->get('vaultLayout')); |
||
98 | } |
||
99 | |||
100 | public function getStorageAdapter(): StorageAdapterInterface |
||
101 | { |
||
102 | return $this->storageAdapter ?: ($this->storageAdapter = $this->getContainer()->get('storageAdapter')); |
||
103 | } |
||
104 | |||
105 | public function getLockAdapter(): LockAdapterInterface |
||
106 | { |
||
107 | return $this->lockAdapter ?: ($this->lockAdapter = $this->getContainer()->get('lockAdapter')); |
||
108 | } |
||
109 | |||
110 | public function getIndexMerger(): IndexMergerInterface |
||
111 | { |
||
112 | return $this->indexMerger ?: ($this->indexMerger = $this->getContainer()->get('indexMerger')); |
||
113 | } |
||
114 | |||
115 | public function getConflictHandler(): ConflictHandlerInterface |
||
116 | { |
||
117 | return $this->conflictHandler ?: ($this->conflictHandler = $this->getContainer()->get('conflictHandler')); |
||
118 | } |
||
119 | |||
120 | public function getOperationListBuilder(): OperationListBuilderInterface |
||
121 | { |
||
122 | return $this->operationListBuilder ?: ($this->operationListBuilder = $this->getContainer()->get('operationListBuilder')); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Reads and returns the index representing the local state on the last synchronization. |
||
127 | * |
||
128 | * @return Index |
||
129 | * @throws Exception |
||
130 | */ |
||
131 | public function getLastLocalIndex(): ?Index |
||
132 | { |
||
133 | if ($this->lastLocalIndex === null) |
||
134 | { |
||
135 | $index = null; |
||
136 | $path = $this->getLastLocalIndexFilePath(); |
||
137 | |||
138 | if (is_file($path)) |
||
139 | { |
||
140 | $this->logger->info("Reading in last local index from {$path}..."); |
||
141 | |||
142 | $stream = fopen($path, 'rb'); |
||
143 | |||
144 | $index = new Index(); |
||
145 | while (($row = fgetcsv($stream)) !== false) |
||
146 | { |
||
147 | $index->addObject($this->createIndexObjectFromScalarArray($row)); |
||
148 | } |
||
149 | |||
150 | fclose($stream); |
||
151 | |||
152 | $this->logger->info("Read {$index->count()} records for last local index"); |
||
153 | } |
||
154 | else |
||
155 | { |
||
156 | $this->logger->info("No last local index exists"); |
||
157 | } |
||
158 | |||
159 | $this->lastLocalIndex = $index; |
||
160 | } |
||
161 | |||
162 | return $this->lastLocalIndex; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Reads and returns the current remote index. |
||
167 | * |
||
168 | * @param int $revision Revision to load. Defaults to the last revision. |
||
169 | * |
||
170 | * @return Index |
||
171 | */ |
||
172 | public function getRemoteIndex(int $revision = null): ?Index |
||
173 | { |
||
174 | $this->logger->info(sprintf("Loading %s remote index...", $revision ? "r{$revision}" : 'latest')); |
||
175 | |||
176 | $synchronization = $revision ? |
||
177 | $this->getVaultLayout()->getSynchronization($revision) : |
||
178 | $this->getVaultLayout()->getLastSynchronization(); |
||
179 | |||
180 | return $synchronization ? $synchronization->getIndex() : null; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Computes and returns the index representing the vault state after the local index has been merged with the remote index. |
||
185 | * |
||
186 | * @return Index |
||
187 | */ |
||
188 | public function getMergedIndex(): Index |
||
189 | { |
||
190 | return $this->doBuildMergedIndex(); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Synchronizes the local with the remote state by executing all operations returned by getOperationList() |
||
195 | * |
||
196 | * @param int $newRevision |
||
197 | * @param SynchronizationProgressListenerInterface $progressionListener |
||
198 | * |
||
199 | * @return OperationResultList |
||
200 | * @throws Exception |
||
201 | */ |
||
202 | public function synchronize(int $newRevision = null, SynchronizationProgressListenerInterface $progressionListener = null): OperationResultList |
||
203 | { |
||
204 | $progressionListener = $progressionListener ?: new DummySynchronizationProgressListener(); |
||
205 | |||
206 | $localIndex = $this->storeman->getLocalIndex(); |
||
207 | $lastLocalIndex = $this->getLastLocalIndex(); |
||
208 | |||
209 | |||
210 | if (!$this->getLockAdapter()->acquireLock(static::LOCK_SYNC)) |
||
211 | { |
||
212 | throw new Exception('Failed to acquire lock.'); |
||
213 | } |
||
214 | |||
215 | |||
216 | $lastSynchronization = $this->getVaultLayout()->getLastSynchronization(); |
||
217 | |||
218 | if ($lastSynchronization) |
||
219 | { |
||
220 | $newRevision = $newRevision ?: ($lastSynchronization->getRevision() + 1); |
||
221 | $remoteIndex = $lastSynchronization->getIndex(); |
||
222 | } |
||
223 | else |
||
224 | { |
||
225 | $newRevision = $newRevision ?: 1; |
||
226 | $remoteIndex = null; |
||
227 | } |
||
228 | |||
229 | // compute merged index |
||
230 | $mergedIndex = $this->doBuildMergedIndex($localIndex, $lastLocalIndex, $remoteIndex); |
||
231 | |||
232 | $synchronization = new Synchronization($newRevision, new \DateTime(), $this->storeman->getConfiguration()->getIdentity(), $mergedIndex); |
||
233 | |||
234 | $operationList = $this->getOperationListBuilder()->buildOperationList($mergedIndex, $localIndex); |
||
235 | $operationList->add(new OperationListItem(new WriteSynchronizationOperation($synchronization))); |
||
236 | |||
237 | $operationResultList = $this->executeOperationList($operationList, $this->storeman->getConfiguration()->getPath(), $progressionListener); |
||
238 | |||
239 | // release lock |
||
240 | if (!$this->getLockAdapter()->releaseLock(static::LOCK_SYNC)) |
||
241 | { |
||
242 | throw new Exception('Failed to release lock.'); |
||
243 | } |
||
244 | |||
245 | // save merged index locally |
||
246 | $this->writeLastLocalIndex($mergedIndex); |
||
247 | |||
248 | return $operationResultList; |
||
249 | } |
||
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 |
||
316 | { |
||
317 | $progressionListener = $progressionListener ?: new DummySynchronizationProgressListener(); |
||
318 | $targetPath = $targetPath ?: $this->storeman->getConfiguration()->getPath(); |
||
319 | |||
320 | $localIndex = $this->storeman->getLocalIndex($targetPath); |
||
321 | |||
322 | if (!$this->getLockAdapter()->acquireLock(static::LOCK_SYNC)) |
||
323 | { |
||
324 | throw new Exception('Failed to acquire lock.'); |
||
325 | } |
||
326 | |||
327 | // fall back to last revision |
||
363 | |||
364 | protected function executeOperationList(OperationList $operationList, string $basePath, SynchronizationProgressListenerInterface $progressionListener): OperationResultList |
||
399 | |||
400 | protected function writeLastLocalIndex(Index $index): void |
||
424 | |||
425 | /** |
||
426 | * Transforms an IndexObject instance into a scalar array suitable for fputcsv(). |
||
427 | * |
||
428 | * @param IndexObject $indexObject |
||
429 | * @return array |
||
430 | */ |
||
431 | protected function indexObjectToScalarArray(IndexObject $indexObject): array |
||
446 | |||
447 | /** |
||
448 | * Reconstructs an IndexObject instance from a scalar array read by fgetcsv(). |
||
449 | * |
||
450 | * @param array $array |
||
451 | * @return IndexObject |
||
452 | */ |
||
453 | protected function createIndexObjectFromScalarArray(array $array): IndexObject |
||
468 | |||
469 | protected function getLastLocalIndexFilePath(): string |
||
473 | |||
474 | /** |
||
475 | * Returns the service container with this vault as its context. |
||
476 | * |
||
477 | * @return Container |
||
478 | */ |
||
479 | protected function getContainer(): Container |
||
483 | } |
||
484 |
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: