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 | /** @var Principal */ |
||
| 52 | private $principalBackend; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | private $dbCardsTable = 'cards'; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $dbCardsPropertiesTable = 'cards_properties'; |
||
| 59 | |||
| 60 | /** @var IDBConnection */ |
||
| 61 | private $db; |
||
| 62 | |||
| 63 | /** @var Backend */ |
||
| 64 | private $sharingBackend; |
||
| 65 | |||
| 66 | /** @var array properties to index */ |
||
| 67 | public static $indexProperties = array( |
||
| 68 | 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
||
| 69 | 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string[] Map of uid => display name |
||
| 73 | */ |
||
| 74 | protected $userDisplayNames; |
||
| 75 | |||
| 76 | /** @var IUserManager */ |
||
| 77 | private $userManager; |
||
| 78 | |||
| 79 | /** @var EventDispatcherInterface */ |
||
| 80 | private $dispatcher; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * CardDavBackend constructor. |
||
| 84 | * |
||
| 85 | * @param IDBConnection $db |
||
| 86 | * @param Principal $principalBackend |
||
| 87 | * @param IUserManager $userManager |
||
| 88 | * @param EventDispatcherInterface $dispatcher |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function __construct(IDBConnection $db, |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the list of address books for a specific user. |
||
| 103 | * |
||
| 104 | * Every addressbook should have the following properties: |
||
| 105 | * id - an arbitrary unique id |
||
| 106 | * uri - the 'basename' part of the url |
||
| 107 | * principaluri - Same as the passed parameter |
||
| 108 | * |
||
| 109 | * Any additional clark-notation property may be passed besides this. Some |
||
| 110 | * common ones are : |
||
| 111 | * {DAV:}displayname |
||
| 112 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
| 113 | * {http://calendarserver.org/ns/}getctag |
||
| 114 | * |
||
| 115 | * @param string $principalUri |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | function getAddressBooksForUser($principalUri) { |
||
| 178 | |||
| 179 | View Code Duplication | private function getUserDisplayName($uid) { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param int $addressBookId |
||
| 195 | */ |
||
| 196 | public function getAddressBookById($addressBookId) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $addressBookUri |
||
| 222 | * @return array|null |
||
| 223 | */ |
||
| 224 | public function getAddressBooksByUri($principal, $addressBookUri) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Updates properties for an address book. |
||
| 252 | * |
||
| 253 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 254 | * To do the actual updates, you must tell this object which properties |
||
| 255 | * you're going to process with the handle() method. |
||
| 256 | * |
||
| 257 | * Calling the handle method is like telling the PropPatch object "I |
||
| 258 | * promise I can handle updating this property". |
||
| 259 | * |
||
| 260 | * Read the PropPatch documentation for more info and examples. |
||
| 261 | * |
||
| 262 | * @param string $addressBookId |
||
| 263 | * @param \Sabre\DAV\PropPatch $propPatch |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Creates a new address book |
||
| 304 | * |
||
| 305 | * @param string $principalUri |
||
| 306 | * @param string $url Just the 'basename' of the url. |
||
| 307 | * @param array $properties |
||
| 308 | * @return int |
||
| 309 | * @throws BadRequest |
||
| 310 | */ |
||
| 311 | function createAddressBook($principalUri, $url, array $properties) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Deletes an entire addressbook and all its contents |
||
| 358 | * |
||
| 359 | * @param mixed $addressBookId |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | 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 | 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 |
||
| 436 | */ |
||
| 437 | 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 | function getMultipleCards($addressBookId, array $uris) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Creates a new card. |
||
| 491 | * |
||
| 492 | * The addressbook id will be passed as the first argument. This is the |
||
| 493 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 494 | * |
||
| 495 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 496 | * cardData argument is the vcard body, and is passed as a string. |
||
| 497 | * |
||
| 498 | * It is possible to return an ETag from this method. This ETag is for the |
||
| 499 | * newly created resource, and must be enclosed with double quotes (that |
||
| 500 | * is, the string itself must contain the double quotes). |
||
| 501 | * |
||
| 502 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 503 | * subsequent GET request on the same card does not have the same body, |
||
| 504 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 505 | * confused. |
||
| 506 | * |
||
| 507 | * If you don't return an ETag, you can just return null. |
||
| 508 | * |
||
| 509 | * @param mixed $addressBookId |
||
| 510 | * @param string $cardUri |
||
| 511 | * @param string $cardData |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | function createCard($addressBookId, $cardUri, $cardData) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Updates a card. |
||
| 545 | * |
||
| 546 | * The addressbook id will be passed as the first argument. This is the |
||
| 547 | * same id as it is returned from the getAddressBooksForUser method. |
||
| 548 | * |
||
| 549 | * The cardUri is a base uri, and doesn't include the full path. The |
||
| 550 | * cardData argument is the vcard body, and is passed as a string. |
||
| 551 | * |
||
| 552 | * It is possible to return an ETag from this method. This ETag should |
||
| 553 | * match that of the updated resource, and must be enclosed with double |
||
| 554 | * quotes (that is: the string itself must contain the actual quotes). |
||
| 555 | * |
||
| 556 | * You should only return the ETag if you store the carddata as-is. If a |
||
| 557 | * subsequent GET request on the same card does not have the same body, |
||
| 558 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
| 559 | * confused. |
||
| 560 | * |
||
| 561 | * If you don't return an ETag, you can just return null. |
||
| 562 | * |
||
| 563 | * @param mixed $addressBookId |
||
| 564 | * @param string $cardUri |
||
| 565 | * @param string $cardData |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | function updateCard($addressBookId, $cardUri, $cardData) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Deletes a card |
||
| 597 | * |
||
| 598 | * @param mixed $addressBookId |
||
| 599 | * @param string $cardUri |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | function deleteCard($addressBookId, $cardUri) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * The getChanges method returns all the changes that have happened, since |
||
| 635 | * the specified syncToken in the specified address book. |
||
| 636 | * |
||
| 637 | * This function should return an array, such as the following: |
||
| 638 | * |
||
| 639 | * [ |
||
| 640 | * 'syncToken' => 'The current synctoken', |
||
| 641 | * 'added' => [ |
||
| 642 | * 'new.txt', |
||
| 643 | * ], |
||
| 644 | * 'modified' => [ |
||
| 645 | * 'modified.txt', |
||
| 646 | * ], |
||
| 647 | * 'deleted' => [ |
||
| 648 | * 'foo.php.bak', |
||
| 649 | * 'old.txt' |
||
| 650 | * ] |
||
| 651 | * ]; |
||
| 652 | * |
||
| 653 | * The returned syncToken property should reflect the *current* syncToken |
||
| 654 | * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
||
| 655 | * property. This is needed here too, to ensure the operation is atomic. |
||
| 656 | * |
||
| 657 | * If the $syncToken argument is specified as null, this is an initial |
||
| 658 | * sync, and all members should be reported. |
||
| 659 | * |
||
| 660 | * The modified property is an array of nodenames that have changed since |
||
| 661 | * the last token. |
||
| 662 | * |
||
| 663 | * The deleted property is an array with nodenames, that have been deleted |
||
| 664 | * from collection. |
||
| 665 | * |
||
| 666 | * The $syncLevel argument is basically the 'depth' of the report. If it's |
||
| 667 | * 1, you only have to report changes that happened only directly in |
||
| 668 | * immediate descendants. If it's 2, it should also include changes from |
||
| 669 | * the nodes below the child collections. (grandchildren) |
||
| 670 | * |
||
| 671 | * The $limit argument allows a client to specify how many results should |
||
| 672 | * be returned at most. If the limit is not specified, it should be treated |
||
| 673 | * as infinite. |
||
| 674 | * |
||
| 675 | * If the limit (infinite or not) is higher than you're willing to return, |
||
| 676 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
||
| 677 | * |
||
| 678 | * If the syncToken is expired (due to data cleanup) or unknown, you must |
||
| 679 | * return null. |
||
| 680 | * |
||
| 681 | * The limit is 'suggestive'. You are free to ignore it. |
||
| 682 | * |
||
| 683 | * @param string $addressBookId |
||
| 684 | * @param string $syncToken |
||
| 685 | * @param int $syncLevel |
||
| 686 | * @param int $limit |
||
| 687 | * @return array |
||
| 688 | */ |
||
| 689 | View Code Duplication | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 750 | |||
| 751 | /** |
||
| 752 | * Adds a change record to the addressbookchanges table. |
||
| 753 | * |
||
| 754 | * @param mixed $addressBookId |
||
| 755 | * @param string $objectUri |
||
| 756 | * @param int $operation 1 = add, 2 = modify, 3 = delete |
||
| 757 | * @return void |
||
| 758 | */ |
||
| 759 | View Code Duplication | protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 773 | |||
| 774 | private function readBlob($cardData) { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @param IShareable $shareable |
||
| 784 | * @param string[] $add |
||
| 785 | * @param string[] $remove |
||
| 786 | */ |
||
| 787 | public function updateShares(IShareable $shareable, $add, $remove) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * search contact |
||
| 793 | * |
||
| 794 | * @param int $addressBookId |
||
| 795 | * @param string $pattern which should match within the $searchProperties |
||
| 796 | * @param array $searchProperties defines the properties within the query pattern should match |
||
| 797 | * @return array an array of contacts which are arrays of key-value-pairs |
||
| 798 | */ |
||
| 799 | public function search($addressBookId, $pattern, $searchProperties) { |
||
| 826 | |||
| 827 | /** |
||
| 828 | * @param int $bookId |
||
| 829 | * @param string $name |
||
| 830 | * @return array |
||
| 831 | */ |
||
| 832 | public function collectCardProperties($bookId, $name) { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * get URI from a given contact |
||
| 848 | * |
||
| 849 | * @param int $id |
||
| 850 | * @return string |
||
| 851 | */ |
||
| 852 | public function getCardUri($id) { |
||
| 868 | |||
| 869 | /** |
||
| 870 | * return contact with the given URI |
||
| 871 | * |
||
| 872 | * @param int $addressBookId |
||
| 873 | * @param string $uri |
||
| 874 | * @returns array |
||
| 875 | */ |
||
| 876 | public function getContact($addressBookId, $uri) { |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Returns the list of people whom this address book is shared with. |
||
| 895 | * |
||
| 896 | * Every element in this array should have the following properties: |
||
| 897 | * * href - Often a mailto: address |
||
| 898 | * * commonName - Optional, for example a first + last name |
||
| 899 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
||
| 900 | * * readOnly - boolean |
||
| 901 | * * summary - Optional, a description for the share |
||
| 902 | * |
||
| 903 | * @return array |
||
| 904 | */ |
||
| 905 | public function getShares($addressBookId) { |
||
| 908 | |||
| 909 | /** |
||
| 910 | * update properties table |
||
| 911 | * |
||
| 912 | * @param int $addressBookId |
||
| 913 | * @param string $cardUri |
||
| 914 | * @param string $vCardSerialized |
||
| 915 | */ |
||
| 916 | protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
||
| 951 | |||
| 952 | /** |
||
| 953 | * read vCard data into a vCard object |
||
| 954 | * |
||
| 955 | * @param string $cardData |
||
| 956 | * @return VCard |
||
| 957 | */ |
||
| 958 | protected function readCard($cardData) { |
||
| 961 | |||
| 962 | /** |
||
| 963 | * delete all properties from a given card |
||
| 964 | * |
||
| 965 | * @param int $addressBookId |
||
| 966 | * @param int $cardId |
||
| 967 | */ |
||
| 968 | protected function purgeProperties($addressBookId, $cardId) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * get ID from a given contact |
||
| 978 | * |
||
| 979 | * @param int $addressBookId |
||
| 980 | * @param string $uri |
||
| 981 | * @return int |
||
| 982 | */ |
||
| 983 | protected function getCardId($addressBookId, $uri) { |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * For shared address books the sharee is set in the ACL of the address book |
||
| 1002 | * @param $addressBookId |
||
| 1003 | * @param $acl |
||
| 1004 | * @return array |
||
| 1005 | */ |
||
| 1006 | public function applyShareAcl($addressBookId, $acl) { |
||
| 1009 | |||
| 1010 | View Code Duplication | private function convertPrincipal($principalUri, $toV2) { |
|
| 1020 | } |
||
| 1021 |
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: