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 CardDavBackend 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 CardDavBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class CardDavBackend implements BackendInterface, SyncSupport { |
||
| 50 | |||
| 51 | const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
||
| 52 | const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
||
| 53 | |||
| 54 | /** @var Principal */ |
||
| 55 | private $principalBackend; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $dbCardsTable = 'cards'; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | private $dbCardsPropertiesTable = 'cards_properties'; |
||
| 62 | |||
| 63 | /** @var IDBConnection */ |
||
| 64 | private $db; |
||
| 65 | |||
| 66 | /** @var Backend */ |
||
| 67 | private $sharingBackend; |
||
| 68 | |||
| 69 | /** @var array properties to index */ |
||
| 70 | public static $indexProperties = array( |
||
| 71 | 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
||
| 72 | 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string[] Map of uid => display name |
||
| 76 | */ |
||
| 77 | protected $userDisplayNames; |
||
| 78 | |||
| 79 | /** @var IUserManager */ |
||
| 80 | private $userManager; |
||
| 81 | |||
| 82 | /** @var EventDispatcherInterface */ |
||
| 83 | private $dispatcher; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * CardDavBackend constructor. |
||
| 87 | * |
||
| 88 | * @param IDBConnection $db |
||
| 89 | * @param Principal $principalBackend |
||
| 90 | * @param IUserManager $userManager |
||
| 91 | * @param EventDispatcherInterface $dispatcher |
||
| 92 | */ |
||
| 93 | public function __construct(IDBConnection $db, |
||
| 94 | Principal $principalBackend, |
||
| 95 | IUserManager $userManager, |
||
| 96 | EventDispatcherInterface $dispatcher = null) { |
||
| 97 | $this->db = $db; |
||
| 98 | $this->principalBackend = $principalBackend; |
||
| 99 | $this->userManager = $userManager; |
||
| 100 | $this->dispatcher = $dispatcher; |
||
| 101 | $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Return the number of address books for a principal |
||
| 106 | * |
||
| 107 | * @param $principalUri |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function getAddressBooksForUserCount($principalUri) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the list of address books for a specific user. |
||
| 122 | * |
||
| 123 | * Every addressbook should have the following properties: |
||
| 124 | * id - an arbitrary unique id |
||
| 125 | * uri - the 'basename' part of the url |
||
| 126 | * principaluri - Same as the passed parameter |
||
| 127 | * |
||
| 128 | * Any additional clark-notation property may be passed besides this. Some |
||
| 129 | * common ones are : |
||
| 130 | * {DAV:}displayname |
||
| 131 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
| 132 | * {http://calendarserver.org/ns/}getctag |
||
| 133 | * |
||
| 134 | * @param string $principalUri |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | function getAddressBooksForUser($principalUri) { |
||
| 218 | |||
| 219 | public function getUsersOwnAddressBooks($principalUri) { |
||
| 246 | |||
| 247 | View Code Duplication | private function getUserDisplayName($uid) { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param int $addressBookId |
||
| 263 | */ |
||
| 264 | public function getAddressBookById($addressBookId) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $addressBookUri |
||
| 294 | * @return array|null |
||
| 295 | */ |
||
| 296 | public function getAddressBooksByUri($principal, $addressBookUri) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Updates properties for an address book. |
||
| 328 | * |
||
| 329 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 330 | * To do the actual updates, you must tell this object which properties |
||
| 331 | * you're going to process with the handle() method. |
||
| 332 | * |
||
| 333 | * Calling the handle method is like telling the PropPatch object "I |
||
| 334 | * promise I can handle updating this property". |
||
| 335 | * |
||
| 336 | * Read the PropPatch documentation for more info and examples. |
||
| 337 | * |
||
| 338 | * @param string $addressBookId |
||
| 339 | * @param \Sabre\DAV\PropPatch $propPatch |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Creates a new address book |
||
| 380 | * |
||
| 381 | * @param string $principalUri |
||
| 382 | * @param string $url Just the 'basename' of the url. |
||
| 383 | * @param array $properties |
||
| 384 | * @return int |
||
| 385 | * @throws BadRequest |
||
| 386 | */ |
||
| 387 | function createAddressBook($principalUri, $url, array $properties) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Deletes an entire addressbook and all its contents |
||
| 434 | * |
||
| 435 | * @param mixed $addressBookId |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | function deleteAddressBook($addressBookId) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns all cards for a specific addressbook id. |
||
| 465 | * |
||
| 466 | * This method should return the following properties for each card: |
||
| 467 | * * carddata - raw vcard data |
||
| 468 | * * uri - Some unique url |
||
| 469 | * * lastmodified - A unix timestamp |
||
| 470 | * |
||
| 471 | * It's recommended to also return the following properties: |
||
| 472 | * * etag - A unique etag. This must change every time the card changes. |
||
| 473 | * * size - The size of the card in bytes. |
||
| 474 | * |
||
| 475 | * If these last two properties are provided, less time will be spent |
||
| 476 | * calculating them. If they are specified, you can also ommit carddata. |
||
| 477 | * This may speed up certain requests, especially with large cards. |
||
| 478 | * |
||
| 479 | * @param mixed $addressBookId |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | function getCards($addressBookId) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns a specific card. |
||
| 503 | * |
||
| 504 | * The same set of properties must be returned as with getCards. The only |
||
| 505 | * exception is that 'carddata' is absolutely required. |
||
| 506 | * |
||
| 507 | * If the card does not exist, you must return false. |
||
| 508 | * |
||
| 509 | * @param mixed $addressBookId |
||
| 510 | * @param string $cardUri |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | function getCard($addressBookId, $cardUri) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Returns a list of cards. |
||
| 534 | * |
||
| 535 | * This method should work identical to getCard, but instead return all the |
||
| 536 | * cards in the list as an array. |
||
| 537 | * |
||
| 538 | * If the backend supports this, it may allow for some speed-ups. |
||
| 539 | * |
||
| 540 | * @param mixed $addressBookId |
||
| 541 | * @param string[] $uris |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | function getMultipleCards($addressBookId, array $uris) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Creates a new card. |
||
| 574 | * |
||
| 575 | * The addressbook id will be passed as the first argument. This is the |
||
| 576 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 577 | * |
||
| 578 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 579 | * cardData argument is the vcard body, and is passed as a string. |
||
| 580 | * |
||
| 581 | * It is possible to return an ETag from this method. This ETag is for the |
||
| 582 | * newly created resource, and must be enclosed with double quotes (that |
||
| 583 | * is, the string itself must contain the double quotes). |
||
| 584 | * |
||
| 585 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 586 | * subsequent GET request on the same card does not have the same body, |
||
| 587 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 588 | * confused. |
||
| 589 | * |
||
| 590 | * If you don't return an ETag, you can just return null. |
||
| 591 | * |
||
| 592 | * @param mixed $addressBookId |
||
| 593 | * @param string $cardUri |
||
| 594 | * @param string $cardData |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | function createCard($addressBookId, $cardUri, $cardData) { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Updates a card. |
||
| 628 | * |
||
| 629 | * The addressbook id will be passed as the first argument. This is the |
||
| 630 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 631 | * |
||
| 632 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 633 | * cardData argument is the vcard body, and is passed as a string. |
||
| 634 | * |
||
| 635 | * It is possible to return an ETag from this method. This ETag should |
||
| 636 | * match that of the updated resource, and must be enclosed with double |
||
| 637 | * quotes (that is: the string itself must contain the actual quotes). |
||
| 638 | * |
||
| 639 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 640 | * subsequent GET request on the same card does not have the same body, |
||
| 641 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 642 | * confused. |
||
| 643 | * |
||
| 644 | * If you don't return an ETag, you can just return null. |
||
| 645 | * |
||
| 646 | * @param mixed $addressBookId |
||
| 647 | * @param string $cardUri |
||
| 648 | * @param string $cardData |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | function updateCard($addressBookId, $cardUri, $cardData) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Deletes a card |
||
| 680 | * |
||
| 681 | * @param mixed $addressBookId |
||
| 682 | * @param string $cardUri |
||
| 683 | * @return bool |
||
| 684 | */ |
||
| 685 | function deleteCard($addressBookId, $cardUri) { |
||
| 715 | |||
| 716 | /** |
||
| 717 | * The getChanges method returns all the changes that have happened, since |
||
| 718 | * the specified syncToken in the specified address book. |
||
| 719 | * |
||
| 720 | * This function should return an array, such as the following: |
||
| 721 | * |
||
| 722 | * [ |
||
| 723 | * 'syncToken' => 'The current synctoken', |
||
| 724 | * 'added' => [ |
||
| 725 | * 'new.txt', |
||
| 726 | * ], |
||
| 727 | * 'modified' => [ |
||
| 728 | * 'modified.txt', |
||
| 729 | * ], |
||
| 730 | * 'deleted' => [ |
||
| 731 | * 'foo.php.bak', |
||
| 732 | * 'old.txt' |
||
| 733 | * ] |
||
| 734 | * ]; |
||
| 735 | * |
||
| 736 | * The returned syncToken property should reflect the *current* syncToken |
||
| 737 | * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
||
| 738 | * property. This is needed here too, to ensure the operation is atomic. |
||
| 739 | * |
||
| 740 | * If the $syncToken argument is specified as null, this is an initial |
||
| 741 | * sync, and all members should be reported. |
||
| 742 | * |
||
| 743 | * The modified property is an array of nodenames that have changed since |
||
| 744 | * the last token. |
||
| 745 | * |
||
| 746 | * The deleted property is an array with nodenames, that have been deleted |
||
| 747 | * from collection. |
||
| 748 | * |
||
| 749 | * The $syncLevel argument is basically the 'depth' of the report. If it's |
||
| 750 | * 1, you only have to report changes that happened only directly in |
||
| 751 | * immediate descendants. If it's 2, it should also include changes from |
||
| 752 | * the nodes below the child collections. (grandchildren) |
||
| 753 | * |
||
| 754 | * The $limit argument allows a client to specify how many results should |
||
| 755 | * be returned at most. If the limit is not specified, it should be treated |
||
| 756 | * as infinite. |
||
| 757 | * |
||
| 758 | * If the limit (infinite or not) is higher than you're willing to return, |
||
| 759 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
||
| 760 | * |
||
| 761 | * If the syncToken is expired (due to data cleanup) or unknown, you must |
||
| 762 | * return null. |
||
| 763 | * |
||
| 764 | * The limit is 'suggestive'. You are free to ignore it. |
||
| 765 | * |
||
| 766 | * @param string $addressBookId |
||
| 767 | * @param string $syncToken |
||
| 768 | * @param int $syncLevel |
||
| 769 | * @param int $limit |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | View Code Duplication | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Adds a change record to the addressbookchanges table. |
||
| 836 | * |
||
| 837 | * @param mixed $addressBookId |
||
| 838 | * @param string $objectUri |
||
| 839 | * @param int $operation 1 = add, 2 = modify, 3 = delete |
||
| 840 | * @return void |
||
| 841 | */ |
||
| 842 | View Code Duplication | protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 856 | |||
| 857 | private function readBlob($cardData) { |
||
| 864 | |||
| 865 | /** |
||
| 866 | * @param IShareable $shareable |
||
| 867 | * @param string[] $add |
||
| 868 | * @param string[] $remove |
||
| 869 | */ |
||
| 870 | public function updateShares(IShareable $shareable, $add, $remove) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * search contact |
||
| 876 | * |
||
| 877 | * @param int $addressBookId |
||
| 878 | * @param string $pattern which should match within the $searchProperties |
||
| 879 | * @param array $searchProperties defines the properties within the query pattern should match |
||
| 880 | * @return array an array of contacts which are arrays of key-value-pairs |
||
| 881 | */ |
||
| 882 | public function search($addressBookId, $pattern, $searchProperties) { |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @param int $bookId |
||
| 912 | * @param string $name |
||
| 913 | * @return array |
||
| 914 | */ |
||
| 915 | public function collectCardProperties($bookId, $name) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * get URI from a given contact |
||
| 931 | * |
||
| 932 | * @param int $id |
||
| 933 | * @return string |
||
| 934 | */ |
||
| 935 | public function getCardUri($id) { |
||
| 951 | |||
| 952 | /** |
||
| 953 | * return contact with the given URI |
||
| 954 | * |
||
| 955 | * @param int $addressBookId |
||
| 956 | * @param string $uri |
||
| 957 | * @returns array |
||
| 958 | */ |
||
| 959 | public function getContact($addressBookId, $uri) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Returns the list of people whom this address book is shared with. |
||
| 978 | * |
||
| 979 | * Every element in this array should have the following properties: |
||
| 980 | * * href - Often a mailto: address |
||
| 981 | * * commonName - Optional, for example a first + last name |
||
| 982 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
||
| 983 | * * readOnly - boolean |
||
| 984 | * * summary - Optional, a description for the share |
||
| 985 | * |
||
| 986 | * @return array |
||
| 987 | */ |
||
| 988 | public function getShares($addressBookId) { |
||
| 991 | |||
| 992 | /** |
||
| 993 | * update properties table |
||
| 994 | * |
||
| 995 | * @param int $addressBookId |
||
| 996 | * @param string $cardUri |
||
| 997 | * @param string $vCardSerialized |
||
| 998 | */ |
||
| 999 | protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * read vCard data into a vCard object |
||
| 1037 | * |
||
| 1038 | * @param string $cardData |
||
| 1039 | * @return VCard |
||
| 1040 | */ |
||
| 1041 | protected function readCard($cardData) { |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * delete all properties from a given card |
||
| 1047 | * |
||
| 1048 | * @param int $addressBookId |
||
| 1049 | * @param int $cardId |
||
| 1050 | */ |
||
| 1051 | protected function purgeProperties($addressBookId, $cardId) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * get ID from a given contact |
||
| 1061 | * |
||
| 1062 | * @param int $addressBookId |
||
| 1063 | * @param string $uri |
||
| 1064 | * @return int |
||
| 1065 | */ |
||
| 1066 | protected function getCardId($addressBookId, $uri) { |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * For shared address books the sharee is set in the ACL of the address book |
||
| 1085 | * @param $addressBookId |
||
| 1086 | * @param $acl |
||
| 1087 | * @return array |
||
| 1088 | */ |
||
| 1089 | public function applyShareAcl($addressBookId, $acl) { |
||
| 1092 | |||
| 1093 | View Code Duplication | private function convertPrincipal($principalUri, $toV2) { |
|
| 1103 | |||
| 1104 | View Code Duplication | private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1118 | } |
||
| 1119 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: