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 OcmController 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 OcmController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class OcmController extends Controller { |
||
| 50 | const API_VERSION = '1.0-proposal1'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var FederatedShareProvider |
||
| 54 | */ |
||
| 55 | private $federatedShareProvider; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var IURLGenerator |
||
| 59 | */ |
||
| 60 | protected $urlGenerator; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var IAppManager |
||
| 64 | */ |
||
| 65 | private $appManager; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var IUserManager |
||
| 69 | */ |
||
| 70 | protected $userManager; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var AddressHandler |
||
| 74 | */ |
||
| 75 | protected $addressHandler; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var FedShareManager |
||
| 79 | */ |
||
| 80 | protected $fedShareManager; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ILogger |
||
| 84 | */ |
||
| 85 | protected $logger; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * OcmController constructor. |
||
| 89 | * |
||
| 90 | * @param string $appName |
||
| 91 | * @param IRequest $request |
||
| 92 | * @param FederatedShareProvider $federatedShareProvider |
||
| 93 | * @param IURLGenerator $urlGenerator |
||
| 94 | * @param IAppManager $appManager |
||
| 95 | * @param IUserManager $userManager |
||
| 96 | * @param AddressHandler $addressHandler |
||
| 97 | * @param FedShareManager $fedShareManager |
||
| 98 | * @param ILogger $logger |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function __construct($appName, |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @NoCSRFRequired |
||
| 123 | * @PublicPage |
||
| 124 | * |
||
| 125 | * EndPoint discovery |
||
| 126 | * Responds to /ocm-provider/ requests |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function discovery() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @NoCSRFRequired |
||
| 147 | * @PublicPage |
||
| 148 | * |
||
| 149 | * @param string $shareWith identifier of the user or group |
||
| 150 | * to share the resource with |
||
| 151 | * @param string $name name of the shared resource |
||
| 152 | * @param string $description share description (optional) |
||
| 153 | * @param string $providerId Identifier of the resource at the provider side |
||
| 154 | * @param string $owner identifier of the user that owns the resource |
||
| 155 | * @param string $ownerDisplayName display name of the owner |
||
| 156 | * @param string $sender Provider specific identifier of the user that wants |
||
| 157 | * to share the resource |
||
| 158 | * @param string $senderDisplayName Display name of the user that wants |
||
| 159 | * to share the resource |
||
| 160 | * @param string $shareType Share type ('user' is supported atm) |
||
| 161 | * @param string $resourceType only 'file' is supported atm |
||
| 162 | * @param array $protocol |
||
| 163 | * [ |
||
| 164 | * 'name' => (string) protocol name. Only 'webdav' is supported atm, |
||
| 165 | * 'options' => [ |
||
| 166 | * protocol specific options |
||
| 167 | * only `webdav` options are supported atm |
||
| 168 | * e.g. `uri`, `access_token`, `password`, `permissions` etc. |
||
| 169 | * |
||
| 170 | * For backward compatibility the webdav protocol will use |
||
| 171 | * the 'sharedSecret" as username and password |
||
| 172 | * ] |
||
| 173 | * |
||
| 174 | * @return JSONResponse |
||
| 175 | */ |
||
| 176 | public function createShare($shareWith, |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @NoCSRFRequired |
||
| 287 | * @PublicPage |
||
| 288 | * |
||
| 289 | * @param string $notificationType notification type (SHARE_REMOVED, etc) |
||
| 290 | * @param string $resourceType only 'file' is supported atm |
||
| 291 | * @param string $providerId Identifier of the resource at the provider side |
||
| 292 | * @param array $notification |
||
| 293 | * [ |
||
| 294 | * optional additional parameters, depending on the notification |
||
| 295 | * and the resource type |
||
| 296 | * ] |
||
| 297 | * |
||
| 298 | * @return JSONResponse |
||
| 299 | */ |
||
| 300 | public function processNotification($notificationType, |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get list of supported protocols |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | protected function getProtocols() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Make sure that incoming shares are enabled |
||
| 418 | * |
||
| 419 | * @return void |
||
| 420 | * |
||
| 421 | * @throws NotImplementedException |
||
| 422 | */ |
||
| 423 | protected function assertIncomingSharingEnabled() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Make sure that outgoing shares are enabled |
||
| 433 | * |
||
| 434 | * @return void |
||
| 435 | * |
||
| 436 | * @throws NotImplementedException |
||
| 437 | */ |
||
| 438 | protected function assertOutgoingSharingEnabled() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Check if value is null or an array has any null item |
||
| 448 | * |
||
| 449 | * @param mixed $param |
||
| 450 | * |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | View Code Duplication | protected function hasNull($param) { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get share by id, validate its type and token |
||
| 463 | * |
||
| 464 | * @param int $id |
||
| 465 | * @param string $sharedSecret |
||
| 466 | * |
||
| 467 | * @return IShare |
||
| 468 | * |
||
| 469 | * @throws BadRequestException |
||
| 470 | * @throws ForbiddenException |
||
| 471 | * @throws Share\Exceptions\ShareNotFound |
||
| 472 | */ |
||
| 473 | protected function getValidShare($id, $sharedSecret) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $resourceType |
||
| 487 | * |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | protected function isSupportedResourceType($resourceType) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $shareType |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | protected function isSupportedShareType($shareType) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param string $protocolName |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | protected function isSupportedProtocol($protocolName) { |
||
| 512 | } |
||
| 513 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.