Complex classes like StorageService 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 StorageService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | final class StorageService implements Storage |
||
29 | { |
||
30 | const DIRECTION_UP = 'up'; |
||
31 | const DIRECTION_DOWN = 'down'; |
||
32 | |||
33 | /** |
||
34 | * @var Storage[] |
||
35 | */ |
||
36 | private $localStorages = []; |
||
37 | |||
38 | /** |
||
39 | * @var Storage[] |
||
40 | */ |
||
41 | private $remoteStorages = []; |
||
42 | |||
43 | /** |
||
44 | * @var CatalogueFetcher |
||
45 | */ |
||
46 | private $catalogueFetcher; |
||
47 | |||
48 | /** |
||
49 | * @var CatalogueWriter |
||
50 | */ |
||
51 | private $catalogueWriter; |
||
52 | |||
53 | /** |
||
54 | * @var Configuration |
||
55 | */ |
||
56 | private $config; |
||
57 | |||
58 | /** |
||
59 | * @param CatalogueFetcher $catalogueFetcher |
||
60 | * @param CatalogueWriter $catalogueWriter |
||
61 | * @param Configuration $config |
||
62 | */ |
||
63 | public function __construct( |
||
72 | |||
73 | /** |
||
74 | * Download all remote storages into all local storages. |
||
75 | * This will overwrite your local copy. |
||
76 | */ |
||
77 | public function download() |
||
83 | |||
84 | /** |
||
85 | * Synchronize translations with remote. |
||
86 | */ |
||
87 | public function sync($direction = self::DIRECTION_DOWN) |
||
102 | |||
103 | /** |
||
104 | * Download and merge all translations from remote storages down to your local storages. |
||
105 | * Only the local storages will be changed. |
||
106 | */ |
||
107 | public function mergeDown() |
||
120 | |||
121 | /** |
||
122 | * Upload and merge all translations from local storages up to your remote storages. |
||
123 | * Only the remote storages will be changed. |
||
124 | * |
||
125 | * This will overwrite your remote copy. |
||
126 | */ |
||
127 | public function mergeUp() |
||
138 | |||
139 | /** |
||
140 | * Get the very latest version we know of a message. First look at the remote storage |
||
141 | * fall back on the local ones. |
||
142 | * |
||
143 | * @param string $locale |
||
144 | * @param string $domain |
||
145 | * @param string $key |
||
146 | * |
||
147 | * @return null|Message |
||
148 | */ |
||
149 | public function syncAndFetchMessage($locale, $domain, $key) |
||
164 | |||
165 | /** |
||
166 | * Try to get a translation from all the storages, start looking in the first |
||
167 | * local storage and then move on to the remote storages. |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function get($locale, $domain, $key) |
||
181 | |||
182 | /** |
||
183 | * @param Storage[] $storages |
||
184 | * @param string $locale |
||
185 | * @param string $domain |
||
186 | * @param string $key |
||
187 | * |
||
188 | * @return null|Message |
||
189 | */ |
||
190 | private function getFromStorages(array $storages, $locale, $domain, $key) |
||
201 | |||
202 | /** |
||
203 | * Create all configured storages with this message. This will not overwrite |
||
204 | * existing message. |
||
205 | * |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function create(Message $message) |
||
222 | |||
223 | /** |
||
224 | * Update all configured storages with this message. If messages does not exist |
||
225 | * it will be created. |
||
226 | * |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | public function update(Message $message) |
||
235 | |||
236 | /** |
||
237 | * @param Storage[] $storages |
||
238 | * @param Message $message |
||
239 | */ |
||
240 | private function updateStorages(array $storages, Message $message) |
||
251 | |||
252 | /** |
||
253 | * Delete the message form all storages. |
||
254 | * |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function delete($locale, $domain, $key) |
||
266 | |||
267 | /** |
||
268 | * @param Storage $localStorage |
||
269 | * |
||
270 | * @return StorageService |
||
271 | */ |
||
272 | public function addLocalStorage(Storage $localStorage) |
||
278 | |||
279 | /** |
||
280 | * @param Storage $remoteStorages |
||
|
|||
281 | * |
||
282 | * @return StorageService |
||
283 | */ |
||
284 | public function addRemoteStorage(Storage $remoteStorage) |
||
290 | |||
291 | /** |
||
292 | * Download catalogues from all storages. |
||
293 | * |
||
294 | * @return MessageCatalogue[] |
||
295 | */ |
||
296 | private function doDownload() |
||
310 | } |
||
311 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.