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 |
||
| 18 | class API |
||
| 19 | { |
||
| 20 | protected static $defaultClientOptions = array( |
||
| 21 | 'version' => ExchangeWebServices::VERSION_2010 |
||
| 22 | ); |
||
| 23 | |||
| 24 | 34 | public function __construct(ExchangeWebServices $client = null) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @return Type\EmailAddressType |
||
| 33 | */ |
||
| 34 | 26 | public function getPrimarySmtpMailbox() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Storing the API client |
||
| 41 | * @var ExchangeWebServices |
||
| 42 | */ |
||
| 43 | private $client; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get a calendar item |
||
| 47 | * |
||
| 48 | * @param string $name |
||
| 49 | * @return CalendarAPI |
||
| 50 | */ |
||
| 51 | 6 | public function getCalendar($name = null) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $folderName |
||
| 62 | * @return MailAPI |
||
| 63 | */ |
||
| 64 | 6 | public function getMailbox($folderName = null) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Set the API client |
||
| 75 | * |
||
| 76 | * @param ExchangeWebServices $client |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | 34 | public function setClient($client) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get the API client |
||
| 88 | * |
||
| 89 | * @return ExchangeWebServices |
||
| 90 | */ |
||
| 91 | 33 | public function getClient() |
|
| 95 | |||
| 96 | 33 | public static function withUsernameAndPassword($server, $username, $password, $options = []) |
|
| 105 | |||
| 106 | 1 | public static function withCallbackToken($server, $token, $options = []) |
|
| 114 | |||
| 115 | 1 | public function getPrimarySmptEmailAddress() |
|
| 123 | |||
| 124 | 1 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Create items through the API client |
||
| 133 | * |
||
| 134 | * @param $items |
||
| 135 | * @param array $options |
||
| 136 | * @return Type |
||
| 137 | */ |
||
| 138 | 16 | public function createItems($items, $options = array()) |
|
| 152 | |||
| 153 | 4 | public function updateItems($items, $options = array()) |
|
| 172 | |||
| 173 | 1 | View Code Duplication | public function createCalendars($names, Type\FolderIdType $parentFolder = null, $options = array()) |
| 198 | |||
| 199 | View Code Duplication | public function createContactsFolder($names, Type\FolderIdType $parentFolder = null, $options = array()) |
|
| 223 | |||
| 224 | 3 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @deprecated Please use API::deleteFolders() instead |
||
| 248 | * |
||
| 249 | * @param Type\FolderIdType $folderId |
||
| 250 | * @param array $options |
||
| 251 | * @return Type |
||
| 252 | */ |
||
| 253 | 3 | public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
|
| 257 | |||
| 258 | 4 | public function deleteFolders($folders, $options = array()) |
|
| 276 | |||
| 277 | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
| 291 | * @param array $options |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | 16 | public function deleteItems($items, $options = array()) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param $identifier |
||
| 319 | * @return Type\BaseFolderType |
||
| 320 | */ |
||
| 321 | 25 | public function getFolder($identifier) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get a folder by it's distinguishedId |
||
| 338 | * |
||
| 339 | * @param string $distinguishedId |
||
| 340 | * @return Type\BaseFolderType |
||
| 341 | */ |
||
| 342 | 25 | public function getFolderByDistinguishedId($distinguishedId) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param $folderId |
||
| 354 | * @return Type\BaseFolderType |
||
| 355 | */ |
||
| 356 | 4 | public function getFolderByFolderId($folderId) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param string|Type\FolderIdType $parentFolderId |
||
| 365 | * @param array $options |
||
| 366 | * @return Type\BaseFolderType[] |
||
| 367 | */ |
||
| 368 | 24 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $folderName |
||
| 394 | * @param string|Type\FolderIdType $parentFolderId |
||
| 395 | * @param array $options |
||
| 396 | * @return bool|Type\BaseFolderType |
||
| 397 | */ |
||
| 398 | 24 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @param $itemId array|Type\ItemIdType |
||
| 413 | * @param array $options |
||
| 414 | * @return Type |
||
| 415 | */ |
||
| 416 | 5 | View Code Duplication | public function getItem($itemId, $options = array()) |
| 431 | |||
| 432 | /** |
||
| 433 | * Get a list of sync changes on a folder |
||
| 434 | * |
||
| 435 | * @param Type\FolderIdType $folderId |
||
| 436 | * @param null $syncState |
||
| 437 | * @param array $options |
||
| 438 | * @return SyncFolderItemsResponseMessageType |
||
| 439 | */ |
||
| 440 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
| 461 | |||
| 462 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param Type\ItemIdType $itemId |
||
| 480 | * @param $fromType |
||
| 481 | * @param $destinationType |
||
| 482 | * @param $mailbox |
||
| 483 | * |
||
| 484 | * @return Type\ItemIdType |
||
| 485 | */ |
||
| 486 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
| 506 | * |
||
| 507 | * @return Type\FindItemParentType|Type\FindFolderParentType |
||
| 508 | */ |
||
| 509 | 2 | public function getNextPage($result) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * @param Type\FolderIdType $folderId |
||
| 530 | * @param string $deleteType |
||
| 531 | * @param bool $deleteSubFolders |
||
| 532 | * @param array $options |
||
| 533 | * @return EmptyFolderResponseType |
||
| 534 | */ |
||
| 535 | public function emptyFolder( |
||
| 551 | } |
||
| 552 |