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() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @NoCSRFRequired |
||
| 146 | * @PublicPage |
||
| 147 | * |
||
| 148 | * @param string $shareWith identifier of the user or group |
||
| 149 | * to share the resource with |
||
| 150 | * @param string $name name of the shared resource |
||
| 151 | * @param string $description share description (optional) |
||
| 152 | * @param string $providerId Identifier of the resource at the provider side |
||
| 153 | * @param string $owner identifier of the user that owns the resource |
||
| 154 | * @param string $ownerDisplayName display name of the owner |
||
| 155 | * @param string $sender Provider specific identifier of the user that wants |
||
| 156 | * to share the resource |
||
| 157 | * @param string $senderDisplayName Display name of the user that wants |
||
| 158 | * to share the resource |
||
| 159 | * @param string $shareType Share type ('user' is supported atm) |
||
| 160 | * @param string $resourceType only 'file' is supported atm |
||
| 161 | * @param array $protocol |
||
| 162 | * [ |
||
| 163 | * 'name' => (string) protocol name. Only 'webdav' is supported atm, |
||
| 164 | * 'options' => [ |
||
| 165 | * protocol specific options |
||
| 166 | * only `webdav` options are supported atm |
||
| 167 | * e.g. `uri`, `access_token`, `password`, `permissions` etc. |
||
| 168 | * |
||
| 169 | * For backward compatibility the webdav protocol will use |
||
| 170 | * the 'sharedSecret" as username and password |
||
| 171 | * ] |
||
| 172 | * |
||
| 173 | * @return JSONResponse |
||
| 174 | */ |
||
| 175 | public function createShare($shareWith, |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @NoCSRFRequired |
||
| 286 | * @PublicPage |
||
| 287 | * |
||
| 288 | * @param string $notificationType notification type (SHARE_REMOVED, etc) |
||
| 289 | * @param string $resourceType only 'file' is supported atm |
||
| 290 | * @param string $providerId Identifier of the resource at the provider side |
||
| 291 | * @param array $notification |
||
| 292 | * [ |
||
| 293 | * optional additional parameters, depending on the notification |
||
| 294 | * and the resource type |
||
| 295 | * ] |
||
| 296 | * |
||
| 297 | * @return JSONResponse |
||
| 298 | */ |
||
| 299 | public function processNotification($notificationType, |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get list of supported protocols |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | protected function getProtocols() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Make sure that incoming shares are enabled |
||
| 410 | * |
||
| 411 | * @return void |
||
| 412 | * |
||
| 413 | * @throws NotImplementedException |
||
| 414 | */ |
||
| 415 | protected function assertIncomingSharingEnabled() { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Make sure that outgoing shares are enabled |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | * |
||
| 428 | * @throws NotImplementedException |
||
| 429 | */ |
||
| 430 | protected function assertOutgoingSharingEnabled() { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Check if value is null or an array has any null item |
||
| 440 | * |
||
| 441 | * @param mixed $param |
||
| 442 | * |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | View Code Duplication | protected function hasNull($param) { |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Get share by id, validate it's type and token |
||
| 455 | * |
||
| 456 | * @param int $id |
||
| 457 | * @param string $sharedSecret |
||
| 458 | * |
||
| 459 | * @return IShare |
||
| 460 | * |
||
| 461 | * @throws BadRequestException |
||
| 462 | * @throws ForbiddenException |
||
| 463 | * @throws Share\Exceptions\ShareNotFound |
||
| 464 | */ |
||
| 465 | protected function getValidShare($id, $sharedSecret) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $resourceType |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | protected function isSupportedResourceType($resourceType) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $shareType |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | protected function isSupportedShareType($shareType) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param string $protocolName |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | protected function isSupportedProtocol($protocolName) { |
||
| 504 | } |
||
| 505 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.