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 |
||
22 | class API |
||
23 | { |
||
24 | protected static $defaultClientOptions = array( |
||
25 | 'version' => ExchangeWebServices::VERSION_2010 |
||
26 | ); |
||
27 | |||
28 | 30 | public function __construct(ExchangeWebServices $client = null) |
|
34 | |||
35 | /** |
||
36 | * @return Type\EmailAddressType |
||
37 | */ |
||
38 | 21 | public function getPrimarySmtpMailbox() |
|
42 | |||
43 | /** |
||
44 | * Storing the API client |
||
45 | * @var ExchangeWebServices |
||
46 | */ |
||
47 | private $client; |
||
48 | |||
49 | /** |
||
50 | * @deprecated This will be removed in 0.9 |
||
51 | * |
||
52 | * @param $className |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getFieldUrisFromClass($className) |
||
56 | { |
||
57 | return FieldURIManager::getFieldUrisFromClass($className); |
||
|
|||
58 | } |
||
59 | |||
60 | public function setupFieldUris() |
||
61 | { |
||
62 | FieldURIManager::setupFieldUris(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @deprecated This will be removed in 0.9. See FieldURIManager |
||
67 | * |
||
68 | * @param $fieldName |
||
69 | * @param string $preference |
||
70 | * @throws ExchangeException |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getFieldUriByName($fieldName, $preference = 'item') |
||
74 | { |
||
75 | return FieldURIManager::getFieldUriByName($fieldName, $preference); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @deprecated This will be removed in 0.9. See FieldURIManager |
||
80 | * |
||
81 | * @param $fieldName |
||
82 | * @param string $preference |
||
83 | * @param bool $entryKey |
||
84 | * @throws ExchangeException |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false) |
||
88 | { |
||
89 | return FieldURIManager::getIndexedFieldUriByName($fieldName, $preference, $entryKey); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get a calendar item |
||
94 | * |
||
95 | * @param string $name |
||
96 | * @return CalendarAPI |
||
97 | */ |
||
98 | 6 | public function getCalendar($name = null) |
|
99 | { |
||
100 | 6 | $calendar = new CalendarAPI(); |
|
101 | 6 | $calendar->setClient($this->getClient()); |
|
102 | 6 | $calendar->pickCalendar($name); |
|
103 | |||
104 | 6 | return $calendar; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $folderName |
||
109 | * @return MailAPI |
||
110 | */ |
||
111 | 8 | public function getMailbox($folderName = null) |
|
112 | { |
||
113 | 6 | $mailApi = new MailAPI(); |
|
114 | 6 | $mailApi->setClient($this->getClient()); |
|
115 | 6 | $mailApi->pickMailFolder($folderName); |
|
116 | |||
117 | 8 | return $mailApi; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Set the API client |
||
122 | * |
||
123 | * @param ExchangeWebServices $client |
||
124 | * @return $this |
||
125 | */ |
||
126 | 30 | public function setClient($client) |
|
127 | { |
||
128 | 30 | $this->client = $client; |
|
129 | |||
130 | 30 | return $this; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get the API client |
||
135 | * |
||
136 | * @return ExchangeWebServices |
||
137 | */ |
||
138 | 28 | public function getClient() |
|
142 | |||
143 | /** |
||
144 | * Instantiate and set a client (ExchangeWebServices) based on the parameters given |
||
145 | * |
||
146 | * @deprecated Since 0.6.3 |
||
147 | * @param $server |
||
148 | * @param $username |
||
149 | * @param $password |
||
150 | * @param array $options |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function buildClient( |
||
154 | $server, |
||
155 | $username, |
||
156 | $password, |
||
157 | $options = [] |
||
158 | ) { |
||
159 | $this->setClient(ExchangeWebServices::fromUsernameAndPassword( |
||
160 | $server, |
||
161 | $username, |
||
162 | $password, |
||
163 | array_replace_recursive(self::$defaultClientOptions, $options) |
||
164 | )); |
||
165 | } |
||
166 | |||
167 | 17 | public static function withUsernameAndPassword($server, $username, $password, $options = []) |
|
176 | |||
177 | 1 | public static function withCallbackToken($server, $token, $options = []) |
|
185 | |||
186 | 1 | public function getPrimarySmptEmailAddress() |
|
194 | |||
195 | 1 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
201 | |||
202 | /** |
||
203 | * Create items through the API client |
||
204 | * |
||
205 | * @param $items |
||
206 | * @param array $options |
||
207 | * @return Type |
||
208 | */ |
||
209 | 13 | public function createItems($items, $options = array()) |
|
226 | |||
227 | 3 | public function updateItems($items, $options = array()) |
|
228 | { |
||
229 | $request = array( |
||
230 | 3 | 'ItemChanges' => $items, |
|
250 | |||
251 | /** |
||
252 | * @param string $itemType |
||
253 | * @param string $uriType |
||
254 | * @param array $changes |
||
255 | * @return array |
||
256 | */ |
||
257 | 3 | protected function buildUpdateItemChanges($itemType, $uriType, $changes) |
|
261 | |||
262 | 2 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
|
285 | |||
286 | 2 | 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 | |||
312 | /** |
||
313 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
314 | * @param array $options |
||
315 | * @return bool |
||
316 | */ |
||
317 | 13 | public function deleteItems($items, $options = array()) |
|
347 | |||
348 | /** |
||
349 | * @param $identifier |
||
350 | * @return Type\BaseFolderType |
||
351 | */ |
||
352 | 20 | public function getFolder($identifier) |
|
366 | |||
367 | /** |
||
368 | * Get a folder by it's distinguishedId |
||
369 | * |
||
370 | * @param string $distinguishedId |
||
371 | * @return Type\BaseFolderType |
||
372 | */ |
||
373 | 20 | public function getFolderByDistinguishedId($distinguishedId) |
|
382 | |||
383 | /** |
||
384 | * @param $folderId |
||
385 | * @return Type\BaseFolderType |
||
386 | */ |
||
387 | 4 | public function getFolderByFolderId($folderId) |
|
393 | |||
394 | /** |
||
395 | * @param string|Type\FolderIdType $parentFolderId |
||
396 | * @param array $options |
||
397 | * @return bool|Type\BaseFolderType |
||
398 | */ |
||
399 | 19 | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
|
424 | |||
425 | /** |
||
426 | * @param string $folderName |
||
427 | * @param string|Type\FolderIdType $parentFolderId |
||
428 | * @param array $options |
||
429 | * @return bool|Type\BaseFolderType |
||
430 | */ |
||
431 | 19 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
443 | |||
444 | /** |
||
445 | * @param $itemId array|Type\ItemIdType |
||
446 | * @param array $options |
||
447 | * @return Type |
||
448 | */ |
||
449 | 4 | View Code Duplication | public function getItem($itemId, $options = array()) |
464 | |||
465 | /** |
||
466 | * Get a list of sync changes on a folder |
||
467 | * |
||
468 | * @param Type\FolderIdType $folderId |
||
469 | * @param null $syncState |
||
470 | * @param array $options |
||
471 | * @return SyncFolderItemsResponseMessageType |
||
472 | */ |
||
473 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
494 | |||
495 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
514 | |||
515 | /** |
||
516 | * @param Type\ItemIdType $itemId |
||
517 | * @param $fromType |
||
518 | * @param $destinationType |
||
519 | * @param $mailbox |
||
520 | * |
||
521 | * @return Type\ItemIdType |
||
522 | */ |
||
523 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
540 | } |
||
541 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.