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 Calendar 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 Calendar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Calendar extends \Sabre\CalDAV\Calendar implements IShareable { |
||
| 34 | |||
| 35 | public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Updates the list of shares. |
||
| 45 | * |
||
| 46 | * The first array is a list of people that are to be added to the |
||
| 47 | * resource. |
||
| 48 | * |
||
| 49 | * Every element in the add array has the following properties: |
||
| 50 | * * href - A url. Usually a mailto: address |
||
| 51 | * * commonName - Usually a first and last name, or false |
||
| 52 | * * summary - A description of the share, can also be false |
||
| 53 | * * readOnly - A boolean value |
||
| 54 | * |
||
| 55 | * Every element in the remove array is just the address string. |
||
| 56 | * |
||
| 57 | * @param array $add |
||
| 58 | * @param array $remove |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | function updateShares(array $add, array $remove) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the list of people whom this resource is shared with. |
||
| 69 | * |
||
| 70 | * Every element in this array should have the following properties: |
||
| 71 | * * href - Often a mailto: address |
||
| 72 | * * commonName - Optional, for example a first + last name |
||
| 73 | * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
||
| 74 | * * readOnly - boolean |
||
| 75 | * * summary - Optional, a description for the share |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | function getShares() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return int |
||
| 87 | */ |
||
| 88 | public function getResourceId() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function getPrincipalURI() { |
||
| 96 | return $this->calendarInfo['principaluri']; |
||
| 97 | } |
||
| 98 | |||
| 99 | View Code Duplication | function getACL() { |
|
| 100 | $acl = [ |
||
| 101 | [ |
||
| 102 | 'privilege' => '{DAV:}read', |
||
| 103 | 'principal' => $this->getOwner(), |
||
| 104 | 'protected' => true, |
||
| 105 | ]]; |
||
| 106 | if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) { |
||
| 107 | $acl[] = [ |
||
| 108 | 'privilege' => '{DAV:}write', |
||
| 109 | 'principal' => $this->getOwner(), |
||
| 110 | 'protected' => true, |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | if ($this->getOwner() !== parent::getOwner()) { |
||
| 114 | $acl[] = [ |
||
| 115 | 'privilege' => '{DAV:}read', |
||
| 116 | 'principal' => parent::getOwner(), |
||
| 117 | 'protected' => true, |
||
| 118 | ]; |
||
| 119 | if ($this->canWrite()) { |
||
| 120 | $acl[] = [ |
||
| 121 | 'privilege' => '{DAV:}write', |
||
| 122 | 'principal' => parent::getOwner(), |
||
| 123 | 'protected' => true, |
||
| 124 | ]; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | if ($this->isPublic()) { |
||
| 128 | $acl[] = [ |
||
| 129 | 'privilege' => '{DAV:}read', |
||
| 130 | 'principal' => 'principals/system/public', |
||
| 131 | 'protected' => true, |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** @var CalDavBackend $calDavBackend */ |
||
| 136 | $calDavBackend = $this->caldavBackend; |
||
| 137 | return $calDavBackend->applyShareAcl($this->getResourceId(), $acl); |
||
| 138 | } |
||
| 139 | |||
| 140 | function getChildACL() { |
||
| 143 | |||
| 144 | function getOwner() { |
||
| 150 | |||
| 151 | View Code Duplication | function delete() { |
|
| 171 | |||
| 172 | function propPatch(PropPatch $propPatch) { |
||
| 180 | |||
| 181 | function getChild($name) { |
||
| 198 | |||
| 199 | View Code Duplication | function getChildren() { |
|
| 213 | |||
| 214 | View Code Duplication | function getMultipleChildren(array $paths) { |
|
| 228 | |||
| 229 | function childExists($name) { |
||
| 240 | |||
| 241 | function calendarQuery(array $filters) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param boolean $value |
||
| 255 | * @return string|null |
||
| 256 | */ |
||
| 257 | function setPublishStatus($value) { |
||
| 258 | $publicUri = $this->caldavBackend->setPublishStatus($value, $this); |
||
| 259 | $this->calendarInfo['publicuri'] = $publicUri; |
||
| 260 | return $publicUri; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return mixed $value |
||
| 265 | */ |
||
| 266 | function getPublishStatus() { |
||
| 267 | return $this->caldavBackend->getPublishStatus($this); |
||
| 268 | } |
||
| 269 | |||
| 270 | private function canWrite() { |
||
| 276 | |||
| 277 | private function isPublic() { |
||
| 278 | return isset($this->calendarInfo['{http://owncloud.org/ns}public']); |
||
| 279 | } |
||
| 280 | |||
| 281 | private function isShared() { |
||
| 284 | |||
| 285 | public function isSubscription() { |
||
| 286 | return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']); |
||
| 287 | } |
||
| 288 | |||
| 289 | } |
||
| 290 |