Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Notifications 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 Notifications, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Notifications { |
||
36 | const RESPONSE_FORMAT = 'json'; // default response format for ocs calls |
||
37 | |||
38 | /** @var AddressHandler */ |
||
39 | private $addressHandler; |
||
40 | |||
41 | /** @var IClientService */ |
||
42 | private $httpClientService; |
||
43 | |||
44 | /** @var IDiscoveryService */ |
||
45 | private $discoveryService; |
||
46 | |||
47 | /** @var IJobList */ |
||
48 | private $jobList; |
||
49 | |||
50 | /** @var ICloudFederationProviderManager */ |
||
51 | private $federationProviderManager; |
||
52 | |||
53 | /** @var ICloudFederationFactory */ |
||
54 | private $cloudFederationFactory; |
||
55 | |||
56 | /** |
||
57 | * @param AddressHandler $addressHandler |
||
58 | * @param IClientService $httpClientService |
||
59 | * @param IDiscoveryService $discoveryService |
||
60 | * @param IJobList $jobList |
||
61 | * @param ICloudFederationProviderManager $federationProviderManager |
||
62 | * @param ICloudFederationFactory $cloudFederationFactory |
||
63 | */ |
||
64 | public function __construct( |
||
79 | |||
80 | /** |
||
81 | * send server-to-server share to remote server |
||
82 | * |
||
83 | * @param string $token |
||
84 | * @param string $shareWith |
||
85 | * @param string $name |
||
86 | * @param int $remote_id |
||
87 | * @param string $owner |
||
88 | * @param string $ownerFederatedId |
||
89 | * @param string $sharedBy |
||
90 | * @param string $sharedByFederatedId |
||
91 | * @return bool |
||
92 | * @throws \OC\HintException |
||
93 | * @throws \OC\ServerNotAvailableException |
||
94 | */ |
||
95 | public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId) { |
||
129 | |||
130 | /** |
||
131 | * ask owner to re-share the file with the given user |
||
132 | * |
||
133 | * @param string $token |
||
134 | * @param int $id remote Id |
||
135 | * @param int $shareId internal share Id |
||
136 | * @param string $remote remote address of the owner |
||
137 | * @param string $shareWith |
||
138 | * @param int $permission |
||
139 | * @param string $filename |
||
140 | * @return bool |
||
141 | * @throws \OC\HintException |
||
142 | * @throws \OC\ServerNotAvailableException |
||
143 | */ |
||
144 | public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission, $filename) { |
||
180 | |||
181 | /** |
||
182 | * send server-to-server unshare to remote server |
||
183 | * |
||
184 | * @param string $remote url |
||
185 | * @param int $id share id |
||
186 | * @param string $token |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function sendRemoteUnShare($remote, $id, $token) { |
||
192 | |||
193 | /** |
||
194 | * send server-to-server unshare to remote server |
||
195 | * |
||
196 | * @param string $remote url |
||
197 | * @param int $id share id |
||
198 | * @param string $token |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function sendRevokeShare($remote, $id, $token) { |
||
204 | |||
205 | /** |
||
206 | * send notification to remote server if the permissions was changed |
||
207 | * |
||
208 | * @param string $remote |
||
209 | * @param int $remoteId |
||
210 | * @param string $token |
||
211 | * @param int $permissions |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
||
217 | |||
218 | /** |
||
219 | * forward accept reShare to remote server |
||
220 | * |
||
221 | * @param string $remote |
||
222 | * @param int $remoteId |
||
223 | * @param string $token |
||
224 | */ |
||
225 | public function sendAcceptShare($remote, $remoteId, $token) { |
||
228 | |||
229 | /** |
||
230 | * forward decline reShare to remote server |
||
231 | * |
||
232 | * @param string $remote |
||
233 | * @param int $remoteId |
||
234 | * @param string $token |
||
235 | */ |
||
236 | public function sendDeclineShare($remote, $remoteId, $token) { |
||
239 | |||
240 | /** |
||
241 | * inform remote server whether server-to-server share was accepted/declined |
||
242 | * |
||
243 | * @param string $remote |
||
244 | * @param string $token |
||
245 | * @param int $remoteId Share id on the remote host |
||
246 | * @param string $action possible actions: accept, decline, unshare, revoke, permissions |
||
247 | * @param array $data |
||
248 | * @param int $try |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
||
287 | |||
288 | |||
289 | /** |
||
290 | * return current timestamp |
||
291 | * |
||
292 | * @return int |
||
293 | */ |
||
294 | protected function getTimestamp() { |
||
297 | |||
298 | /** |
||
299 | * try http post with the given protocol, if no protocol is given we pick |
||
300 | * the secure one (https) |
||
301 | * |
||
302 | * @param string $remoteDomain |
||
303 | * @param string $urlSuffix |
||
304 | * @param array $fields post parameters |
||
305 | * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions) |
||
306 | * @return array |
||
307 | * @throws \Exception |
||
308 | */ |
||
309 | protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action="share") { |
||
331 | |||
332 | /** |
||
333 | * try old federated sharing API if the OCM api doesn't work |
||
334 | * |
||
335 | * @param $remoteDomain |
||
336 | * @param $urlSuffix |
||
337 | * @param array $fields |
||
338 | * @return mixed |
||
339 | * @throws \Exception |
||
340 | */ |
||
341 | protected function tryLegacyEndPoint($remoteDomain, $urlSuffix, array $fields) { |
||
372 | |||
373 | /** |
||
374 | * send action regarding federated sharing to the remote server using the OCM API |
||
375 | * |
||
376 | * @param $remoteDomain |
||
377 | * @param $fields |
||
378 | * @param $action |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | protected function tryOCMEndPoint($remoteDomain, $fields, $action) { |
||
442 | } |
||
443 |