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 AutodiscoveryManager 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 AutodiscoveryManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class AutodiscoveryManager |
||
30 | { |
||
31 | /** |
||
32 | * The path appended to the various schemes and hostnames used during |
||
33 | * autodiscovery. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | const AUTODISCOVER_PATH = '/autodiscover/autodiscover.xml'; |
||
38 | |||
39 | /** |
||
40 | * Server was discovered using the TLD method. |
||
41 | * |
||
42 | * @var integer |
||
43 | */ |
||
44 | const AUTODISCOVERED_VIA_TLD = 10; |
||
45 | |||
46 | /** |
||
47 | * Server was discovered using the subdomain method. |
||
48 | * |
||
49 | * @var integer |
||
50 | */ |
||
51 | const AUTODISCOVERED_VIA_SUBDOMAIN = 11; |
||
52 | |||
53 | /** |
||
54 | * Server was discovered using the unauthenticated GET method. |
||
55 | * |
||
56 | * @var integer |
||
57 | */ |
||
58 | const AUTODISCOVERED_VIA_UNAUTHENTICATED_GET = 12; |
||
59 | |||
60 | /** |
||
61 | * Server was discovered using the DNS SRV redirect method. |
||
62 | * |
||
63 | * @var integer |
||
64 | */ |
||
65 | const AUTODISCOVERED_VIA_SRV_RECORD = 13; |
||
66 | |||
67 | /** |
||
68 | * Server was discovered using the HTTP redirect method. |
||
69 | * |
||
70 | * @var integer |
||
71 | * |
||
72 | * @todo We do not currently support this. |
||
73 | */ |
||
74 | const AUTODISCOVERED_VIA_RESPONSE_REDIRECT = 14; |
||
75 | |||
76 | /** |
||
77 | * The email address to attempt autodiscovery against. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $email; |
||
82 | |||
83 | /** |
||
84 | * The password to present during autodiscovery. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $password; |
||
89 | |||
90 | /** |
||
91 | * The Exchange username to use during authentication. If unspecified, |
||
92 | * the provided email address will be used as the username. |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $username; |
||
97 | |||
98 | /** |
||
99 | * The top-level domain name, extracted from the provided email address. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $tld; |
||
104 | |||
105 | /** |
||
106 | * The Autodiscover XML request. Since it's used repeatedly, it's cached |
||
107 | * in this property to avoid redundant re-generation. |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $requestxml; |
||
112 | |||
113 | /** |
||
114 | * The Certificate Authority path. Should point to a directory containing |
||
115 | * one or more certificates to use in SSL verification. |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | protected $capath; |
||
120 | |||
121 | /** |
||
122 | * The path to a specific Certificate Authority file. Get one and use it |
||
123 | * for full Autodiscovery compliance. |
||
124 | * |
||
125 | * @var string |
||
126 | * |
||
127 | * @link http://curl.haxx.se/ca/cacert.pem |
||
128 | * @link http://curl.haxx.se/ca/ |
||
129 | */ |
||
130 | protected $cainfo; |
||
131 | |||
132 | /** |
||
133 | * Skip SSL verification. Bad idea, and violates the strict Autodiscover |
||
134 | * protocol. But, here in case you have no other option. |
||
135 | * Defaults to FALSE. |
||
136 | * |
||
137 | * @var boolean |
||
138 | */ |
||
139 | protected $skip_ssl_verification = false; |
||
140 | |||
141 | /** |
||
142 | * An associative array of response headers that resulted from the |
||
143 | * last request. Keys are lowercased for easy checking. |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | public $last_response_headers; |
||
148 | |||
149 | /** |
||
150 | * The output of curl_info() relating to the most recent cURL request. |
||
151 | * |
||
152 | * @var array |
||
153 | */ |
||
154 | public $last_info; |
||
155 | |||
156 | /** |
||
157 | * The cURL error code associated with the most recent cURL request. |
||
158 | * |
||
159 | * @var integer |
||
160 | */ |
||
161 | public $last_curl_errno; |
||
162 | |||
163 | /** |
||
164 | * Human-readable description of the most recent cURL error. |
||
165 | * |
||
166 | * @var string |
||
167 | */ |
||
168 | public $last_curl_error; |
||
169 | |||
170 | /** |
||
171 | * The value in seconds to use for Autodiscover host connection timeouts. |
||
172 | * Default connection timeout is 2 seconds, so that unresponsive methods |
||
173 | * can be bypassed quickly. |
||
174 | * |
||
175 | * @var integer |
||
176 | */ |
||
177 | public $connection_timeout = 2; |
||
178 | |||
179 | /** |
||
180 | * Information about an Autodiscover Response containing an error will |
||
181 | * be stored here. |
||
182 | * |
||
183 | * @var mixed |
||
184 | */ |
||
185 | public $error = false; |
||
186 | |||
187 | /** |
||
188 | * Information about an Autodiscover Response with a redirect will be |
||
189 | * retained here. |
||
190 | * |
||
191 | * @var mixed |
||
192 | */ |
||
193 | public $redirect = false; |
||
194 | |||
195 | /** |
||
196 | * A successful, non-error and non-redirect parsed Autodiscover response |
||
197 | * will be stored here. |
||
198 | * |
||
199 | * @var mixed |
||
200 | */ |
||
201 | public $discovered = null; |
||
202 | |||
203 | /** |
||
204 | * Constructor for the Autodiscovery class. |
||
205 | * |
||
206 | * @param string $email |
||
207 | * @param string $password |
||
208 | * @param string $username If left blank, the email provided will be used. |
||
209 | */ |
||
210 | public function __construct($email, $password, $username = null) |
||
211 | { |
||
212 | $this->email = $email; |
||
213 | $this->password = $password; |
||
214 | if ($username === null) { |
||
215 | $this->username = $email; |
||
216 | } else { |
||
217 | $this->username = $username; |
||
218 | } |
||
219 | |||
220 | $this->setTLD(); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Execute the full discovery chain of events in the correct sequence |
||
225 | * until a valid response is received, or all methods have failed. |
||
226 | * |
||
227 | * @return An AUTODISCOVERED_VIA_* constant or FALSE on failure. |
||
228 | */ |
||
229 | public function discover() |
||
247 | |||
248 | /** |
||
249 | * Return the settings discovered from the Autodiscover process. |
||
250 | * |
||
251 | * NULL indicates discovery hasn't completed (or been attempted) |
||
252 | * FALSE indicates discovery wasn't successful. Check for errors |
||
253 | * or redirects. |
||
254 | * An array will be returned with discovered settings on success. |
||
255 | * |
||
256 | * @return mixed |
||
257 | */ |
||
258 | public function discoveredSettings() |
||
262 | |||
263 | /** |
||
264 | * Toggle skipping of SSL verification in cURL requests. |
||
265 | * |
||
266 | * @param boolean $skip To skip, or not. |
||
267 | * @return AutodiscoveryManager |
||
268 | */ |
||
269 | public function skipSSLVerification($skip = true) |
||
275 | |||
276 | /** |
||
277 | * Parse the hex ServerVersion value and return a valid |
||
278 | * \PhpEws\EwsConnection::VERSION_* constant. |
||
279 | * |
||
280 | * @return string|boolean A known version constant, or FALSE if it could not |
||
281 | * be determined. |
||
282 | * |
||
283 | * @link http://msdn.microsoft.com/en-us/library/bb204122(v=exchg.140).aspx |
||
284 | * @link http://blogs.msdn.com/b/pcreehan/archive/2009/09/21/parsing-serverversion-when-an-int-is-really-5-ints.aspx |
||
285 | * @link http://office.microsoft.com/en-us/outlook-help/determine-the-version-of-microsoft-exchange-server-my-account-connects-to-HA001191800.aspx |
||
286 | */ |
||
287 | public function parseServerVersion($version_hex) |
||
334 | |||
335 | /** |
||
336 | * Method to return a new EwsConnection object, auto-configured |
||
337 | * with the proper hostname. |
||
338 | * |
||
339 | * @return EwsConnection|false EwsConnection on success, false on failure. |
||
340 | */ |
||
341 | public function createNewConnection() |
||
390 | |||
391 | /** |
||
392 | * Static method may fail if there are issues surrounding SSL certificates. |
||
393 | * In such cases, set up the object as needed, and then call newEWS(). |
||
394 | * |
||
395 | * @param string $email |
||
396 | * @param string $password |
||
397 | * @param string $username If left blank, the email provided will be used. |
||
398 | * |
||
399 | * @return EwsConnection |
||
400 | */ |
||
401 | public static function getConnection($email, $password, $username = null) |
||
407 | |||
408 | /** |
||
409 | * Perform an NTLM authenticated HTTPS POST to the top-level |
||
410 | * domain of the email address. |
||
411 | * |
||
412 | * @return An AUTODISCOVERED_VIA_* constant or FALSE on failure. |
||
413 | */ |
||
414 | View Code Duplication | public function tryTLD() |
|
424 | |||
425 | /** |
||
426 | * Perform an NTLM authenticated HTTPS POST to the 'autodiscover' |
||
427 | * subdomain of the email address' TLD. |
||
428 | * |
||
429 | * @return An AUTODISCOVERED_VIA_* constant or FALSE on failure. |
||
430 | */ |
||
431 | View Code Duplication | public function trySubdomain() |
|
441 | |||
442 | /** |
||
443 | * Perform an unauthenticated HTTP GET in an attempt to get redirected |
||
444 | * via 302 to the correct location to perform the HTTPS POST. |
||
445 | * |
||
446 | * @return An AUTODISCOVERED_VIA_* constant or FALSE on failure. |
||
447 | */ |
||
448 | public function trySubdomainUnauthenticatedGet() |
||
487 | |||
488 | /** |
||
489 | * Attempt to retrieve the autodiscover host from an SRV DNS record. |
||
490 | * |
||
491 | * @link http://support.microsoft.com/kb/940881 |
||
492 | * @return AutodiscoveryManager::AUTODISCOVERED_VIA_SRV_RECORD or false |
||
493 | */ |
||
494 | public function trySRVRecord() |
||
509 | |||
510 | /** |
||
511 | * Set the path to the file to be used by CURLOPT_CAINFO. |
||
512 | * |
||
513 | * @param string $path Path to a certificate file such as cacert.pem |
||
514 | * @return AutodiscoveryManager |
||
515 | */ |
||
516 | public function setCAInfo($path) |
||
524 | |||
525 | /** |
||
526 | * Set the path to the file to be used by CURLOPT_CAPATH. |
||
527 | * |
||
528 | * @param string $path Path to a directory containing one or more CA |
||
529 | * certificates. |
||
530 | * @return AutodiscoveryManager |
||
531 | */ |
||
532 | public function setCAPath($path) |
||
540 | |||
541 | /** |
||
542 | * Set a connection timeout for the POST methods. |
||
543 | * |
||
544 | * @param integer $seconds Seconds to wait for a connection. |
||
545 | * @return AutodiscoveryManager |
||
546 | */ |
||
547 | public function setConnectionTimeout($seconds) |
||
553 | |||
554 | /** |
||
555 | * Perform the NTLM authenticated post against one of the chosen |
||
556 | * endpoints. |
||
557 | * |
||
558 | * @param string $url URL to try posting to |
||
559 | * @param integer $timeout Overall cURL timeout for this request |
||
560 | * @return boolean |
||
561 | */ |
||
562 | public function doNTLMPost($url, $timeout = 6) |
||
614 | |||
615 | /** |
||
616 | * Parse the Autoresponse Payload, particularly to determine if an |
||
617 | * additional request is necessary. |
||
618 | * |
||
619 | * @return mixed FALSE if response isn't XML or parsed response array |
||
620 | */ |
||
621 | protected function parseAutodiscoverResponse() |
||
655 | |||
656 | /** |
||
657 | * Set the top-level domain to be used with autodiscover attempts based |
||
658 | * on the provided email address. |
||
659 | * |
||
660 | * @return boolean |
||
661 | */ |
||
662 | protected function setTLD() |
||
672 | |||
673 | /** |
||
674 | * Reset the response-related structures. Called before making a new |
||
675 | * request. |
||
676 | * |
||
677 | * @return AutodiscoveryManager |
||
678 | */ |
||
679 | public function reset() |
||
688 | |||
689 | /** |
||
690 | * Return the generated Autodiscover XML request body. |
||
691 | * |
||
692 | * @return string |
||
693 | */ |
||
694 | public function getAutodiscoverRequest() |
||
722 | |||
723 | /** |
||
724 | * Utility function to pick headers off of the incoming cURL response. |
||
725 | * Used with CURLOPT_HEADERFUNCTION. |
||
726 | * |
||
727 | * @param resource $_ch cURL handle |
||
728 | * @param string $str Header string to read |
||
729 | * @return integer Bytes read |
||
730 | */ |
||
731 | public function readHeaders($_ch, $str) |
||
742 | |||
743 | /** |
||
744 | * Utility function to parse XML payloads from the response into easier |
||
745 | * to manage associative arrays. |
||
746 | * |
||
747 | * @param string $xml XML to parse |
||
748 | * @return array |
||
749 | */ |
||
750 | public function responseToArray($xml) |
||
758 | |||
759 | /** |
||
760 | * Recursive method for parsing DOM nodes. |
||
761 | * |
||
762 | * @link https://github.com/gaarf/XML-string-to-PHP-array |
||
763 | * @param object $node DOMNode object |
||
764 | * @return mixed |
||
765 | */ |
||
766 | protected function nodeToArray($node) |
||
815 | } |
||
816 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.