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()) |
|
| 155 | |||
| 156 | 4 | public function updateItems($items, $options = array()) |
|
| 179 | |||
| 180 | 1 | View Code Duplication | public function createCalendars($names, Type\FolderIdType $parentFolder = null, $options = array()) |
| 207 | |||
| 208 | View Code Duplication | public function createContactsFolder($names, Type\FolderIdType $parentFolder = null, $options = array()) |
|
| 235 | |||
| 236 | 3 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @deprecated Please use API::deleteFolders() instead |
||
| 263 | * |
||
| 264 | * @param Type\FolderIdType $folderId |
||
| 265 | * @param array $options |
||
| 266 | * @return Type |
||
| 267 | */ |
||
| 268 | public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
||
| 269 | { |
||
| 270 | return $this->deleteFolders($folderId, $options); |
||
| 271 | } |
||
| 272 | |||
| 273 | 4 | public function deleteFolders($folders, $options = array()) |
|
| 293 | |||
| 294 | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
| 308 | * @param array $options |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 16 | public function deleteItems($items, $options = array()) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param $identifier |
||
| 338 | * @return Type\BaseFolderType |
||
| 339 | */ |
||
| 340 | 25 | public function getFolder($identifier) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Get a folder by it's distinguishedId |
||
| 357 | * |
||
| 358 | * @param string $distinguishedId |
||
| 359 | * @return Type\BaseFolderType |
||
| 360 | */ |
||
| 361 | 25 | public function getFolderByDistinguishedId($distinguishedId) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @param $folderId |
||
| 373 | * @return Type\BaseFolderType |
||
| 374 | */ |
||
| 375 | 4 | public function getFolderByFolderId($folderId) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param string|Type\FolderIdType $parentFolderId |
||
| 384 | * @param array $options |
||
| 385 | * @return Type\BaseFolderType[] |
||
| 386 | */ |
||
| 387 | 24 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $folderName |
||
| 413 | * @param string|Type\FolderIdType $parentFolderId |
||
| 414 | * @param array $options |
||
| 415 | * @return bool|Type\BaseFolderType |
||
| 416 | */ |
||
| 417 | 24 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @param $itemId array|Type\ItemIdType |
||
| 432 | * @param array $options |
||
| 433 | * @return Type |
||
| 434 | */ |
||
| 435 | 5 | View Code Duplication | public function getItem($itemId, $options = array()) |
| 450 | |||
| 451 | /** |
||
| 452 | * Get a list of sync changes on a folder |
||
| 453 | * |
||
| 454 | * @param Type\FolderIdType $folderId |
||
| 455 | * @param null $syncState |
||
| 456 | * @param array $options |
||
| 457 | * @return SyncFolderItemsResponseMessageType |
||
| 458 | */ |
||
| 459 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
| 480 | |||
| 481 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param Type\ItemIdType $itemId |
||
| 503 | * @param $fromType |
||
| 504 | * @param $destinationType |
||
| 505 | * @param $mailbox |
||
| 506 | * |
||
| 507 | * @return Type\ItemIdType |
||
| 508 | */ |
||
| 509 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
| 529 | * |
||
| 530 | * @return Type\FindItemParentType|Type\FindFolderParentType |
||
| 531 | */ |
||
| 532 | 2 | public function getNextPage($result) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * @param Type\FolderIdType $folderId |
||
| 553 | * @param string $deleteType |
||
| 554 | * @param bool $deleteSubFolders |
||
| 555 | * @param array $options |
||
| 556 | * @return EmptyFolderResponseType |
||
| 557 | */ |
||
| 558 | public function emptyFolder( |
||
| 574 | } |
||
| 575 |