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) { | 
            ||
| 197 | |||
| 198 | 	public function getUsersOwnAddressBooks($principalUri) { | 
            ||
| 224 | |||
| 225 | View Code Duplication | 	private function getUserDisplayName($uid) { | 
            |
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * @param int $addressBookId  | 
            ||
| 241 | */  | 
            ||
| 242 | 	public function getAddressBookById($addressBookId) { | 
            ||
| 265 | |||
| 266 | /**  | 
            ||
| 267 | * @param $addressBookUri  | 
            ||
| 268 | * @return array|null  | 
            ||
| 269 | */  | 
            ||
| 270 | 	public function getAddressBooksByUri($principal, $addressBookUri) { | 
            ||
| 295 | |||
| 296 | /**  | 
            ||
| 297 | * Updates properties for an address book.  | 
            ||
| 298 | *  | 
            ||
| 299 | * The list of mutations is stored in a Sabre\DAV\PropPatch object.  | 
            ||
| 300 | * To do the actual updates, you must tell this object which properties  | 
            ||
| 301 | * you're going to process with the handle() method.  | 
            ||
| 302 | *  | 
            ||
| 303 | * Calling the handle method is like telling the PropPatch object "I  | 
            ||
| 304 | * promise I can handle updating this property".  | 
            ||
| 305 | *  | 
            ||
| 306 | * Read the PropPatch documentation for more info and examples.  | 
            ||
| 307 | *  | 
            ||
| 308 | * @param string $addressBookId  | 
            ||
| 309 | * @param \Sabre\DAV\PropPatch $propPatch  | 
            ||
| 310 | * @return void  | 
            ||
| 311 | */  | 
            ||
| 312 | 	function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { | 
            ||
| 347 | |||
| 348 | /**  | 
            ||
| 349 | * Creates a new address book  | 
            ||
| 350 | *  | 
            ||
| 351 | * @param string $principalUri  | 
            ||
| 352 | * @param string $url Just the 'basename' of the url.  | 
            ||
| 353 | * @param array $properties  | 
            ||
| 354 | * @return int  | 
            ||
| 355 | * @throws BadRequest  | 
            ||
| 356 | */  | 
            ||
| 357 | 	function createAddressBook($principalUri, $url, array $properties) { | 
            ||
| 401 | |||
| 402 | /**  | 
            ||
| 403 | * Deletes an entire addressbook and all its contents  | 
            ||
| 404 | *  | 
            ||
| 405 | * @param mixed $addressBookId  | 
            ||
| 406 | * @return void  | 
            ||
| 407 | */  | 
            ||
| 408 | 	function deleteAddressBook($addressBookId) { | 
            ||
| 432 | |||
| 433 | /**  | 
            ||
| 434 | * Returns all cards for a specific addressbook id.  | 
            ||
| 435 | *  | 
            ||
| 436 | * This method should return the following properties for each card:  | 
            ||
| 437 | * * carddata - raw vcard data  | 
            ||
| 438 | * * uri - Some unique url  | 
            ||
| 439 | * * lastmodified - A unix timestamp  | 
            ||
| 440 | *  | 
            ||
| 441 | * It's recommended to also return the following properties:  | 
            ||
| 442 | * * etag - A unique etag. This must change every time the card changes.  | 
            ||
| 443 | * * size - The size of the card in bytes.  | 
            ||
| 444 | *  | 
            ||
| 445 | * If these last two properties are provided, less time will be spent  | 
            ||
| 446 | * calculating them. If they are specified, you can also ommit carddata.  | 
            ||
| 447 | * This may speed up certain requests, especially with large cards.  | 
            ||
| 448 | *  | 
            ||
| 449 | * @param mixed $addressBookId  | 
            ||
| 450 | * @return array  | 
            ||
| 451 | */  | 
            ||
| 452 | 	function getCards($addressBookId) { | 
            ||
| 470 | |||
| 471 | /**  | 
            ||
| 472 | * Returns a specific card.  | 
            ||
| 473 | *  | 
            ||
| 474 | * The same set of properties must be returned as with getCards. The only  | 
            ||
| 475 | * exception is that 'carddata' is absolutely required.  | 
            ||
| 476 | *  | 
            ||
| 477 | * If the card does not exist, you must return false.  | 
            ||
| 478 | *  | 
            ||
| 479 | * @param mixed $addressBookId  | 
            ||
| 480 | * @param string $cardUri  | 
            ||
| 481 | * @return array  | 
            ||
| 482 | */  | 
            ||
| 483 | 	function getCard($addressBookId, $cardUri) { | 
            ||
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * Returns a list of cards.  | 
            ||
| 504 | *  | 
            ||
| 505 | * This method should work identical to getCard, but instead return all the  | 
            ||
| 506 | * cards in the list as an array.  | 
            ||
| 507 | *  | 
            ||
| 508 | * If the backend supports this, it may allow for some speed-ups.  | 
            ||
| 509 | *  | 
            ||
| 510 | * @param mixed $addressBookId  | 
            ||
| 511 | * @param string[] $uris  | 
            ||
| 512 | * @return array  | 
            ||
| 513 | */  | 
            ||
| 514 | 	function getMultipleCards($addressBookId, array $uris) { | 
            ||
| 541 | |||
| 542 | /**  | 
            ||
| 543 | * Creates a new card.  | 
            ||
| 544 | *  | 
            ||
| 545 | * The addressbook id will be passed as the first argument. This is the  | 
            ||
| 546 | * same id as it is returned from the getAddressBooksForUser method.  | 
            ||
| 547 | *  | 
            ||
| 548 | * The cardUri is a base uri, and doesn't include the full path. The  | 
            ||
| 549 | * cardData argument is the vcard body, and is passed as a string.  | 
            ||
| 550 | *  | 
            ||
| 551 | * It is possible to return an ETag from this method. This ETag is for the  | 
            ||
| 552 | * newly created resource, and must be enclosed with double quotes (that  | 
            ||
| 553 | * is, the string itself must contain the double quotes).  | 
            ||
| 554 | *  | 
            ||
| 555 | * You should only return the ETag if you store the carddata as-is. If a  | 
            ||
| 556 | * subsequent GET request on the same card does not have the same body,  | 
            ||
| 557 | * byte-by-byte and you did return an ETag here, clients tend to get  | 
            ||
| 558 | * confused.  | 
            ||
| 559 | *  | 
            ||
| 560 | * If you don't return an ETag, you can just return null.  | 
            ||
| 561 | *  | 
            ||
| 562 | * @param mixed $addressBookId  | 
            ||
| 563 | * @param string $cardUri  | 
            ||
| 564 | * @param string $cardData  | 
            ||
| 565 | * @return string  | 
            ||
| 566 | */  | 
            ||
| 567 | 	function createCard($addressBookId, $cardUri, $cardData) { | 
            ||
| 595 | |||
| 596 | /**  | 
            ||
| 597 | * Updates a card.  | 
            ||
| 598 | *  | 
            ||
| 599 | * The addressbook id will be passed as the first argument. This is the  | 
            ||
| 600 | * same id as it is returned from the getAddressBooksForUser method.  | 
            ||
| 601 | *  | 
            ||
| 602 | * The cardUri is a base uri, and doesn't include the full path. The  | 
            ||
| 603 | * cardData argument is the vcard body, and is passed as a string.  | 
            ||
| 604 | *  | 
            ||
| 605 | * It is possible to return an ETag from this method. This ETag should  | 
            ||
| 606 | * match that of the updated resource, and must be enclosed with double  | 
            ||
| 607 | * quotes (that is: the string itself must contain the actual quotes).  | 
            ||
| 608 | *  | 
            ||
| 609 | * You should only return the ETag if you store the carddata as-is. If a  | 
            ||
| 610 | * subsequent GET request on the same card does not have the same body,  | 
            ||
| 611 | * byte-by-byte and you did return an ETag here, clients tend to get  | 
            ||
| 612 | * confused.  | 
            ||
| 613 | *  | 
            ||
| 614 | * If you don't return an ETag, you can just return null.  | 
            ||
| 615 | *  | 
            ||
| 616 | * @param mixed $addressBookId  | 
            ||
| 617 | * @param string $cardUri  | 
            ||
| 618 | * @param string $cardData  | 
            ||
| 619 | * @return string  | 
            ||
| 620 | */  | 
            ||
| 621 | 	function updateCard($addressBookId, $cardUri, $cardData) { | 
            ||
| 647 | |||
| 648 | /**  | 
            ||
| 649 | * Deletes a card  | 
            ||
| 650 | *  | 
            ||
| 651 | * @param mixed $addressBookId  | 
            ||
| 652 | * @param string $cardUri  | 
            ||
| 653 | * @return bool  | 
            ||
| 654 | */  | 
            ||
| 655 | 	function deleteCard($addressBookId, $cardUri) { | 
            ||
| 685 | |||
| 686 | /**  | 
            ||
| 687 | * The getChanges method returns all the changes that have happened, since  | 
            ||
| 688 | * the specified syncToken in the specified address book.  | 
            ||
| 689 | *  | 
            ||
| 690 | * This function should return an array, such as the following:  | 
            ||
| 691 | *  | 
            ||
| 692 | * [  | 
            ||
| 693 | * 'syncToken' => 'The current synctoken',  | 
            ||
| 694 | * 'added' => [  | 
            ||
| 695 | * 'new.txt',  | 
            ||
| 696 | * ],  | 
            ||
| 697 | * 'modified' => [  | 
            ||
| 698 | * 'modified.txt',  | 
            ||
| 699 | * ],  | 
            ||
| 700 | * 'deleted' => [  | 
            ||
| 701 | * 'foo.php.bak',  | 
            ||
| 702 | * 'old.txt'  | 
            ||
| 703 | * ]  | 
            ||
| 704 | * ];  | 
            ||
| 705 | *  | 
            ||
| 706 | * The returned syncToken property should reflect the *current* syncToken  | 
            ||
| 707 | 	 * of the calendar, as reported in the {http://sabredav.org/ns}sync-token | 
            ||
| 708 | * property. This is needed here too, to ensure the operation is atomic.  | 
            ||
| 709 | *  | 
            ||
| 710 | * If the $syncToken argument is specified as null, this is an initial  | 
            ||
| 711 | * sync, and all members should be reported.  | 
            ||
| 712 | *  | 
            ||
| 713 | * The modified property is an array of nodenames that have changed since  | 
            ||
| 714 | * the last token.  | 
            ||
| 715 | *  | 
            ||
| 716 | * The deleted property is an array with nodenames, that have been deleted  | 
            ||
| 717 | * from collection.  | 
            ||
| 718 | *  | 
            ||
| 719 | * The $syncLevel argument is basically the 'depth' of the report. If it's  | 
            ||
| 720 | * 1, you only have to report changes that happened only directly in  | 
            ||
| 721 | * immediate descendants. If it's 2, it should also include changes from  | 
            ||
| 722 | * the nodes below the child collections. (grandchildren)  | 
            ||
| 723 | *  | 
            ||
| 724 | * The $limit argument allows a client to specify how many results should  | 
            ||
| 725 | * be returned at most. If the limit is not specified, it should be treated  | 
            ||
| 726 | * as infinite.  | 
            ||
| 727 | *  | 
            ||
| 728 | * If the limit (infinite or not) is higher than you're willing to return,  | 
            ||
| 729 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.  | 
            ||
| 730 | *  | 
            ||
| 731 | * If the syncToken is expired (due to data cleanup) or unknown, you must  | 
            ||
| 732 | * return null.  | 
            ||
| 733 | *  | 
            ||
| 734 | * The limit is 'suggestive'. You are free to ignore it.  | 
            ||
| 735 | *  | 
            ||
| 736 | * @param string $addressBookId  | 
            ||
| 737 | * @param string $syncToken  | 
            ||
| 738 | * @param int $syncLevel  | 
            ||
| 739 | * @param int $limit  | 
            ||
| 740 | * @return array  | 
            ||
| 741 | */  | 
            ||
| 742 | View Code Duplication | 	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { | 
            |
| 803 | |||
| 804 | /**  | 
            ||
| 805 | * Adds a change record to the addressbookchanges table.  | 
            ||
| 806 | *  | 
            ||
| 807 | * @param mixed $addressBookId  | 
            ||
| 808 | * @param string $objectUri  | 
            ||
| 809 | * @param int $operation 1 = add, 2 = modify, 3 = delete  | 
            ||
| 810 | * @return void  | 
            ||
| 811 | */  | 
            ||
| 812 | View Code Duplication | 	protected function addChange($addressBookId, $objectUri, $operation) { | 
            |
| 826 | |||
| 827 | 	private function readBlob($cardData) { | 
            ||
| 834 | |||
| 835 | /**  | 
            ||
| 836 | * @param IShareable $shareable  | 
            ||
| 837 | * @param string[] $add  | 
            ||
| 838 | * @param string[] $remove  | 
            ||
| 839 | */  | 
            ||
| 840 | 	public function updateShares(IShareable $shareable, $add, $remove) { | 
            ||
| 843 | |||
| 844 | /**  | 
            ||
| 845 | * search contact  | 
            ||
| 846 | *  | 
            ||
| 847 | * @param int $addressBookId  | 
            ||
| 848 | * @param string $pattern which should match within the $searchProperties  | 
            ||
| 849 | * @param array $searchProperties defines the properties within the query pattern should match  | 
            ||
| 850 | * @return array an array of contacts which are arrays of key-value-pairs  | 
            ||
| 851 | */  | 
            ||
| 852 | 	public function search($addressBookId, $pattern, $searchProperties) { | 
            ||
| 879 | |||
| 880 | /**  | 
            ||
| 881 | * @param int $bookId  | 
            ||
| 882 | * @param string $name  | 
            ||
| 883 | * @return array  | 
            ||
| 884 | */  | 
            ||
| 885 | 	public function collectCardProperties($bookId, $name) { | 
            ||
| 898 | |||
| 899 | /**  | 
            ||
| 900 | * get URI from a given contact  | 
            ||
| 901 | *  | 
            ||
| 902 | * @param int $id  | 
            ||
| 903 | * @return string  | 
            ||
| 904 | */  | 
            ||
| 905 | 	public function getCardUri($id) { | 
            ||
| 921 | |||
| 922 | /**  | 
            ||
| 923 | * return contact with the given URI  | 
            ||
| 924 | *  | 
            ||
| 925 | * @param int $addressBookId  | 
            ||
| 926 | * @param string $uri  | 
            ||
| 927 | * @returns array  | 
            ||
| 928 | */  | 
            ||
| 929 | 	public function getContact($addressBookId, $uri) { | 
            ||
| 945 | |||
| 946 | /**  | 
            ||
| 947 | * Returns the list of people whom this address book is shared with.  | 
            ||
| 948 | *  | 
            ||
| 949 | * Every element in this array should have the following properties:  | 
            ||
| 950 | * * href - Often a mailto: address  | 
            ||
| 951 | * * commonName - Optional, for example a first + last name  | 
            ||
| 952 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.  | 
            ||
| 953 | * * readOnly - boolean  | 
            ||
| 954 | * * summary - Optional, a description for the share  | 
            ||
| 955 | *  | 
            ||
| 956 | * @return array  | 
            ||
| 957 | */  | 
            ||
| 958 | 	public function getShares($addressBookId) { | 
            ||
| 961 | |||
| 962 | /**  | 
            ||
| 963 | * update properties table  | 
            ||
| 964 | *  | 
            ||
| 965 | * @param int $addressBookId  | 
            ||
| 966 | * @param string $cardUri  | 
            ||
| 967 | * @param string $vCardSerialized  | 
            ||
| 968 | */  | 
            ||
| 969 | 	protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { | 
            ||
| 1004 | |||
| 1005 | /**  | 
            ||
| 1006 | * read vCard data into a vCard object  | 
            ||
| 1007 | *  | 
            ||
| 1008 | * @param string $cardData  | 
            ||
| 1009 | * @return VCard  | 
            ||
| 1010 | */  | 
            ||
| 1011 | 	protected function readCard($cardData) { | 
            ||
| 1014 | |||
| 1015 | /**  | 
            ||
| 1016 | * delete all properties from a given card  | 
            ||
| 1017 | *  | 
            ||
| 1018 | * @param int $addressBookId  | 
            ||
| 1019 | * @param int $cardId  | 
            ||
| 1020 | */  | 
            ||
| 1021 | 	protected function purgeProperties($addressBookId, $cardId) { | 
            ||
| 1028 | |||
| 1029 | /**  | 
            ||
| 1030 | * get ID from a given contact  | 
            ||
| 1031 | *  | 
            ||
| 1032 | * @param int $addressBookId  | 
            ||
| 1033 | * @param string $uri  | 
            ||
| 1034 | * @return int  | 
            ||
| 1035 | */  | 
            ||
| 1036 | 	protected function getCardId($addressBookId, $uri) { | 
            ||
| 1052 | |||
| 1053 | /**  | 
            ||
| 1054 | * For shared address books the sharee is set in the ACL of the address book  | 
            ||
| 1055 | * @param $addressBookId  | 
            ||
| 1056 | * @param $acl  | 
            ||
| 1057 | * @return array  | 
            ||
| 1058 | */  | 
            ||
| 1059 | 	public function applyShareAcl($addressBookId, $acl) { | 
            ||
| 1062 | |||
| 1063 | View Code Duplication | 	private function convertPrincipal($principalUri, $toV2) { | 
            |
| 1073 | }  | 
            ||
| 1074 | 
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: