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 |
||
34 | class Notifications { |
||
35 | const RESPONSE_FORMAT = 'json'; // default response format for ocs calls |
||
36 | |||
37 | /** @var AddressHandler */ |
||
38 | private $addressHandler; |
||
39 | |||
40 | /** @var IClientService */ |
||
41 | private $httpClientService; |
||
42 | |||
43 | /** @var DiscoveryManager */ |
||
44 | private $discoveryManager; |
||
45 | 1 | ||
46 | /** @var IJobList */ |
||
47 | private $jobList; |
||
48 | |||
49 | 1 | /** @var IConfig */ |
|
50 | 1 | private $config; |
|
51 | 1 | ||
52 | /** |
||
53 | * @param AddressHandler $addressHandler |
||
54 | * @param IClientService $httpClientService |
||
55 | * @param DiscoveryManager $discoveryManager |
||
56 | * @param IJobList $jobList |
||
57 | * @param IConfig $config |
||
58 | */ |
||
59 | public function __construct( |
||
72 | |||
73 | /** |
||
74 | * send server-to-server share to remote server |
||
75 | * |
||
76 | * @param Address $shareWithAddress |
||
77 | * @param Address $ownerAddress |
||
78 | * @param Address $sharedByAddress |
||
79 | * @param string $token |
||
80 | * @param string $shareWith |
||
|
|||
81 | * @param string $name |
||
82 | * @param int $remote_id |
||
83 | * |
||
84 | * @return bool |
||
85 | * |
||
86 | * @throws \OC\HintException |
||
87 | * @throws \OC\ServerNotAvailableException |
||
88 | */ |
||
89 | public function sendRemoteShare(Address $shareWithAddress, |
||
116 | |||
117 | /** |
||
118 | * ask owner to re-share the file with the given user |
||
119 | * |
||
120 | * @param string $token |
||
121 | * @param int $id remote Id |
||
122 | * @param int $shareId internal share Id |
||
123 | * @param string $remote remote address of the owner |
||
124 | * @param string $shareWith |
||
125 | * @param int $permission |
||
126 | * @return bool |
||
127 | * @throws \Exception |
||
128 | */ |
||
129 | public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) { |
||
178 | |||
179 | /** |
||
180 | * send server-to-server unshare to remote server |
||
181 | * |
||
182 | * @param string $remote url |
||
183 | * @param int $id share id |
||
184 | * @param string $token |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function sendRemoteUnShare($remote, $id, $token) { |
||
190 | |||
191 | /** |
||
192 | * send server-to-server unshare to remote server |
||
193 | * |
||
194 | * @param string $remote url |
||
195 | * @param int $id share id |
||
196 | * @param string $token |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function sendRevokeShare($remote, $id, $token) { |
||
202 | |||
203 | /** |
||
204 | * send notification to remote server if the permissions was changed |
||
205 | * |
||
206 | * @param string $remote |
||
207 | * @param int $remoteId |
||
208 | * @param string $token |
||
209 | * @param int $permissions |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
||
215 | |||
216 | /** |
||
217 | * forward accept reShare to remote server |
||
218 | * |
||
219 | * @param string $remote |
||
220 | * @param int $remoteId |
||
221 | * @param string $token |
||
222 | */ |
||
223 | public function sendAcceptShare($remote, $remoteId, $token) { |
||
226 | |||
227 | /** |
||
228 | * forward decline reShare to remote server |
||
229 | * |
||
230 | * @param string $remote |
||
231 | * @param int $remoteId |
||
232 | * @param string $token |
||
233 | */ |
||
234 | public function sendDeclineShare($remote, $remoteId, $token) { |
||
237 | |||
238 | /** |
||
239 | * inform remote server whether server-to-server share was accepted/declined |
||
240 | * |
||
241 | * @param string $remote |
||
242 | * @param int $remoteId Share id on the remote host |
||
243 | * @param string $token |
||
244 | * @param string $action possible actions: |
||
245 | * accept, decline, unshare, revoke, permissions |
||
246 | * @param array $data |
||
247 | * @param int $try |
||
248 | * |
||
249 | * @return boolean |
||
250 | * |
||
251 | * @throws \Exception |
||
252 | */ |
||
253 | public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
||
297 | |||
298 | /** |
||
299 | * return current timestamp |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | protected function getTimestamp() { |
||
306 | |||
307 | /** |
||
308 | * try http post first with https and then with http as a fallback |
||
309 | * |
||
310 | * @param string $remoteDomain |
||
311 | * @param string $urlSuffix |
||
312 | * @param array $fields post parameters |
||
313 | * @return array |
||
314 | * @throws \Exception |
||
315 | */ |
||
316 | protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $useOcm = false) { |
||
362 | |||
363 | protected function sendOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
390 | |||
391 | protected function sendPreOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
412 | |||
413 | /** |
||
414 | * Validate ocs response - 100 or 200 means success |
||
415 | * |
||
416 | * @param array $status |
||
417 | * |
||
418 | * @return bool |
||
419 | */ |
||
420 | private function isOcsStatusOk($status) { |
||
423 | } |
||
424 |
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.