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 |
||
| 41 | abstract class AbstractBrowserBindingService implements LinkAccessInterface |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var BindingSessionInterface |
||
| 45 | */ |
||
| 46 | protected $session; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $succinct; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var CmisBindingsHelper |
||
| 55 | */ |
||
| 56 | protected $cmisBindingsHelper; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var DateTimeFormat |
||
| 60 | */ |
||
| 61 | protected $dateTimeFormat; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param BindingSessionInterface $session |
||
| 65 | * @param CmisBindingsHelper|null $cmisBindingsHelper |
||
| 66 | */ |
||
| 67 | 168 | public function __construct(BindingSessionInterface $session, $cmisBindingsHelper = null) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Set cmis binding helper property |
||
| 75 | * |
||
| 76 | * @param CmisBindingsHelper|null $cmisBindingsHelper The cmis binding helper that should be defined. |
||
| 77 | * If <code>null</code> is given a new instance of CmisBindingsHelper will be created. |
||
| 78 | */ |
||
| 79 | 168 | protected function setCmisBindingsHelper($cmisBindingsHelper = null) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get the url for an object |
||
| 86 | * |
||
| 87 | * @param string $repositoryId |
||
| 88 | * @param string $objectId |
||
| 89 | * @param string|null $selector |
||
| 90 | * @throws CmisConnectionException |
||
| 91 | * @throws CmisObjectNotFoundException |
||
| 92 | * @return Url |
||
| 93 | */ |
||
| 94 | 2 | View Code Duplication | protected function getObjectUrl($repositoryId, $objectId, $selector = null) |
| 116 | |||
| 117 | /** |
||
| 118 | * Returns the repository URL cache or creates a new cache if it doesn't |
||
| 119 | * exist. |
||
| 120 | * |
||
| 121 | * @return RepositoryUrlCache |
||
| 122 | */ |
||
| 123 | 2 | protected function getRepositoryUrlCache() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get current session |
||
| 136 | * |
||
| 137 | * @return BindingSessionInterface |
||
| 138 | */ |
||
| 139 | 63 | public function getSession() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Sets the current session. |
||
| 146 | * |
||
| 147 | * @param BindingSessionInterface $session |
||
| 148 | */ |
||
| 149 | 168 | protected function setSession(BindingSessionInterface $session) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Retrieves the the repository info objects. |
||
| 160 | * |
||
| 161 | * @param string|null $repositoryId |
||
| 162 | * @throws CmisConnectionException |
||
| 163 | * @return RepositoryInfo[] Returns ALL Repository Infos that are available and not just the one requested by id. |
||
| 164 | */ |
||
| 165 | 8 | protected function getRepositoriesInternal($repositoryId = null) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the service URL of this session. |
||
| 226 | * |
||
| 227 | * @return string|null |
||
| 228 | */ |
||
| 229 | 2 | protected function getServiceUrl() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Do a get request for the given url |
||
| 241 | * |
||
| 242 | * @param Url $url |
||
| 243 | * @return \GuzzleHttp\Message\Response |
||
| 244 | * @throws CmisBaseException an more specific exception of this type could be thrown. For more details see |
||
| 245 | * @see AbstractBrowserBindingService::convertStatusCode() |
||
| 246 | */ |
||
| 247 | 3 | protected function read(Url $url) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Get a HTTP Invoker instance |
||
| 271 | * |
||
| 272 | * @return \GuzzleHttp\Client |
||
| 273 | */ |
||
| 274 | 4 | protected function getHttpInvoker() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Converts an error message or a HTTP status code into an Exception. |
||
| 284 | * |
||
| 285 | * @see http://docs.oasis-open.org/cmis/CMIS/v1.1/os/CMIS-v1.1-os.html#x1-551021r549 |
||
| 286 | * |
||
| 287 | * @param integer $code |
||
| 288 | * @param string $message |
||
| 289 | * @param null|\Exception $exception |
||
| 290 | * @return CmisBaseException |
||
| 291 | */ |
||
| 292 | 26 | protected function convertStatusCode($code, $message, \Exception $exception = null) |
|
| 345 | |||
| 346 | // ---- helpers ---- |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns JSON Converter instance |
||
| 350 | * |
||
| 351 | * @return \Dkd\PhpCmis\Converter\JsonConverter |
||
| 352 | */ |
||
| 353 | 62 | protected function getJsonConverter() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Generate url for a given path of a given repository. |
||
| 360 | * |
||
| 361 | * @param string $repositoryId |
||
| 362 | * @param string $path |
||
| 363 | * @param string|null $selector |
||
| 364 | * @throws CmisConnectionException |
||
| 365 | * @throws CmisObjectNotFoundException |
||
| 366 | * @return Url |
||
| 367 | */ |
||
| 368 | 2 | View Code Duplication | protected function getPathUrl($repositoryId, $path, $selector = null) |
| 390 | |||
| 391 | // ---- URL ---- |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get if succinct mode is used |
||
| 395 | * |
||
| 396 | * @return boolean |
||
| 397 | */ |
||
| 398 | 46 | protected function getSuccinct() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Performs a POST on an URL, checks the response code and returns the |
||
| 405 | * result. |
||
| 406 | * |
||
| 407 | * @param Url $url Request url |
||
| 408 | * @param resource|string|StreamInterface|array $content Entity body data or an array for POST fields and files |
||
| 409 | * @param array $headers Additional header options |
||
| 410 | * @return \GuzzleHttp\Message\Response |
||
| 411 | * @throws CmisBaseException an more specific exception of this type could be thrown. For more details see |
||
| 412 | * @see AbstractBrowserBindingService::convertStatusCode() |
||
| 413 | */ |
||
| 414 | 2 | protected function post(Url $url, $content = array(), array $headers = array()) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Retrieves a type definition. |
||
| 434 | * |
||
| 435 | * @param string $repositoryId |
||
| 436 | * @param string $typeId |
||
| 437 | * @return TypeDefinitionInterface|null |
||
| 438 | * @throws CmisInvalidArgumentException if repository id or type id is <code>null</code> |
||
| 439 | */ |
||
| 440 | 3 | protected function getTypeDefinitionInternal($repositoryId, $typeId) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Get url for a repository |
||
| 459 | * |
||
| 460 | * @param string $repositoryId |
||
| 461 | * @param string|null $selector |
||
| 462 | * @throws CmisConnectionException |
||
| 463 | * @throws CmisObjectNotFoundException |
||
| 464 | * @return Url |
||
| 465 | */ |
||
| 466 | 2 | View Code Duplication | protected function getRepositoryUrl($repositoryId, $selector = null) |
| 487 | |||
| 488 | /** |
||
| 489 | * Converts a Properties list into an array that can be used for the CMIS request. |
||
| 490 | * |
||
| 491 | * @param PropertiesInterface $properties |
||
| 492 | * @return array Example <code> |
||
| 493 | * array('propertyId' => array(0 => 'myId'), 'propertyValue' => array(0 => 'valueOfMyId')) |
||
| 494 | * </code> |
||
| 495 | */ |
||
| 496 | 13 | protected function convertPropertiesToQueryArray(PropertiesInterface $properties) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Converts values to a format that can be used for the CMIS Browser binding request. |
||
| 533 | * |
||
| 534 | * @param mixed $value |
||
| 535 | * @return mixed |
||
| 536 | */ |
||
| 537 | 13 | protected function convertPropertyValueToSimpleType($value) |
|
| 538 | { |
||
| 539 | 13 | if ($value instanceof \DateTime) { |
|
| 540 | // CMIS expects a timestamp in milliseconds |
||
| 541 | 1 | $value = $value->getTimestamp() * 1000; |
|
| 542 | 13 | } elseif (is_bool($value)) { |
|
| 543 | // Booleans must be represented in string form since request will fail if cast to integer |
||
| 544 | 1 | $value = $value ? 'true' : 'false'; |
|
| 545 | 1 | } |
|
| 546 | |||
| 547 | 13 | return $value; |
|
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Converts a Access Control list into an array that can be used for the CMIS request |
||
| 552 | * |
||
| 553 | * @param AclInterface $acl |
||
| 554 | * @param string $principalControl one of principal ace constants |
||
| 555 | * CONTROL_ADD_ACE_PRINCIPAL or CONTROL_REMOVE_ACE_PRINCIPAL |
||
| 556 | * @param string $permissionControl one of permission ace constants |
||
| 557 | * CONTROL_REMOVE_ACE_PRINCIPAL or CONTROL_REMOVE_ACE_PERMISSION |
||
| 558 | * @return array Example <code> |
||
| 559 | * array('addACEPrincipal' => array(0 => 'principalId'), |
||
| 560 | * 'addACEPermission' => array(0 => array(0 => 'permissonValue'))) |
||
| 561 | * </code> |
||
| 562 | */ |
||
| 563 | 7 | protected function convertAclToQueryArray(AclInterface $acl, $principalControl, $permissionControl) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Converts a policies array into an array that can be used for the CMIS request |
||
| 589 | * |
||
| 590 | * @param string[] $policies A list of policy string representations |
||
| 591 | * @return array |
||
| 592 | */ |
||
| 593 | 6 | protected function convertPolicyIdArrayToQueryArray(array $policies) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Returns the date time format |
||
| 608 | * |
||
| 609 | * @return DateTimeFormat |
||
| 610 | */ |
||
| 611 | 32 | public function getDateTimeFormat() |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Sets the date time format |
||
| 618 | * |
||
| 619 | * @param DateTimeFormat $dateTimeFormat |
||
| 620 | */ |
||
| 621 | 1 | public function setDateTimeFormat(DateTimeFormat $dateTimeFormat) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Appends policies parameters to url |
||
| 628 | * |
||
| 629 | * @param Url $url |
||
| 630 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
| 631 | */ |
||
| 632 | 9 | protected function appendPoliciesToUrl(Url $url, array $policies) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Appends addAces parameters to url |
||
| 641 | * |
||
| 642 | * @param Url $url |
||
| 643 | * @param AclInterface|null $addAces A list of ACEs |
||
| 644 | */ |
||
| 645 | 9 | View Code Duplication | protected function appendAddAcesToUrl(Url $url, AclInterface $addAces = null) |
| 657 | |||
| 658 | /** |
||
| 659 | * Appends removeAces parameters to url |
||
| 660 | * |
||
| 661 | * @param Url $url |
||
| 662 | * @param AclInterface|null $removeAces A list of ACEs |
||
| 663 | */ |
||
| 664 | 9 | View Code Duplication | protected function appendRemoveAcesToUrl(Url $url, AclInterface $removeAces = null) |
| 676 | |||
| 677 | /** |
||
| 678 | * Gets the content link from the cache if it is there or loads it into the |
||
| 679 | * cache if it is not there. |
||
| 680 | * |
||
| 681 | * @param string $repositoryId |
||
| 682 | * @param string $documentId |
||
| 683 | * @return string|null |
||
| 684 | */ |
||
| 685 | public function loadContentLink($repositoryId, $documentId) |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Gets a rendition content link from the cache if it is there or loads it |
||
| 693 | * into the cache if it is not there. |
||
| 694 | * |
||
| 695 | * @param string $repositoryId |
||
| 696 | * @param string $documentId |
||
| 697 | * @param string $streamId |
||
| 698 | * @return string|null |
||
| 699 | */ |
||
| 700 | public function loadRenditionContentLink($repositoryId, $documentId, $streamId) |
||
| 709 | } |
||
| 710 |