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 |
||
| 46 | class CardDavBackend implements BackendInterface, SyncSupport { |
||
| 47 | |||
| 48 | /** @var Principal */ |
||
| 49 | private $principalBackend; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $dbCardsTable = 'cards'; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | private $dbCardsPropertiesTable = 'cards_properties'; |
||
| 56 | |||
| 57 | /** @var IDBConnection */ |
||
| 58 | private $db; |
||
| 59 | |||
| 60 | /** @var Backend */ |
||
| 61 | private $sharingBackend; |
||
| 62 | |||
| 63 | /** @var CappedMemoryCache Cache of card URI to db row ids */ |
||
| 64 | private $idCache; |
||
| 65 | |||
| 66 | /** @var array properties to index */ |
||
| 67 | public static $indexProperties = [ |
||
| 68 | 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
||
| 69 | 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD']; |
||
| 70 | |||
| 71 | /** @var EventDispatcherInterface */ |
||
| 72 | private $dispatcher; |
||
| 73 | /** @var bool */ |
||
| 74 | private $legacyMode; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * CardDavBackend constructor. |
||
| 78 | * |
||
| 79 | * @param IDBConnection $db |
||
| 80 | * @param Principal $principalBackend |
||
| 81 | * @param EventDispatcherInterface $dispatcher |
||
| 82 | */ |
||
| 83 | public function __construct(IDBConnection $db, |
||
| 84 | Principal $principalBackend, |
||
| 85 | EventDispatcherInterface $dispatcher = null, |
||
| 86 | $legacyMode = false) { |
||
| 87 | $this->db = $db; |
||
| 88 | $this->principalBackend = $principalBackend; |
||
| 89 | $this->dispatcher = $dispatcher; |
||
| 90 | $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
||
| 91 | $this->legacyMode = $legacyMode; |
||
| 92 | $this->idCache = new CappedMemoryCache(); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the list of address books for a specific user. |
||
| 97 | * |
||
| 98 | * Every addressbook should have the following properties: |
||
| 99 | * id - an arbitrary unique id |
||
| 100 | * uri - the 'basename' part of the url |
||
| 101 | * principaluri - Same as the passed parameter |
||
| 102 | * |
||
| 103 | * Any additional clark-notation property may be passed besides this. Some |
||
| 104 | * common ones are : |
||
| 105 | * {DAV:}displayname |
||
| 106 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
| 107 | * {http://calendarserver.org/ns/}getctag |
||
| 108 | * |
||
| 109 | * @param string $principalUri |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | function getUsersOwnAddressBooks($principalUri) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the list of address books for a specific user, including shared by other users. |
||
| 139 | * |
||
| 140 | * Every addressbook should have the following properties: |
||
| 141 | * id - an arbitrary unique id |
||
| 142 | * uri - the 'basename' part of the url |
||
| 143 | * principaluri - Same as the passed parameter |
||
| 144 | * |
||
| 145 | * Any additional clark-notation property may be passed besides this. Some |
||
| 146 | * common ones are : |
||
| 147 | * {DAV:}displayname |
||
| 148 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
| 149 | * {http://calendarserver.org/ns/}getctag |
||
| 150 | * |
||
| 151 | * @param string $principalUri |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | function getAddressBooksForUser($principalUri) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param int $addressBookId |
||
| 196 | */ |
||
| 197 | public function getAddressBookById($addressBookId) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param $addressBookUri |
||
| 223 | * @return array|null |
||
| 224 | */ |
||
| 225 | public function getAddressBooksByUri($principal, $addressBookUri) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Updates properties for an address book. |
||
| 253 | * |
||
| 254 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 255 | * To do the actual updates, you must tell this object which properties |
||
| 256 | * you're going to process with the handle() method. |
||
| 257 | * |
||
| 258 | * Calling the handle method is like telling the PropPatch object "I |
||
| 259 | * promise I can handle updating this property". |
||
| 260 | * |
||
| 261 | * Read the PropPatch documentation for more info and examples. |
||
| 262 | * |
||
| 263 | * @param string $addressBookId |
||
| 264 | * @param \Sabre\DAV\PropPatch $propPatch |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Creates a new address book |
||
| 305 | * |
||
| 306 | * @param string $principalUri |
||
| 307 | * @param string $url Just the 'basename' of the url. |
||
| 308 | * @param array $properties |
||
| 309 | * @return int |
||
| 310 | * @throws BadRequest |
||
| 311 | */ |
||
| 312 | function createAddressBook($principalUri, $url, array $properties) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Deletes an entire addressbook and all its contents |
||
| 359 | * |
||
| 360 | * @param mixed $addressBookId |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | function deleteAddressBook($addressBookId) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Returns all cards for a specific addressbook id. |
||
| 390 | * |
||
| 391 | * This method should return the following properties for each card: |
||
| 392 | * * carddata - raw vcard data |
||
| 393 | * * uri - Some unique url |
||
| 394 | * * lastmodified - A unix timestamp |
||
| 395 | * |
||
| 396 | * It's recommended to also return the following properties: |
||
| 397 | * * etag - A unique etag. This must change every time the card changes. |
||
| 398 | * * size - The size of the card in bytes. |
||
| 399 | * |
||
| 400 | * If these last two properties are provided, less time will be spent |
||
| 401 | * calculating them. If they are specified, you can also ommit carddata. |
||
| 402 | * This may speed up certain requests, especially with large cards. |
||
| 403 | * |
||
| 404 | * @param mixed $addressBookId |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | function getCards($addressBookId) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns a specific card. |
||
| 428 | * |
||
| 429 | * The same set of properties must be returned as with getCards. The only |
||
| 430 | * exception is that 'carddata' is absolutely required. |
||
| 431 | * |
||
| 432 | * If the card does not exist, you must return false. |
||
| 433 | * |
||
| 434 | * @param mixed $addressBookId |
||
| 435 | * @param string $cardUri |
||
| 436 | * @return array|false |
||
| 437 | */ |
||
| 438 | function getCard($addressBookId, $cardUri) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Returns a list of cards. |
||
| 459 | * |
||
| 460 | * This method should work identical to getCard, but instead return all the |
||
| 461 | * cards in the list as an array. |
||
| 462 | * |
||
| 463 | * If the backend supports this, it may allow for some speed-ups. |
||
| 464 | * |
||
| 465 | * @param mixed $addressBookId |
||
| 466 | * @param string[] $uris |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | function getMultipleCards($addressBookId, array $uris) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Creates a new card. |
||
| 500 | * |
||
| 501 | * The addressbook id will be passed as the first argument. This is the |
||
| 502 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 503 | * |
||
| 504 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 505 | * cardData argument is the vcard body, and is passed as a string. |
||
| 506 | * |
||
| 507 | * It is possible to return an ETag from this method. This ETag is for the |
||
| 508 | * newly created resource, and must be enclosed with double quotes (that |
||
| 509 | * is, the string itself must contain the double quotes). |
||
| 510 | * |
||
| 511 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 512 | * subsequent GET request on the same card does not have the same body, |
||
| 513 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 514 | * confused. |
||
| 515 | * |
||
| 516 | * If you don't return an ETag, you can just return null. |
||
| 517 | * |
||
| 518 | * @param mixed $addressBookId |
||
| 519 | * @param string $cardUri |
||
| 520 | * @param string $cardData |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | function createCard($addressBookId, $cardUri, $cardData) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Updates a card. |
||
| 557 | * |
||
| 558 | * The addressbook id will be passed as the first argument. This is the |
||
| 559 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 560 | * |
||
| 561 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 562 | * cardData argument is the vcard body, and is passed as a string. |
||
| 563 | * |
||
| 564 | * It is possible to return an ETag from this method. This ETag should |
||
| 565 | * match that of the updated resource, and must be enclosed with double |
||
| 566 | * quotes (that is: the string itself must contain the actual quotes). |
||
| 567 | * |
||
| 568 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 569 | * subsequent GET request on the same card does not have the same body, |
||
| 570 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 571 | * confused. |
||
| 572 | * |
||
| 573 | * If you don't return an ETag, you can just return null. |
||
| 574 | * |
||
| 575 | * @param mixed $addressBookId |
||
| 576 | * @param string $cardUri |
||
| 577 | * @param string $cardData |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | function updateCard($addressBookId, $cardUri, $cardData) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Deletes a card |
||
| 609 | * |
||
| 610 | * @param mixed $addressBookId |
||
| 611 | * @param string $cardUri |
||
| 612 | * @return bool |
||
| 613 | */ |
||
| 614 | function deleteCard($addressBookId, $cardUri) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * The getChanges method returns all the changes that have happened, since |
||
| 647 | * the specified syncToken in the specified address book. |
||
| 648 | * |
||
| 649 | * This function should return an array, such as the following: |
||
| 650 | * |
||
| 651 | * [ |
||
| 652 | * 'syncToken' => 'The current synctoken', |
||
| 653 | * 'added' => [ |
||
| 654 | * 'new.txt', |
||
| 655 | * ], |
||
| 656 | * 'modified' => [ |
||
| 657 | * 'modified.txt', |
||
| 658 | * ], |
||
| 659 | * 'deleted' => [ |
||
| 660 | * 'foo.php.bak', |
||
| 661 | * 'old.txt' |
||
| 662 | * ] |
||
| 663 | * ]; |
||
| 664 | * |
||
| 665 | * The returned syncToken property should reflect the *current* syncToken |
||
| 666 | * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
||
| 667 | * property. This is needed here too, to ensure the operation is atomic. |
||
| 668 | * |
||
| 669 | * If the $syncToken argument is specified as null, this is an initial |
||
| 670 | * sync, and all members should be reported. |
||
| 671 | * |
||
| 672 | * The modified property is an array of nodenames that have changed since |
||
| 673 | * the last token. |
||
| 674 | * |
||
| 675 | * The deleted property is an array with nodenames, that have been deleted |
||
| 676 | * from collection. |
||
| 677 | * |
||
| 678 | * The $syncLevel argument is basically the 'depth' of the report. If it's |
||
| 679 | * 1, you only have to report changes that happened only directly in |
||
| 680 | * immediate descendants. If it's 2, it should also include changes from |
||
| 681 | * the nodes below the child collections. (grandchildren) |
||
| 682 | * |
||
| 683 | * The $limit argument allows a client to specify how many results should |
||
| 684 | * be returned at most. If the limit is not specified, it should be treated |
||
| 685 | * as infinite. |
||
| 686 | * |
||
| 687 | * If the limit (infinite or not) is higher than you're willing to return, |
||
| 688 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
||
| 689 | * |
||
| 690 | * If the syncToken is expired (due to data cleanup) or unknown, you must |
||
| 691 | * return null. |
||
| 692 | * |
||
| 693 | * The limit is 'suggestive'. You are free to ignore it. |
||
| 694 | * |
||
| 695 | * @param string $addressBookId |
||
| 696 | * @param string $syncToken |
||
| 697 | * @param int $syncLevel |
||
| 698 | * @param int $limit |
||
| 699 | * @return array |
||
| 700 | */ |
||
| 701 | View Code Duplication | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Adds a change record to the addressbookchanges table. |
||
| 765 | * |
||
| 766 | * @param mixed $addressBookId |
||
| 767 | * @param string $objectUri |
||
| 768 | * @param int $operation 1 = add, 2 = modify, 3 = delete |
||
| 769 | * @return void |
||
| 770 | */ |
||
| 771 | View Code Duplication | protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 785 | |||
| 786 | private function readBlob($cardData) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @param IShareable $shareable |
||
| 796 | * @param string[] $add |
||
| 797 | * @param string[] $remove |
||
| 798 | */ |
||
| 799 | public function updateShares(IShareable $shareable, $add, $remove) { |
||
| 802 | |||
| 803 | /** |
||
| 804 | * search contact |
||
| 805 | * |
||
| 806 | * @param int $addressBookId |
||
| 807 | * @param string $pattern which should match within the $searchProperties |
||
| 808 | * @param array $searchProperties defines the properties within the query pattern should match |
||
| 809 | * @param int $limit |
||
| 810 | * @param int $offset |
||
| 811 | * @return array an array of contacts which are arrays of key-value-pairs |
||
| 812 | */ |
||
| 813 | public function search($addressBookId, $pattern, $searchProperties, $limit = 100, $offset = 0) { |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @param int $bookId |
||
| 846 | * @param string $name |
||
| 847 | * @return array |
||
| 848 | */ |
||
| 849 | public function collectCardProperties($bookId, $name) { |
||
| 862 | |||
| 863 | /** |
||
| 864 | * get URI from a given contact |
||
| 865 | * |
||
| 866 | * @param int $id |
||
| 867 | * @return string |
||
| 868 | */ |
||
| 869 | View Code Duplication | public function getCardUri($id) { |
|
| 885 | |||
| 886 | /** |
||
| 887 | * return contact with the given URI |
||
| 888 | * |
||
| 889 | * @param int $addressBookId |
||
| 890 | * @param string $uri |
||
| 891 | * @returns array |
||
| 892 | */ |
||
| 893 | public function getContact($addressBookId, $uri) { |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Returns the list of people whom this address book is shared with. |
||
| 912 | * |
||
| 913 | * Every element in this array should have the following properties: |
||
| 914 | * * href - Often a mailto: address |
||
| 915 | * * commonName - Optional, for example a first + last name |
||
| 916 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
||
| 917 | * * readOnly - boolean |
||
| 918 | * * summary - Optional, a description for the share |
||
| 919 | * |
||
| 920 | * @return array |
||
| 921 | */ |
||
| 922 | public function getShares($addressBookId) { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * update properties table |
||
| 928 | * |
||
| 929 | * @param int $addressBookId |
||
| 930 | * @param string $cardUri |
||
| 931 | * @param string $vCardSerialized |
||
| 932 | */ |
||
| 933 | protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * read vCard data into a vCard object |
||
| 971 | * |
||
| 972 | * @param string $cardData |
||
| 973 | * @return VCard |
||
| 974 | */ |
||
| 975 | protected function readCard($cardData) { |
||
| 978 | |||
| 979 | /** |
||
| 980 | * delete all properties from a given card |
||
| 981 | * |
||
| 982 | * @param int $addressBookId |
||
| 983 | * @param int $cardId |
||
| 984 | */ |
||
| 985 | protected function purgeProperties($addressBookId, $cardId) { |
||
| 992 | |||
| 993 | /** |
||
| 994 | * get ID from a given contact |
||
| 995 | * |
||
| 996 | * @param int $addressBookId |
||
| 997 | * @param string $uri |
||
| 998 | * @return int |
||
| 999 | */ |
||
| 1000 | protected function getCardId($addressBookId, $uri) { |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * For shared address books the sharee is set in the ACL of the address book |
||
| 1024 | * @param $addressBookId |
||
| 1025 | * @param $acl |
||
| 1026 | * @return array |
||
| 1027 | */ |
||
| 1028 | public function applyShareAcl($addressBookId, $acl) { |
||
| 1031 | |||
| 1032 | View Code Duplication | private function convertPrincipal($principalUri, $toV2 = null) { |
|
| 1043 | } |
||
| 1044 |
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: