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 GroupPrincipalBackend $groupPrincipalBackend |
||
| 82 | * @param EventDispatcherInterface $dispatcher |
||
| 83 | * @param bool $legacyMode |
||
| 84 | */ |
||
| 85 | public function __construct(IDBConnection $db, |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the list of address books for a specific user. |
||
| 100 | * |
||
| 101 | * Every addressbook should have the following properties: |
||
| 102 | * id - an arbitrary unique id |
||
| 103 | * uri - the 'basename' part of the url |
||
| 104 | * principaluri - Same as the passed parameter |
||
| 105 | * |
||
| 106 | * Any additional clark-notation property may be passed besides this. Some |
||
| 107 | * common ones are : |
||
| 108 | * {DAV:}displayname |
||
| 109 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
| 110 | * {http://calendarserver.org/ns/}getctag |
||
| 111 | * |
||
| 112 | * @param string $principalUri |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getUsersOwnAddressBooks($principalUri) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the list of address books for a specific user, including shared by other users. |
||
| 142 | * |
||
| 143 | * Every addressbook should have the following properties: |
||
| 144 | * id - an arbitrary unique id |
||
| 145 | * uri - the 'basename' part of the url |
||
| 146 | * principaluri - Same as the passed parameter |
||
| 147 | * |
||
| 148 | * Any additional clark-notation property may be passed besides this. Some |
||
| 149 | * common ones are : |
||
| 150 | * {DAV:}displayname |
||
| 151 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
| 152 | * {http://calendarserver.org/ns/}getctag |
||
| 153 | * |
||
| 154 | * @param string $principalUri |
||
| 155 | * @return array |
||
| 156 | * @throws \Sabre\DAV\Exception |
||
| 157 | */ |
||
| 158 | public function getAddressBooksForUser($principalUri) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $addressBookId |
||
| 200 | */ |
||
| 201 | public function getAddressBookById($addressBookId) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param $addressBookUri |
||
| 227 | * @return array|null |
||
| 228 | */ |
||
| 229 | public function getAddressBooksByUri($principal, $addressBookUri) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Updates properties for an address book. |
||
| 257 | * |
||
| 258 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 259 | * To do the actual updates, you must tell this object which properties |
||
| 260 | * you're going to process with the handle() method. |
||
| 261 | * |
||
| 262 | * Calling the handle method is like telling the PropPatch object "I |
||
| 263 | * promise I can handle updating this property". |
||
| 264 | * |
||
| 265 | * Read the PropPatch documentation for more info and examples. |
||
| 266 | * |
||
| 267 | * @param string $addressBookId |
||
| 268 | * @param \Sabre\DAV\PropPatch $propPatch |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Creates a new address book |
||
| 306 | * |
||
| 307 | * @param string $principalUri |
||
| 308 | * @param string $url Just the 'basename' of the url. |
||
| 309 | * @param array $properties |
||
| 310 | * @return int |
||
| 311 | * @throws \BadMethodCallException |
||
| 312 | * @throws BadRequest |
||
| 313 | */ |
||
| 314 | public 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 | public function deleteAddressBook($addressBookId) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns all cards for a specific addressbook id. |
||
| 389 | * |
||
| 390 | * This method should return the following properties for each card: |
||
| 391 | * * carddata - raw vcard data |
||
| 392 | * * uri - Some unique url |
||
| 393 | * * lastmodified - A unix timestamp |
||
| 394 | * |
||
| 395 | * It's recommended to also return the following properties: |
||
| 396 | * * etag - A unique etag. This must change every time the card changes. |
||
| 397 | * * size - The size of the card in bytes. |
||
| 398 | * |
||
| 399 | * If these last two properties are provided, less time will be spent |
||
| 400 | * calculating them. If they are specified, you can also ommit carddata. |
||
| 401 | * This may speed up certain requests, especially with large cards. |
||
| 402 | * |
||
| 403 | * @param mixed $addressBookId |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function getCards($addressBookId) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns a specific card. |
||
| 427 | * |
||
| 428 | * The same set of properties must be returned as with getCards. The only |
||
| 429 | * exception is that 'carddata' is absolutely required. |
||
| 430 | * |
||
| 431 | * If the card does not exist, you must return false. |
||
| 432 | * |
||
| 433 | * @param mixed $addressBookId |
||
| 434 | * @param string $cardUri |
||
| 435 | * @return array|false |
||
| 436 | */ |
||
| 437 | public function getCard($addressBookId, $cardUri) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns a list of cards. |
||
| 458 | * |
||
| 459 | * This method should work identical to getCard, but instead return all the |
||
| 460 | * cards in the list as an array. |
||
| 461 | * |
||
| 462 | * If the backend supports this, it may allow for some speed-ups. |
||
| 463 | * |
||
| 464 | * @param mixed $addressBookId |
||
| 465 | * @param string[] $uris |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | public 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 | * @throws \BadMethodCallException |
||
| 523 | */ |
||
| 524 | public function createCard($addressBookId, $cardUri, $cardData) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Updates a card. |
||
| 558 | * |
||
| 559 | * The addressbook id will be passed as the first argument. This is the |
||
| 560 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 561 | * |
||
| 562 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 563 | * cardData argument is the vcard body, and is passed as a string. |
||
| 564 | * |
||
| 565 | * It is possible to return an ETag from this method. This ETag should |
||
| 566 | * match that of the updated resource, and must be enclosed with double |
||
| 567 | * quotes (that is: the string itself must contain the actual quotes). |
||
| 568 | * |
||
| 569 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 570 | * subsequent GET request on the same card does not have the same body, |
||
| 571 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 572 | * confused. |
||
| 573 | * |
||
| 574 | * If you don't return an ETag, you can just return null. |
||
| 575 | * |
||
| 576 | * @param mixed $addressBookId |
||
| 577 | * @param string $cardUri |
||
| 578 | * @param string $cardData |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | public 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 | public 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 | public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Adds a change record to the addressbookchanges table. |
||
| 759 | * |
||
| 760 | * @param mixed $addressBookId |
||
| 761 | * @param string $objectUri |
||
| 762 | * @param int $operation 1 = add, 2 = modify, 3 = delete |
||
| 763 | * @return void |
||
| 764 | */ |
||
| 765 | protected function addChange($addressBookId, $objectUri, $operation) { |
||
| 779 | |||
| 780 | private function readBlob($cardData) { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @param IShareable $shareable |
||
| 790 | * @param string[] $add |
||
| 791 | * @param string[] $remove |
||
| 792 | */ |
||
| 793 | public function updateShares(IShareable $shareable, $add, $remove) { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * search contact |
||
| 799 | * |
||
| 800 | * @param int $addressBookId |
||
| 801 | * @param string $pattern which should match within the $searchProperties |
||
| 802 | * @param array $searchProperties defines the properties within the query pattern should match |
||
| 803 | * @param int $limit |
||
| 804 | * @param int $offset |
||
| 805 | * @return array an array of contacts which are arrays of key-value-pairs |
||
| 806 | */ |
||
| 807 | public function search($addressBookId, $pattern, $searchProperties, $limit = 100, $offset = 0) { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @param int $bookId |
||
| 840 | * @param string $name |
||
| 841 | * @return array |
||
| 842 | */ |
||
| 843 | public function collectCardProperties($bookId, $name) { |
||
| 856 | |||
| 857 | /** |
||
| 858 | * get URI from a given contact |
||
| 859 | * |
||
| 860 | * @param int $id |
||
| 861 | * @return string |
||
| 862 | * @throws \InvalidArgumentException |
||
| 863 | */ |
||
| 864 | View Code Duplication | public function getCardUri($id) { |
|
| 880 | |||
| 881 | /** |
||
| 882 | * return contact with the given URI |
||
| 883 | * |
||
| 884 | * @param int $addressBookId |
||
| 885 | * @param string $uri |
||
| 886 | * @return array |
||
| 887 | */ |
||
| 888 | public function getContact($addressBookId, $uri) { |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Returns the list of people whom this address book is shared with. |
||
| 907 | * |
||
| 908 | * Every element in this array should have the following properties: |
||
| 909 | * * href - Often a mailto: address |
||
| 910 | * * commonName - Optional, for example a first + last name |
||
| 911 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
||
| 912 | * * readOnly - boolean |
||
| 913 | * * summary - Optional, a description for the share |
||
| 914 | * |
||
| 915 | * @return array |
||
| 916 | */ |
||
| 917 | public function getShares($addressBookId) { |
||
| 920 | |||
| 921 | /** |
||
| 922 | * update properties table |
||
| 923 | * |
||
| 924 | * @param int $addressBookId |
||
| 925 | * @param string $cardUri |
||
| 926 | * @param string $vCardSerialized |
||
| 927 | */ |
||
| 928 | protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
||
| 963 | |||
| 964 | /** |
||
| 965 | * read vCard data into a vCard object |
||
| 966 | * |
||
| 967 | * @param string $cardData |
||
| 968 | * @return VCard |
||
| 969 | */ |
||
| 970 | protected function readCard($cardData) { |
||
| 973 | |||
| 974 | /** |
||
| 975 | * delete all properties from a given card |
||
| 976 | * |
||
| 977 | * @param int $addressBookId |
||
| 978 | * @param int $cardId |
||
| 979 | */ |
||
| 980 | protected function purgeProperties($addressBookId, $cardId) { |
||
| 987 | |||
| 988 | /** |
||
| 989 | * get ID from a given contact |
||
| 990 | * |
||
| 991 | * @param int $addressBookId |
||
| 992 | * @param string $uri |
||
| 993 | * @return int |
||
| 994 | * @throws \InvalidArgumentException |
||
| 995 | */ |
||
| 996 | protected function getCardId($addressBookId, $uri) { |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * For shared address books the sharee is set in the ACL of the address book |
||
| 1020 | * @param $addressBookId |
||
| 1021 | * @param $acl |
||
| 1022 | * @return array |
||
| 1023 | */ |
||
| 1024 | public function applyShareAcl($addressBookId, $acl) { |
||
| 1027 | |||
| 1028 | private function convertPrincipal($principalUri, $toV2 = null) { |
||
| 1039 | } |
||
| 1040 |
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: