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) |
||
161 | |||
162 | /** |
||
163 | * Try to get a translation from all the storages, start looking in the first |
||
164 | * local storage and then move on to the remote storages. |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function get($locale, $domain, $key) |
||
178 | |||
179 | /** |
||
180 | * @param Storage[] $storages |
||
181 | * @param string $locale |
||
182 | * @param string $domain |
||
183 | * @param string $key |
||
184 | * |
||
185 | * @return null|Message |
||
186 | */ |
||
187 | private function getFromStorages($storages, $locale, $domain, $key) |
||
198 | |||
199 | /** |
||
200 | * Create all configured storages with this message. This will not overwrite |
||
201 | * existing message. |
||
202 | * |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function create(Message $message) |
||
211 | |||
212 | /** |
||
213 | * @param Storage[] $storages |
||
214 | * @param Message $message |
||
215 | */ |
||
216 | private function createStorages($storages, Message $message) |
||
227 | |||
228 | /** |
||
229 | * Update all configured storages with this message. If messages does not exist |
||
230 | * it will be created. |
||
231 | * |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function update(Message $message) |
||
240 | |||
241 | /** |
||
242 | * @param Storage[] $storages |
||
243 | * @param Message $message |
||
244 | */ |
||
245 | private function updateStorages($storages, Message $message) |
||
256 | |||
257 | /** |
||
258 | * Delete the message form all storages. |
||
259 | * |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function delete($locale, $domain, $key) |
||
268 | |||
269 | /** |
||
270 | * @param Storage[] $storages |
||
271 | * @param string $locale |
||
272 | * @param string $domain |
||
273 | * @param string $key |
||
274 | */ |
||
275 | private function deleteFromStorages($storages, $locale, $domain, $key) |
||
281 | |||
282 | /** |
||
283 | * @param Storage $localStorage |
||
284 | * |
||
285 | * @return StorageService |
||
286 | */ |
||
287 | public function addLocalStorage(Storage $localStorage) |
||
293 | |||
294 | /** |
||
295 | * @param Storage $remoteStorages |
||
296 | * |
||
297 | * @return StorageService |
||
298 | */ |
||
299 | public function addRemoteStorage(Storage $remoteStorage) |
||
305 | |||
306 | /** |
||
307 | * Download catalogues from all storages. |
||
308 | * |
||
309 | * @return MessageCatalogue[] |
||
310 | */ |
||
311 | private function doDownload() |
||
325 | } |
||
326 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.