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 API 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 API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class API |
||
| 20 | { |
||
| 21 | protected static $defaultClientOptions = array( |
||
| 22 | 'version' => ExchangeWebServices::VERSION_2010 |
||
| 23 | ); |
||
| 24 | |||
| 25 | 36 | public function __construct(ExchangeWebServices $client = null) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @return Type\EmailAddressType |
||
| 34 | */ |
||
| 35 | 27 | public function getPrimarySmtpMailbox() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Storing the API client |
||
| 42 | * @var ExchangeWebServices |
||
| 43 | */ |
||
| 44 | private $client; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get a calendar item |
||
| 48 | * |
||
| 49 | * @param string $name |
||
| 50 | * @return CalendarAPI |
||
| 51 | */ |
||
| 52 | 6 | public function getCalendar($name = null) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $folderName |
||
| 63 | * @return MailAPI |
||
| 64 | */ |
||
| 65 | 7 | public function getMailbox($folderName = null) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Set the API client |
||
| 76 | * |
||
| 77 | * @param ExchangeWebServices $client |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | 36 | public function setClient($client) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Get the API client |
||
| 89 | * |
||
| 90 | * @return ExchangeWebServices |
||
| 91 | */ |
||
| 92 | 35 | public function getClient() |
|
| 96 | |||
| 97 | 34 | public static function withUsernameAndPassword($server, $username, $password, $options = []) |
|
| 106 | |||
| 107 | 1 | public static function withCallbackToken($server, $token, $options = []) |
|
| 115 | |||
| 116 | 1 | public static function withCustomAuthentication($server, $authentication, $options = []) |
|
| 117 | { |
||
| 118 | 1 | return new static(ExchangeWebServices::fromCustomAuthentication( |
|
| 119 | 1 | $server, |
|
| 120 | 1 | $authentication, |
|
| 121 | 1 | array_replace_recursive(self::$defaultClientOptions, $options) |
|
| 122 | 1 | )); |
|
| 123 | } |
||
| 124 | |||
| 125 | 2 | public function getPrimarySmptEmailAddress() |
|
| 126 | { |
||
| 127 | 1 | if ($this->getPrimarySmtpMailbox() == null) { |
|
| 128 | 1 | return null; |
|
| 129 | } |
||
| 130 | |||
| 131 | 2 | return $this->getPrimarySmtpMailbox()->getEmailAddress(); |
|
| 132 | } |
||
| 133 | |||
| 134 | 1 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
| 135 | { |
||
| 136 | 1 | $this->getClient()->setPrimarySmtpEmailAddress($emailAddress); |
|
| 137 | |||
| 138 | 1 | return $this; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create items through the API client |
||
| 143 | * |
||
| 144 | * @param $items |
||
| 145 | * @param array $options |
||
| 146 | * @return Type |
||
| 147 | */ |
||
| 148 | 16 | public function createItems($items, $options = array()) |
|
| 149 | { |
||
| 150 | 16 | $items = Utilities\ensureIsArray($items); |
|
| 151 | $request = array( |
||
| 152 | 'Items' => $items |
||
| 153 | 16 | ); |
|
| 154 | |||
| 155 | 16 | $request = array_replace_recursive($request, $options); |
|
| 156 | 16 | $request = Type::buildFromArray($request); |
|
| 157 | |||
| 158 | 16 | $response = $this->getClient()->CreateItem($request); |
|
| 159 | |||
| 160 | 16 | return $response; |
|
| 161 | } |
||
| 162 | |||
| 163 | 4 | public function updateItems($items, $options = array()) |
|
| 182 | |||
| 183 | 1 | View Code Duplication | public function createCalendars($names, BaseFolderIdType $parentFolder = null, $options = array()) |
| 184 | { |
||
| 185 | 1 | if ($parentFolder === null) { |
|
| 186 | 1 | $parentFolder = $this->getDistinguishedFolderId('calendar'); |
|
| 187 | 1 | } |
|
| 188 | |||
| 189 | 1 | return $this->createFolders($names, $parentFolder, $options, 'IPF.Appointment'); |
|
| 190 | } |
||
| 191 | |||
| 192 | View Code Duplication | public function createContactsFolder($names, BaseFolderIdType $parentFolder = null, $options = array()) |
|
| 193 | { |
||
| 194 | if ($parentFolder === null) { |
||
| 195 | $parentFolder = $this->getDistinguishedFolderId('contacts'); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $this->createFolders($names, $parentFolder, $options, 'IPF.Contact'); |
||
| 199 | } |
||
| 200 | |||
| 201 | 4 | public function createFolders($names, BaseFolderIdType $parentFolder, $options = array(), $folderClass = null) |
|
| 202 | { |
||
| 203 | 4 | $names = Utilities\ensureIsArray($names); |
|
| 204 | $names = array_map(function ($name) use ($folderClass) { |
||
| 205 | 4 | return ['DisplayName' => $name, 'FolderClass' => $folderClass]; |
|
| 206 | 4 | }, $names); |
|
| 207 | |||
| 208 | $request = [ |
||
| 209 | 4 | 'Folders' => ['Folder' => $names] |
|
| 210 | 4 | ]; |
|
| 211 | |||
| 212 | 4 | if ($parentFolder !== null) { |
|
| 213 | 4 | $request['ParentFolderId'] = $parentFolder->toArray(true); |
|
| 214 | 4 | } |
|
| 215 | |||
| 216 | 4 | $request = array_merge_recursive($request, $options); |
|
| 217 | |||
| 218 | 4 | $this->client->CreateFolder($request); |
|
| 219 | 4 | return true; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @deprecated Please use API::deleteFolders() instead |
||
| 224 | * |
||
| 225 | * @param BaseFolderIdType $folderId |
||
| 226 | * @param array $options |
||
| 227 | * @return Type |
||
| 228 | */ |
||
| 229 | 3 | public function deleteFolder(BaseFolderIdType $folderId, $options = array()) |
|
| 233 | |||
| 234 | 4 | public function deleteFolders($folders, $options = array()) |
|
| 235 | { |
||
| 236 | 4 | $folderIds = Utilities\getFolderIds($folders); |
|
| 237 | |||
| 238 | $request = [ |
||
| 239 | 4 | 'DeleteType' => 'HardDelete', |
|
| 240 | 'FolderIds' => $folderIds |
||
| 246 | |||
| 247 | public function moveItem(Type\ItemIdType $itemId, BaseFolderIdType $folderId, $options = array()) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
| 261 | * @param array $options |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | 16 | public function deleteItems($items, $options = array()) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param $identifier |
||
| 289 | * @param array $options |
||
| 290 | * @return Type\BaseFolderType |
||
| 291 | */ |
||
| 292 | 8 | View Code Duplication | public function getFolder($identifier, $options = []) |
| 309 | |||
| 310 | /** |
||
| 311 | * Get a folder by it's distinguishedId |
||
| 312 | * |
||
| 313 | * @param string $distinguishedId |
||
| 314 | * @param array $options |
||
| 315 | * @return Type\BaseFolderType |
||
| 316 | */ |
||
| 317 | 4 | public function getFolderByDistinguishedId($distinguishedId, $options = []) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string|BaseFolderIdType $folderId |
||
| 329 | * @param array $options |
||
| 330 | * @return Type\BaseFolderType |
||
| 331 | * @throws API\Exception |
||
| 332 | */ |
||
| 333 | 4 | public function getFolderByFolderId($folderId, $options = []) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param string|BaseFolderIdType $parentFolderId |
||
| 346 | * @param array $options |
||
| 347 | * @return Type\BaseFolderType[] |
||
| 348 | */ |
||
| 349 | 25 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', array $options = array()) |
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $folderName |
||
| 373 | * @param string|BaseFolderIdType $parentFolderId |
||
| 374 | * @param array $options |
||
| 375 | * @return bool|Type\BaseFolderType |
||
| 376 | */ |
||
| 377 | 25 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @param $itemId array|Type\ItemIdType |
||
| 392 | * @param array $options |
||
| 393 | * @return Type |
||
| 394 | */ |
||
| 395 | 5 | View Code Duplication | public function getItem($itemId, $options = array()) |
| 410 | |||
| 411 | /** |
||
| 412 | * Get a list of sync changes on a folder |
||
| 413 | * |
||
| 414 | * @param BaseFolderIdType $folderId |
||
| 415 | * @param null $syncState |
||
| 416 | * @param array $options |
||
| 417 | * @return SyncFolderItemsResponseMessageType |
||
| 418 | */ |
||
| 419 | 2 | public function listItemChanges($folderId, $syncState = null, array $options = array()) |
|
| 440 | |||
| 441 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param Type\ItemIdType $itemId |
||
| 459 | * @param $fromType |
||
| 460 | * @param $destinationType |
||
| 461 | * @param $mailbox |
||
| 462 | * |
||
| 463 | * @return Type\ItemIdType |
||
| 464 | */ |
||
| 465 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
| 485 | * |
||
| 486 | * @return Type\FindItemParentType|Type\FindFolderParentType |
||
| 487 | */ |
||
| 488 | 2 | public function getNextPage($result) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * @param BaseFolderIdType $folderId |
||
| 509 | * @param string $deleteType |
||
| 510 | * @param bool $deleteSubFolders |
||
| 511 | * @param array $options |
||
| 512 | * @return EmptyFolderResponseType |
||
| 513 | */ |
||
| 514 | public function emptyFolder( |
||
| 530 | |||
| 531 | 23 | protected function getDistinguishedFolderId($id = null, $changeKey = null) |
|
| 539 | } |
||
| 540 |