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 ExchangeAutodiscover 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 ExchangeAutodiscover, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ExchangeAutodiscover |
||
10 | { |
||
11 | protected $autodiscoverPath = '/autodiscover/autodiscover.xml'; |
||
12 | |||
13 | /** |
||
14 | * @var Client |
||
15 | */ |
||
16 | protected $httpPlayback; |
||
17 | |||
18 | 2 | protected function __construct() |
|
21 | |||
22 | /** |
||
23 | * Parse the hex ServerVersion value and return a valid |
||
24 | * ExchangeWebServices::VERSION_* constant. |
||
25 | * |
||
26 | * @param $versionHex |
||
27 | * @return string|boolean A known version constant, or FALSE if it could not |
||
28 | * be determined. |
||
29 | * |
||
30 | * @link http://msdn.microsoft.com/en-us/library/bb204122(v=exchg.140).aspx |
||
31 | * @link http://blogs.msdn.com/b/pcreehan/archive/2009/09/21/parsing-serverversion-when-an-int-is-really-5-ints.aspx |
||
32 | */ |
||
33 | 5 | protected function parseServerVersion($versionHex) |
|
50 | |||
51 | /** |
||
52 | * @param string $email |
||
53 | * @param string $password |
||
54 | * @param string $username |
||
55 | * @param array $options |
||
56 | * |
||
57 | * @return API |
||
58 | * @throws AutodiscoverFailed |
||
59 | */ |
||
60 | 2 | protected function newAPI($email, $password, $username = null, $options = []) |
|
82 | |||
83 | 1 | protected function getServerVersionFromResponse($response) |
|
96 | |||
97 | 1 | protected function getServerFromResponse($response) |
|
107 | |||
108 | /** |
||
109 | * Static method may fail if there are issues surrounding SSL certificates. |
||
110 | * In such cases, set up the object as needed, and then call newEWS(). |
||
111 | * |
||
112 | * @param string $email |
||
113 | * @param string $password |
||
114 | * @param string $username If left blank, the email provided will be used. |
||
115 | * @throws AutodiscoverFailed |
||
116 | * @return API |
||
117 | */ |
||
118 | 2 | public static function getAPI($email, $password, $username = null, $options = []) |
|
124 | |||
125 | /** |
||
126 | * Execute the full discovery chain of events in the correct sequence |
||
127 | * until a valid response is received, or all methods have failed. |
||
128 | * |
||
129 | * @param string $email |
||
130 | * @param string $password |
||
131 | * @param string $username |
||
132 | * @return string The discovered settings |
||
133 | * @throws AutodiscoverFailed |
||
134 | */ |
||
135 | 2 | protected function discover($email, $password, $username) |
|
157 | |||
158 | /** |
||
159 | * Perform an NTLM authenticated HTTPS POST to the top-level |
||
160 | * domain of the email address. |
||
161 | * |
||
162 | * @param string $email |
||
163 | * @param string $password |
||
164 | * @param string $username |
||
165 | * |
||
166 | * @return string The discovered settings |
||
167 | */ |
||
168 | 2 | View Code Duplication | protected function tryTopLevelDomain($email, $password, $username) |
175 | |||
176 | /** |
||
177 | * Perform an NTLM authenticated HTTPS POST to the 'autodiscover' |
||
178 | * subdomain of the email address' TLD. |
||
179 | * |
||
180 | * @param string $email |
||
181 | * @param string $password |
||
182 | * @param string $username |
||
183 | * |
||
184 | * @return string The discovered settings |
||
185 | */ |
||
186 | 2 | View Code Duplication | protected function tryAutoDiscoverSubDomain($email, $password, $username) |
193 | |||
194 | /** |
||
195 | * Perform an unauthenticated HTTP GET in an attempt to get redirected |
||
196 | * via 302 to the correct location to perform the HTTPS POST. |
||
197 | * |
||
198 | * @param string $email |
||
199 | * @param string $password |
||
200 | * @param string $username |
||
201 | * |
||
202 | * @return string The discovered settings |
||
203 | */ |
||
204 | 2 | protected function trySubdomainUnauthenticatedGet($email, $password, $username) |
|
230 | |||
231 | /** |
||
232 | * Attempt to retrieve the autodiscover host from an SRV DNS record. |
||
233 | * |
||
234 | * @link http://support.microsoft.com/kb/940881 |
||
235 | * |
||
236 | * @param string $email |
||
237 | * @param string $password |
||
238 | * @param string $username |
||
239 | * |
||
240 | * @return string The discovered settings |
||
241 | */ |
||
242 | 1 | protected function trySRVRecord($email, $password, $username) |
|
256 | |||
257 | /** |
||
258 | * Perform the NTLM authenticated post against one of the chosen |
||
259 | * endpoints. |
||
260 | * |
||
261 | * @param string $url URL to try posting to |
||
262 | * @param string $email |
||
263 | * @param string $password |
||
264 | * @param string $username |
||
265 | * |
||
266 | * @return string The discovered settings |
||
267 | */ |
||
268 | 2 | protected function doNTLMPost($url, $email, $password, $username) |
|
300 | |||
301 | /** |
||
302 | * Parse the Autoresponse Payload, particularly to determine if an |
||
303 | * additional request is necessary. |
||
304 | * |
||
305 | * @param $response |
||
306 | * @return array|bool |
||
307 | * @throws AutodiscoverFailed |
||
308 | */ |
||
309 | 1 | protected function parseAutodiscoverResponse($response) |
|
329 | |||
330 | /** |
||
331 | * Get a top level domain based on an email address |
||
332 | * |
||
333 | * @param string $email |
||
334 | * @return string|false |
||
335 | */ |
||
336 | 2 | protected function getTopLevelDomainFromEmail($email) |
|
345 | |||
346 | /** |
||
347 | * Utility function to parse XML payloads from the response into easier |
||
348 | * to manage associative arrays. |
||
349 | * |
||
350 | * @param string $xml XML to parse |
||
351 | * @return array |
||
352 | */ |
||
353 | 1 | protected function responseToArray($xml) |
|
359 | |||
360 | /** |
||
361 | * @param $majorVersion |
||
362 | * @param $minorVersion |
||
363 | * @return bool|mixed |
||
364 | */ |
||
365 | 1 | protected function parseVersionBefore2013($majorVersion, $minorVersion) |
|
389 | |||
390 | 4 | protected function parseVersionAfter2013($majorVersion, $minorVersion, $buildVersion) |
|
398 | } |
||
399 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.