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 AbstractBrowserBindingService 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 AbstractBrowserBindingService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | abstract class AbstractBrowserBindingService implements LinkAccessInterface |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var BindingSessionInterface |
||
| 47 | */ |
||
| 48 | protected $session; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | protected $succinct; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var CmisBindingsHelper |
||
| 57 | */ |
||
| 58 | protected $cmisBindingsHelper; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var DateTimeFormat |
||
| 62 | */ |
||
| 63 | protected $dateTimeFormat; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param BindingSessionInterface $session |
||
| 67 | * @param CmisBindingsHelper|null $cmisBindingsHelper |
||
| 68 | */ |
||
| 69 | 168 | public function __construct(BindingSessionInterface $session, $cmisBindingsHelper = null) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Set cmis binding helper property |
||
| 77 | * |
||
| 78 | * @param CmisBindingsHelper|null $cmisBindingsHelper The cmis binding helper that should be defined. |
||
| 79 | * If <code>null</code> is given a new instance of CmisBindingsHelper will be created. |
||
| 80 | */ |
||
| 81 | 168 | protected function setCmisBindingsHelper($cmisBindingsHelper = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get the url for an object |
||
| 88 | * |
||
| 89 | * @param string $repositoryId |
||
| 90 | * @param string $objectId |
||
| 91 | * @param string|null $selector |
||
| 92 | * @throws CmisConnectionException |
||
| 93 | * @throws CmisObjectNotFoundException |
||
| 94 | * @return Url |
||
| 95 | */ |
||
| 96 | 2 | View Code Duplication | protected function getObjectUrl($repositoryId, $objectId, $selector = null) |
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the repository URL cache or creates a new cache if it doesn't |
||
| 121 | * exist. |
||
| 122 | * |
||
| 123 | * @return RepositoryUrlCache |
||
| 124 | */ |
||
| 125 | 2 | protected function getRepositoryUrlCache() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get current session |
||
| 138 | * |
||
| 139 | * @return BindingSessionInterface |
||
| 140 | */ |
||
| 141 | 66 | public function getSession() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Sets the current session. |
||
| 148 | * |
||
| 149 | * @param BindingSessionInterface $session |
||
| 150 | */ |
||
| 151 | 168 | protected function setSession(BindingSessionInterface $session) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Retrieves the the repository info objects. |
||
| 162 | * |
||
| 163 | * @param string|null $repositoryId |
||
| 164 | * @throws CmisConnectionException |
||
| 165 | * @return RepositoryInfo[] Returns ALL Repository Infos that are available and not just the one requested by id. |
||
| 166 | */ |
||
| 167 | 8 | protected function getRepositoriesInternal($repositoryId = null) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the service URL of this session. |
||
| 231 | * |
||
| 232 | * @return string|null |
||
| 233 | */ |
||
| 234 | 2 | protected function getServiceUrl() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Do a get request for the given url |
||
| 246 | * |
||
| 247 | * @param Url $url |
||
| 248 | * @return Response |
||
| 249 | * @throws CmisBaseException an more specific exception of this type could be thrown. For more details see |
||
| 250 | * @see AbstractBrowserBindingService::convertStatusCode() |
||
| 251 | */ |
||
| 252 | 3 | protected function read(Url $url) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Get a HTTP Invoker instance |
||
| 276 | * |
||
| 277 | * @return Client |
||
| 278 | */ |
||
| 279 | 4 | protected function getHttpInvoker() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Converts an error message or a HTTP status code into an Exception. |
||
| 289 | * |
||
| 290 | * @see http://docs.oasis-open.org/cmis/CMIS/v1.1/os/CMIS-v1.1-os.html#x1-551021r549 |
||
| 291 | * |
||
| 292 | * @param integer $code |
||
| 293 | * @param string $message |
||
| 294 | * @param null|\Exception $exception |
||
| 295 | * @return CmisBaseException |
||
| 296 | */ |
||
| 297 | 26 | protected function convertStatusCode($code, $message, \Exception $exception = null) |
|
| 350 | |||
| 351 | // ---- helpers ---- |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns JSON Converter instance |
||
| 355 | * |
||
| 356 | * @return \Dkd\PhpCmis\Converter\JsonConverter |
||
| 357 | */ |
||
| 358 | 65 | protected function getJsonConverter() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Generate url for a given path of a given repository. |
||
| 365 | * |
||
| 366 | * @param string $repositoryId |
||
| 367 | * @param string $path |
||
| 368 | * @param string|null $selector |
||
| 369 | * @throws CmisConnectionException |
||
| 370 | * @throws CmisObjectNotFoundException |
||
| 371 | * @return Url |
||
| 372 | */ |
||
| 373 | 2 | View Code Duplication | protected function getPathUrl($repositoryId, $path, $selector = null) |
| 395 | |||
| 396 | // ---- URL ---- |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get if succinct mode is used |
||
| 400 | * |
||
| 401 | * @return boolean |
||
| 402 | */ |
||
| 403 | 46 | protected function getSuccinct() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Performs a POST on an URL, checks the response code and returns the |
||
| 410 | * result. |
||
| 411 | * |
||
| 412 | * @param Url $url Request url |
||
| 413 | * @param resource|string|StreamInterface|array $content Entity body data or an array for POST fields and files |
||
| 414 | * @param array $headers Additional header options |
||
| 415 | * @return Response |
||
| 416 | * @throws CmisBaseException an more specific exception of this type could be thrown. For more details see |
||
| 417 | * @see AbstractBrowserBindingService::convertStatusCode() |
||
| 418 | */ |
||
| 419 | 2 | protected function post(Url $url, $content = array(), array $headers = array()) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Retrieves a type definition. |
||
| 439 | * |
||
| 440 | * @param string $repositoryId |
||
| 441 | * @param string $typeId |
||
| 442 | * @return TypeDefinitionInterface|null |
||
| 443 | * @throws CmisInvalidArgumentException if repository id or type id is <code>null</code> |
||
| 444 | */ |
||
| 445 | 3 | protected function getTypeDefinitionInternal($repositoryId, $typeId) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Get url for a repository |
||
| 469 | * |
||
| 470 | * @param string $repositoryId |
||
| 471 | * @param string|null $selector |
||
| 472 | * @throws CmisConnectionException |
||
| 473 | * @throws CmisObjectNotFoundException |
||
| 474 | * @return Url |
||
| 475 | */ |
||
| 476 | 2 | View Code Duplication | protected function getRepositoryUrl($repositoryId, $selector = null) |
| 497 | |||
| 498 | /** |
||
| 499 | * Converts a Properties list into an array that can be used for the CMIS request. |
||
| 500 | * |
||
| 501 | * @param PropertiesInterface $properties |
||
| 502 | * @return array Example <code> |
||
| 503 | * array('propertyId' => array(0 => 'myId'), 'propertyValue' => array(0 => 'valueOfMyId')) |
||
| 504 | * </code> |
||
| 505 | */ |
||
| 506 | 13 | protected function convertPropertiesToQueryArray(PropertiesInterface $properties) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Converts values to a format that can be used for the CMIS Browser binding request. |
||
| 543 | * |
||
| 544 | * @param mixed $value |
||
| 545 | * @return mixed |
||
| 546 | */ |
||
| 547 | 13 | protected function convertPropertyValueToSimpleType($value) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Converts a Access Control list into an array that can be used for the CMIS request |
||
| 562 | * |
||
| 563 | * @param AclInterface $acl |
||
| 564 | * @param string $principalControl one of principal ace constants |
||
| 565 | * CONTROL_ADD_ACE_PRINCIPAL or CONTROL_REMOVE_ACE_PRINCIPAL |
||
| 566 | * @param string $permissionControl one of permission ace constants |
||
| 567 | * CONTROL_REMOVE_ACE_PRINCIPAL or CONTROL_REMOVE_ACE_PERMISSION |
||
| 568 | * @return array Example <code> |
||
| 569 | * array('addACEPrincipal' => array(0 => 'principalId'), |
||
| 570 | * 'addACEPermission' => array(0 => array(0 => 'permissonValue'))) |
||
| 571 | * </code> |
||
| 572 | */ |
||
| 573 | 7 | protected function convertAclToQueryArray(AclInterface $acl, $principalControl, $permissionControl) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Converts a policies array into an array that can be used for the CMIS request |
||
| 599 | * |
||
| 600 | * @param string[] $policies A list of policy string representations |
||
| 601 | * @return array |
||
| 602 | */ |
||
| 603 | 10 | protected function convertPolicyIdArrayToQueryArray(array $policies) |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Returns the date time format |
||
| 618 | * |
||
| 619 | * @return DateTimeFormat |
||
| 620 | */ |
||
| 621 | 32 | public function getDateTimeFormat() |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Sets the date time format |
||
| 628 | * |
||
| 629 | * @param DateTimeFormat $dateTimeFormat |
||
| 630 | */ |
||
| 631 | 1 | public function setDateTimeFormat(DateTimeFormat $dateTimeFormat) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Appends policies parameters to url |
||
| 638 | * |
||
| 639 | * @param Url $url |
||
| 640 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
| 641 | */ |
||
| 642 | protected function appendPoliciesToUrl(Url $url, array $policies) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Appends addAces parameters to url |
||
| 651 | * |
||
| 652 | * @param Url $url |
||
| 653 | * @param AclInterface|null $addAces A list of ACEs |
||
| 654 | */ |
||
| 655 | View Code Duplication | protected function appendAddAcesToUrl(Url $url, AclInterface $addAces = null) |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Appends removeAces parameters to url |
||
| 670 | * |
||
| 671 | * @param Url $url |
||
| 672 | * @param AclInterface|null $removeAces A list of ACEs |
||
| 673 | */ |
||
| 674 | View Code Duplication | protected function appendRemoveAcesToUrl(Url $url, AclInterface $removeAces = null) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Gets the content link from the cache if it is there or loads it into the |
||
| 689 | * cache if it is not there. |
||
| 690 | * |
||
| 691 | * @param string $repositoryId |
||
| 692 | * @param string $documentId |
||
| 693 | * @return string|null |
||
| 694 | */ |
||
| 695 | public function loadContentLink($repositoryId, $documentId) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Gets a rendition content link from the cache if it is there or loads it |
||
| 703 | * into the cache if it is not there. |
||
| 704 | * |
||
| 705 | * @param string $repositoryId |
||
| 706 | * @param string $documentId |
||
| 707 | * @param string $streamId |
||
| 708 | * @return string|null |
||
| 709 | */ |
||
| 710 | public function loadRenditionContentLink($repositoryId, $documentId, $streamId) |
||
| 719 | } |
||
| 720 |