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) { |
||
| 130 | $ocmNotificationManager = new NotificationManager(); |
||
| 131 | $data = [ |
||
| 132 | 'shareWith' => $shareWith, |
||
| 133 | 'senderId' => $id |
||
| 134 | ]; |
||
| 135 | $ocmNotification = $ocmNotificationManager->convertToOcmFileNotification($shareId, $token, 'reshare', $data); |
||
| 136 | $ocmFields = $ocmNotification->toArray(); |
||
| 137 | |||
| 138 | $url = \rtrim( |
||
| 139 | $this->addressHandler->removeProtocolFromUrl($remote), |
||
| 140 | '/' |
||
| 141 | ); |
||
| 142 | $result = $this->tryHttpPostToShareEndpoint($url, '/notifications', $ocmFields, true); |
||
| 143 | if (isset($result['statusCode']) && $result['statusCode'] === Http::STATUS_CREATED) { |
||
| 144 | $response = \json_decode($result['result'], true); |
||
| 145 | if (\is_array($response) && isset($response['token'], $response['providerId'])) { |
||
| 146 | return [ |
||
| 147 | $response['token'], |
||
| 148 | (int) $response['providerId'] |
||
| 149 | ]; |
||
| 150 | } |
||
| 151 | return true; |
||
| 152 | } |
||
| 153 | |||
| 154 | $fields = [ |
||
| 155 | 'shareWith' => $shareWith, |
||
| 156 | 'token' => $token, |
||
| 157 | 'permission' => $permission, |
||
| 158 | 'remoteId' => $shareId |
||
| 159 | ]; |
||
| 160 | |||
| 161 | $url = $this->addressHandler->removeProtocolFromUrl($remote); |
||
| 162 | $result = $this->tryHttpPostToShareEndpoint(\rtrim($url, '/'), '/' . $id . '/reshare', $fields); |
||
| 163 | $status = \json_decode($result['result'], true); |
||
| 164 | |||
| 165 | $httpRequestSuccessful = $result['success']; |
||
| 166 | $validToken = isset($status['ocs']['data']['token']) && \is_string($status['ocs']['data']['token']); |
||
| 167 | $validRemoteId = isset($status['ocs']['data']['remoteId']); |
||
| 168 | |||
| 169 | if ($httpRequestSuccessful && $this->isOcsStatusOk($status) && $validToken && $validRemoteId) { |
||
| 170 | return [ |
||
| 171 | $status['ocs']['data']['token'], |
||
| 172 | (int)$status['ocs']['data']['remoteId'] |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 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 | * @param bool $useOcm send request to OCM endpoint instead of OCS |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | * |
||
| 317 | * @throws \Exception |
||
| 318 | */ |
||
| 319 | protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $useOcm = false) { |
||
| 365 | |||
| 366 | protected function sendOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
| 393 | |||
| 394 | protected function sendPreOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Validate ocs response - 100 or 200 means success |
||
| 418 | * |
||
| 419 | * @param array $status |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | private function isOcsStatusOk($status) { |
||
| 426 | } |
||
| 427 |
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.