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 | 23 | ||
| 24 | public function __construct(ExchangeWebServices $client = null) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return Type\EmailAddressType |
||
| 33 | 15 | */ |
|
| 34 | public function getPrimarySmtpMailbox() |
||
| 38 | |||
| 39 | private $fieldUris = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Storing the API client |
||
| 43 | * @var ExchangeWebServices |
||
| 44 | */ |
||
| 45 | private $client; |
||
| 46 | 4 | ||
| 47 | public function setupFieldUris() |
||
| 72 | 4 | ||
| 73 | public function getFieldUriByName($fieldName, $preference = 'item') |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get a calendar item |
||
| 99 | * |
||
| 100 | * @param string $name |
||
| 101 | * @return CalendarAPI |
||
| 102 | 6 | */ |
|
| 103 | public function getCalendar($name = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $folderName |
||
| 114 | * @return MailAPI |
||
| 115 | 8 | */ |
|
| 116 | public function getMailbox($folderName = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set the API client |
||
| 127 | * |
||
| 128 | * @param ExchangeWebServices $client |
||
| 129 | * @return $this |
||
| 130 | 23 | */ |
|
| 131 | public function setClient($client) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the API client |
||
| 139 | * |
||
| 140 | * @return ExchangeWebServices |
||
| 141 | 22 | */ |
|
| 142 | public function getClient() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Instantiate and set a client (ExchangeWebServices) based on the parameters given |
||
| 149 | * |
||
| 150 | * @deprecated Since 0.6.3 |
||
| 151 | * @param $server |
||
| 152 | * @param $username |
||
| 153 | * @param $password |
||
| 154 | * @param array $options |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function buildClient( |
||
| 170 | 9 | ||
| 171 | public static function withUsernameAndPassword($server, $username, $password, $options = [ ]) |
||
| 180 | 1 | ||
| 181 | public static function withCallbackToken($server, $token, $options = [ ]) |
||
| 189 | |||
| 190 | public function getPrimarySmptEmailAddress() |
||
| 198 | |||
| 199 | public function setPrimarySmtpEmailAddress($emailAddress) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Create items through the API client |
||
| 208 | * |
||
| 209 | * @param $items |
||
| 210 | * @param array $options |
||
| 211 | * @return API\CreateItemResponseType |
||
| 212 | 9 | */ |
|
| 213 | public function createItems($items, $options = array()) |
||
| 230 | 2 | ||
| 231 | public function updateItems($items, $options = array()) |
||
| 245 | 2 | ||
| 246 | protected function buildUpdateItemChanges($itemType, $uriType, $changes) |
||
| 262 | |||
| 263 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
||
| 286 | |||
| 287 | View Code Duplication | public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
|
| 299 | |||
| 300 | View Code Duplication | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
|
| 311 | 9 | ||
| 312 | 8 | /** |
|
| 313 | 8 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
|
| 314 | 9 | * @param array $options |
|
| 315 | 9 | * @return bool |
|
| 316 | 9 | */ |
|
| 317 | 9 | public function deleteItems($items, $options = array()) |
|
| 347 | |||
| 348 | 15 | /** |
|
| 349 | 15 | * @param $identifier |
|
| 350 | * @return Type\BaseFolderType |
||
| 351 | */ |
||
| 352 | public function getFolder($identifier) |
||
| 365 | 15 | ||
| 366 | /** |
||
| 367 | * Get a folder by it's distinguishedId |
||
| 368 | * |
||
| 369 | * @param string $distinguishedId |
||
| 370 | * @return Type\BaseFolderType |
||
| 371 | */ |
||
| 372 | 4 | public function getFolderByDistinguishedId($distinguishedId) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param $folderId |
||
| 384 | 14 | * @return Type\BaseFolderType |
|
| 385 | */ |
||
| 386 | 14 | public function getFolderByFolderId($folderId) |
|
| 392 | |||
| 393 | /** |
||
| 394 | 14 | * @param string|Type\FolderIdType $parentFolderId |
|
| 395 | * @param array $options |
||
| 396 | 14 | * @return bool|Type\BaseFolderType |
|
| 397 | 14 | */ |
|
| 398 | 14 | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
|
| 422 | |||
| 423 | 6 | /** |
|
| 424 | * @param $folderName |
||
| 425 | * @param string|Type\FolderIdType $parentFolderId |
||
| 426 | * @param array $options |
||
| 427 | * @return bool|Type\BaseFolderType |
||
| 428 | */ |
||
| 429 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
||
| 441 | 1 | ||
| 442 | 1 | /** |
|
| 443 | * @param $itemId array|Type\ItemIdType |
||
| 444 | 1 | * @param array $options |
|
| 445 | * @return Type |
||
| 446 | 1 | */ |
|
| 447 | public function getItem($itemId, $options = array()) |
||
| 462 | 2 | ||
| 463 | /** |
||
| 464 | 2 | * Get a list of sync changes on a folder |
|
| 465 | * |
||
| 466 | 2 | * @param Type\FolderIdType $folderId |
|
| 467 | * @param null $syncState |
||
| 468 | * @param array $options |
||
| 469 | * @return SyncFolderItemsResponseMessageType |
||
| 470 | */ |
||
| 471 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
| 491 | } |
||
| 492 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: