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 | 35 | 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 | 35 | public function setClient($client) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Get the API client |
||
| 89 | * |
||
| 90 | * @return ExchangeWebServices |
||
| 91 | */ |
||
| 92 | 34 | 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 function getPrimarySmptEmailAddress() |
|
| 124 | |||
| 125 | 1 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Create items through the API client |
||
| 134 | * |
||
| 135 | * @param $items |
||
| 136 | * @param array $options |
||
| 137 | * @return Type |
||
| 138 | */ |
||
| 139 | 16 | public function createItems($items, $options = array()) |
|
| 153 | |||
| 154 | 4 | public function updateItems($items, $options = array()) |
|
| 173 | |||
| 174 | 1 | View Code Duplication | public function createCalendars($names, BaseFolderIdType $parentFolder = null, $options = array()) |
| 182 | |||
| 183 | View Code Duplication | public function createContactsFolder($names, BaseFolderIdType $parentFolder = null, $options = array()) |
|
| 191 | |||
| 192 | 4 | public function createFolders($names, BaseFolderIdType $parentFolder, $options = array(), $folderClass = null) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @deprecated Please use API::deleteFolders() instead |
||
| 215 | * |
||
| 216 | * @param BaseFolderIdType $folderId |
||
| 217 | * @param array $options |
||
| 218 | * @return Type |
||
| 219 | */ |
||
| 220 | 3 | public function deleteFolder(BaseFolderIdType $folderId, $options = array()) |
|
| 224 | |||
| 225 | 4 | public function deleteFolders($folders, $options = array()) |
|
| 237 | |||
| 238 | public function moveItem(Type\ItemIdType $itemId, BaseFolderIdType $folderId, $options = array()) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
| 252 | * @param array $options |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 16 | public function deleteItems($items, $options = array()) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param $identifier |
||
| 280 | * @return Type\BaseFolderType |
||
| 281 | */ |
||
| 282 | 8 | public function getFolder($identifier) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get a folder by it's distinguishedId |
||
| 299 | * |
||
| 300 | * @param string $distinguishedId |
||
| 301 | * @return Type\BaseFolderType |
||
| 302 | */ |
||
| 303 | 4 | public function getFolderByDistinguishedId($distinguishedId) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param string|BaseFolderIdType $folderId |
||
| 315 | * @return Type\BaseFolderType |
||
| 316 | */ |
||
| 317 | 4 | public function getFolderByFolderId($folderId) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string|BaseFolderIdType $parentFolderId |
||
| 330 | * @param array $options |
||
| 331 | * @return Type\BaseFolderType[] |
||
| 332 | */ |
||
| 333 | 25 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', array $options = array()) |
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $folderName |
||
| 357 | * @param string|BaseFolderIdType $parentFolderId |
||
| 358 | * @param array $options |
||
| 359 | * @return bool|Type\BaseFolderType |
||
| 360 | */ |
||
| 361 | 25 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param $itemId array|Type\ItemIdType |
||
| 376 | * @param array $options |
||
| 377 | * @return Type |
||
| 378 | */ |
||
| 379 | 5 | public function getItem($itemId, $options = array()) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Get a list of sync changes on a folder |
||
| 397 | * |
||
| 398 | * @param BaseFolderIdType $folderId |
||
| 399 | * @param null $syncState |
||
| 400 | * @param array $options |
||
| 401 | * @return SyncFolderItemsResponseMessageType |
||
| 402 | */ |
||
| 403 | 2 | public function listItemChanges($folderId, $syncState = null, array $options = array()) |
|
| 424 | |||
| 425 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param Type\ItemIdType $itemId |
||
| 443 | * @param $fromType |
||
| 444 | * @param $destinationType |
||
| 445 | * @param $mailbox |
||
| 446 | * |
||
| 447 | * @return Type\ItemIdType |
||
| 448 | */ |
||
| 449 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
| 469 | * |
||
| 470 | * @return Type\FindItemParentType|Type\FindFolderParentType |
||
| 471 | */ |
||
| 472 | 2 | public function getNextPage($result) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @param BaseFolderIdType $folderId |
||
| 493 | * @param string $deleteType |
||
| 494 | * @param bool $deleteSubFolders |
||
| 495 | * @param array $options |
||
| 496 | * @return EmptyFolderResponseType |
||
| 497 | */ |
||
| 498 | public function emptyFolder( |
||
| 514 | |||
| 515 | 23 | protected function getDistinguishedFolderId($id = null, $changeKey = null) |
|
| 523 | } |
||
| 524 |