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 |
||
47 | class CardDavBackend implements BackendInterface, SyncSupport { |
||
48 | |||
49 | /** @var Principal */ |
||
50 | private $principalBackend; |
||
51 | |||
52 | /** @var string */ |
||
53 | private $dbCardsTable = 'cards'; |
||
54 | |||
55 | /** @var string */ |
||
56 | private $dbCardsPropertiesTable = 'cards_properties'; |
||
57 | |||
58 | /** @var IDBConnection */ |
||
59 | private $db; |
||
60 | |||
61 | /** @var Backend */ |
||
62 | private $sharingBackend; |
||
63 | |||
64 | /** @var array properties to index */ |
||
65 | public static $indexProperties = array( |
||
66 | 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
||
67 | 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
||
68 | |||
69 | /** @var EventDispatcherInterface */ |
||
70 | private $dispatcher; |
||
71 | |||
72 | /** |
||
73 | * CardDavBackend constructor. |
||
74 | * |
||
75 | * @param IDBConnection $db |
||
76 | * @param Principal $principalBackend |
||
77 | * @param EventDispatcherInterface $dispatcher |
||
78 | */ |
||
79 | public function __construct(IDBConnection $db, |
||
87 | |||
88 | /** |
||
89 | * Returns the list of address books for a specific user. |
||
90 | * |
||
91 | * Every addressbook should have the following properties: |
||
92 | * id - an arbitrary unique id |
||
93 | * uri - the 'basename' part of the url |
||
94 | * principaluri - Same as the passed parameter |
||
95 | * |
||
96 | * Any additional clark-notation property may be passed besides this. Some |
||
97 | * common ones are : |
||
98 | * {DAV:}displayname |
||
99 | * {urn:ietf:params:xml:ns:carddav}addressbook-description |
||
100 | * {http://calendarserver.org/ns/}getctag |
||
101 | * |
||
102 | * @param string $principalUri |
||
103 | * @return array |
||
104 | */ |
||
105 | function getAddressBooksForUser($principalUri) { |
||
165 | |||
166 | /** |
||
167 | * @param int $addressBookId |
||
168 | */ |
||
169 | public function getAddressBookById($addressBookId) { |
||
192 | |||
193 | /** |
||
194 | * @param $addressBookUri |
||
195 | * @return array|null |
||
196 | */ |
||
197 | public function getAddressBooksByUri($principal, $addressBookUri) { |
||
222 | |||
223 | /** |
||
224 | * Updates properties for an address book. |
||
225 | * |
||
226 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
227 | * To do the actual updates, you must tell this object which properties |
||
228 | * you're going to process with the handle() method. |
||
229 | * |
||
230 | * Calling the handle method is like telling the PropPatch object "I |
||
231 | * promise I can handle updating this property". |
||
232 | * |
||
233 | * Read the PropPatch documentation for more info and examples. |
||
234 | * |
||
235 | * @param string $addressBookId |
||
236 | * @param \Sabre\DAV\PropPatch $propPatch |
||
237 | * @return void |
||
238 | */ |
||
239 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
||
274 | |||
275 | /** |
||
276 | * Creates a new address book |
||
277 | * |
||
278 | * @param string $principalUri |
||
279 | * @param string $url Just the 'basename' of the url. |
||
280 | * @param array $properties |
||
281 | * @return int |
||
282 | * @throws BadRequest |
||
283 | */ |
||
284 | function createAddressBook($principalUri, $url, array $properties) { |
||
328 | |||
329 | /** |
||
330 | * Deletes an entire addressbook and all its contents |
||
331 | * |
||
332 | * @param mixed $addressBookId |
||
333 | * @return void |
||
334 | */ |
||
335 | function deleteAddressBook($addressBookId) { |
||
359 | |||
360 | /** |
||
361 | * Returns all cards for a specific addressbook id. |
||
362 | * |
||
363 | * This method should return the following properties for each card: |
||
364 | * * carddata - raw vcard data |
||
365 | * * uri - Some unique url |
||
366 | * * lastmodified - A unix timestamp |
||
367 | * |
||
368 | * It's recommended to also return the following properties: |
||
369 | * * etag - A unique etag. This must change every time the card changes. |
||
370 | * * size - The size of the card in bytes. |
||
371 | * |
||
372 | * If these last two properties are provided, less time will be spent |
||
373 | * calculating them. If they are specified, you can also ommit carddata. |
||
374 | * This may speed up certain requests, especially with large cards. |
||
375 | * |
||
376 | * @param mixed $addressBookId |
||
377 | * @return array |
||
378 | */ |
||
379 | function getCards($addressBookId) { |
||
397 | |||
398 | /** |
||
399 | * Returns a specific card. |
||
400 | * |
||
401 | * The same set of properties must be returned as with getCards. The only |
||
402 | * exception is that 'carddata' is absolutely required. |
||
403 | * |
||
404 | * If the card does not exist, you must return false. |
||
405 | * |
||
406 | * @param mixed $addressBookId |
||
407 | * @param string $cardUri |
||
408 | * @return array |
||
409 | */ |
||
410 | function getCard($addressBookId, $cardUri) { |
||
428 | |||
429 | /** |
||
430 | * Returns a list of cards. |
||
431 | * |
||
432 | * This method should work identical to getCard, but instead return all the |
||
433 | * cards in the list as an array. |
||
434 | * |
||
435 | * If the backend supports this, it may allow for some speed-ups. |
||
436 | * |
||
437 | * @param mixed $addressBookId |
||
438 | * @param string[] $uris |
||
439 | * @return array |
||
440 | */ |
||
441 | function getMultipleCards($addressBookId, array $uris) { |
||
468 | |||
469 | /** |
||
470 | * Creates a new card. |
||
471 | * |
||
472 | * The addressbook id will be passed as the first argument. This is the |
||
473 | * same id as it is returned from the getAddressBooksForUser method. |
||
474 | * |
||
475 | * The cardUri is a base uri, and doesn't include the full path. The |
||
476 | * cardData argument is the vcard body, and is passed as a string. |
||
477 | * |
||
478 | * It is possible to return an ETag from this method. This ETag is for the |
||
479 | * newly created resource, and must be enclosed with double quotes (that |
||
480 | * is, the string itself must contain the double quotes). |
||
481 | * |
||
482 | * You should only return the ETag if you store the carddata as-is. If a |
||
483 | * subsequent GET request on the same card does not have the same body, |
||
484 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
485 | * confused. |
||
486 | * |
||
487 | * If you don't return an ETag, you can just return null. |
||
488 | * |
||
489 | * @param mixed $addressBookId |
||
490 | * @param string $cardUri |
||
491 | * @param string $cardData |
||
492 | * @return string |
||
493 | */ |
||
494 | function createCard($addressBookId, $cardUri, $cardData) { |
||
522 | |||
523 | /** |
||
524 | * Updates a card. |
||
525 | * |
||
526 | * The addressbook id will be passed as the first argument. This is the |
||
527 | * same id as it is returned from the getAddressBooksForUser method. |
||
528 | * |
||
529 | * The cardUri is a base uri, and doesn't include the full path. The |
||
530 | * cardData argument is the vcard body, and is passed as a string. |
||
531 | * |
||
532 | * It is possible to return an ETag from this method. This ETag should |
||
533 | * match that of the updated resource, and must be enclosed with double |
||
534 | * quotes (that is: the string itself must contain the actual quotes). |
||
535 | * |
||
536 | * You should only return the ETag if you store the carddata as-is. If a |
||
537 | * subsequent GET request on the same card does not have the same body, |
||
538 | * byte-by-byte and you did return an ETag here, clients tend to get |
||
539 | * confused. |
||
540 | * |
||
541 | * If you don't return an ETag, you can just return null. |
||
542 | * |
||
543 | * @param mixed $addressBookId |
||
544 | * @param string $cardUri |
||
545 | * @param string $cardData |
||
546 | * @return string |
||
547 | */ |
||
548 | function updateCard($addressBookId, $cardUri, $cardData) { |
||
574 | |||
575 | /** |
||
576 | * Deletes a card |
||
577 | * |
||
578 | * @param mixed $addressBookId |
||
579 | * @param string $cardUri |
||
580 | * @return bool |
||
581 | */ |
||
582 | function deleteCard($addressBookId, $cardUri) { |
||
612 | |||
613 | /** |
||
614 | * The getChanges method returns all the changes that have happened, since |
||
615 | * the specified syncToken in the specified address book. |
||
616 | * |
||
617 | * This function should return an array, such as the following: |
||
618 | * |
||
619 | * [ |
||
620 | * 'syncToken' => 'The current synctoken', |
||
621 | * 'added' => [ |
||
622 | * 'new.txt', |
||
623 | * ], |
||
624 | * 'modified' => [ |
||
625 | * 'modified.txt', |
||
626 | * ], |
||
627 | * 'deleted' => [ |
||
628 | * 'foo.php.bak', |
||
629 | * 'old.txt' |
||
630 | * ] |
||
631 | * ]; |
||
632 | * |
||
633 | * The returned syncToken property should reflect the *current* syncToken |
||
634 | * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
||
635 | * property. This is needed here too, to ensure the operation is atomic. |
||
636 | * |
||
637 | * If the $syncToken argument is specified as null, this is an initial |
||
638 | * sync, and all members should be reported. |
||
639 | * |
||
640 | * The modified property is an array of nodenames that have changed since |
||
641 | * the last token. |
||
642 | * |
||
643 | * The deleted property is an array with nodenames, that have been deleted |
||
644 | * from collection. |
||
645 | * |
||
646 | * The $syncLevel argument is basically the 'depth' of the report. If it's |
||
647 | * 1, you only have to report changes that happened only directly in |
||
648 | * immediate descendants. If it's 2, it should also include changes from |
||
649 | * the nodes below the child collections. (grandchildren) |
||
650 | * |
||
651 | * The $limit argument allows a client to specify how many results should |
||
652 | * be returned at most. If the limit is not specified, it should be treated |
||
653 | * as infinite. |
||
654 | * |
||
655 | * If the limit (infinite or not) is higher than you're willing to return, |
||
656 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
||
657 | * |
||
658 | * If the syncToken is expired (due to data cleanup) or unknown, you must |
||
659 | * return null. |
||
660 | * |
||
661 | * The limit is 'suggestive'. You are free to ignore it. |
||
662 | * |
||
663 | * @param string $addressBookId |
||
664 | * @param string $syncToken |
||
665 | * @param int $syncLevel |
||
666 | * @param int $limit |
||
667 | * @return array |
||
668 | */ |
||
669 | View Code Duplication | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
730 | |||
731 | /** |
||
732 | * Adds a change record to the addressbookchanges table. |
||
733 | * |
||
734 | * @param mixed $addressBookId |
||
735 | * @param string $objectUri |
||
736 | * @param int $operation 1 = add, 2 = modify, 3 = delete |
||
737 | * @return void |
||
738 | */ |
||
739 | View Code Duplication | protected function addChange($addressBookId, $objectUri, $operation) { |
|
753 | |||
754 | private function readBlob($cardData) { |
||
761 | |||
762 | /** |
||
763 | * @param IShareable $shareable |
||
764 | * @param string[] $add |
||
765 | * @param string[] $remove |
||
766 | */ |
||
767 | public function updateShares(IShareable $shareable, $add, $remove) { |
||
770 | |||
771 | /** |
||
772 | * search contact |
||
773 | * |
||
774 | * @param int $addressBookId |
||
775 | * @param string $pattern which should match within the $searchProperties |
||
776 | * @param array $searchProperties defines the properties within the query pattern should match |
||
777 | * @return array an array of contacts which are arrays of key-value-pairs |
||
778 | */ |
||
779 | public function search($addressBookId, $pattern, $searchProperties) { |
||
806 | |||
807 | /** |
||
808 | * @param int $bookId |
||
809 | * @param string $name |
||
810 | * @return array |
||
811 | */ |
||
812 | public function collectCardProperties($bookId, $name) { |
||
825 | |||
826 | /** |
||
827 | * get URI from a given contact |
||
828 | * |
||
829 | * @param int $id |
||
830 | * @return string |
||
831 | */ |
||
832 | public function getCardUri($id) { |
||
848 | |||
849 | /** |
||
850 | * return contact with the given URI |
||
851 | * |
||
852 | * @param int $addressBookId |
||
853 | * @param string $uri |
||
854 | * @returns array |
||
855 | */ |
||
856 | public function getContact($addressBookId, $uri) { |
||
872 | |||
873 | /** |
||
874 | * Returns the list of people whom this address book is shared with. |
||
875 | * |
||
876 | * Every element in this array should have the following properties: |
||
877 | * * href - Often a mailto: address |
||
878 | * * commonName - Optional, for example a first + last name |
||
879 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
||
880 | * * readOnly - boolean |
||
881 | * * summary - Optional, a description for the share |
||
882 | * |
||
883 | * @return array |
||
884 | */ |
||
885 | public function getShares($addressBookId) { |
||
888 | |||
889 | /** |
||
890 | * update properties table |
||
891 | * |
||
892 | * @param int $addressBookId |
||
893 | * @param string $cardUri |
||
894 | * @param string $vCardSerialized |
||
895 | */ |
||
896 | protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
||
931 | |||
932 | /** |
||
933 | * read vCard data into a vCard object |
||
934 | * |
||
935 | * @param string $cardData |
||
936 | * @return VCard |
||
937 | */ |
||
938 | protected function readCard($cardData) { |
||
941 | |||
942 | /** |
||
943 | * delete all properties from a given card |
||
944 | * |
||
945 | * @param int $addressBookId |
||
946 | * @param int $cardId |
||
947 | */ |
||
948 | protected function purgeProperties($addressBookId, $cardId) { |
||
955 | |||
956 | /** |
||
957 | * get ID from a given contact |
||
958 | * |
||
959 | * @param int $addressBookId |
||
960 | * @param string $uri |
||
961 | * @return int |
||
962 | */ |
||
963 | protected function getCardId($addressBookId, $uri) { |
||
979 | |||
980 | /** |
||
981 | * For shared address books the sharee is set in the ACL of the address book |
||
982 | * @param $addressBookId |
||
983 | * @param $acl |
||
984 | * @return array |
||
985 | */ |
||
986 | public function applyShareAcl($addressBookId, $acl) { |
||
989 | |||
990 | View Code Duplication | private function convertPrincipal($principalUri, $toV2) { |
|
1000 | } |
||
1001 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: