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 | public function createCalendars($names, Type\FolderIdType $parentFolder = null, $options = array()) |
|
| 207 | |||
| 208 | 3 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @deprecated Please use API::deleteFolders() instead |
||
| 235 | * |
||
| 236 | * @param Type\FolderIdType $folderId |
||
| 237 | * @param array $options |
||
| 238 | * @return Type |
||
| 239 | */ |
||
| 240 | public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
||
| 241 | { |
||
| 242 | return $this->deleteFolders($folderId, $options); |
||
| 243 | } |
||
| 244 | |||
| 245 | 4 | public function deleteFolders($folders, $options = array()) |
|
| 265 | |||
| 266 | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
| 280 | * @param array $options |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 16 | public function deleteItems($items, $options = array()) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param $identifier |
||
| 310 | * @return Type\BaseFolderType |
||
| 311 | */ |
||
| 312 | 25 | public function getFolder($identifier) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get a folder by it's distinguishedId |
||
| 329 | * |
||
| 330 | * @param string $distinguishedId |
||
| 331 | * @return Type\BaseFolderType |
||
| 332 | */ |
||
| 333 | 25 | public function getFolderByDistinguishedId($distinguishedId) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @param $folderId |
||
| 345 | * @return Type\BaseFolderType |
||
| 346 | */ |
||
| 347 | 4 | public function getFolderByFolderId($folderId) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param string|Type\FolderIdType $parentFolderId |
||
| 356 | * @param array $options |
||
| 357 | * @return Type\BaseFolderType[] |
||
| 358 | */ |
||
| 359 | 24 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $folderName |
||
| 385 | * @param string|Type\FolderIdType $parentFolderId |
||
| 386 | * @param array $options |
||
| 387 | * @return bool|Type\BaseFolderType |
||
| 388 | */ |
||
| 389 | 24 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @param $itemId array|Type\ItemIdType |
||
| 404 | * @param array $options |
||
| 405 | * @return Type |
||
| 406 | */ |
||
| 407 | 5 | View Code Duplication | public function getItem($itemId, $options = array()) |
| 422 | |||
| 423 | /** |
||
| 424 | * Get a list of sync changes on a folder |
||
| 425 | * |
||
| 426 | * @param Type\FolderIdType $folderId |
||
| 427 | * @param null $syncState |
||
| 428 | * @param array $options |
||
| 429 | * @return SyncFolderItemsResponseMessageType |
||
| 430 | */ |
||
| 431 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
| 452 | |||
| 453 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param Type\ItemIdType $itemId |
||
| 475 | * @param $fromType |
||
| 476 | * @param $destinationType |
||
| 477 | * @param $mailbox |
||
| 478 | * |
||
| 479 | * @return Type\ItemIdType |
||
| 480 | */ |
||
| 481 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
| 501 | * |
||
| 502 | * @return Type\FindItemParentType|Type\FindFolderParentType |
||
| 503 | */ |
||
| 504 | 2 | public function getNextPage($result) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param Type\FolderIdType $folderId |
||
| 525 | * @param string $deleteType |
||
| 526 | * @param bool $deleteSubFolders |
||
| 527 | * @param array $options |
||
| 528 | * @return EmptyFolderResponseType |
||
| 529 | */ |
||
| 530 | public function emptyFolder( |
||
| 546 | } |
||
| 547 |