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 Autodiscover 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 Autodiscover, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Autodiscover |
||
42 | { |
||
43 | /** |
||
44 | * The path appended to the various schemes and hostnames used during |
||
45 | * autodiscovery. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const AUTODISCOVER_PATH = '/autodiscover/autodiscover.xml'; |
||
50 | |||
51 | /** |
||
52 | * Server was discovered using the TLD method. |
||
53 | * |
||
54 | * @var integer |
||
55 | */ |
||
56 | const AUTODISCOVERED_VIA_TLD = 10; |
||
57 | |||
58 | /** |
||
59 | * Server was discovered using the subdomain method. |
||
60 | * |
||
61 | * @var integer |
||
62 | */ |
||
63 | const AUTODISCOVERED_VIA_SUBDOMAIN = 11; |
||
64 | |||
65 | /** |
||
66 | * Server was discovered using the unauthenticated GET method. |
||
67 | * |
||
68 | * @var integer |
||
69 | */ |
||
70 | const AUTODISCOVERED_VIA_UNAUTHENTICATED_GET = 12; |
||
71 | |||
72 | /** |
||
73 | * Server was discovered using the DNS SRV redirect method. |
||
74 | * |
||
75 | * @var integer |
||
76 | */ |
||
77 | const AUTODISCOVERED_VIA_SRV_RECORD = 13; |
||
78 | |||
79 | /** |
||
80 | * Server was discovered using the HTTP redirect method. |
||
81 | * |
||
82 | * @var integer |
||
83 | * |
||
84 | * @todo We do not currently support this. |
||
85 | */ |
||
86 | const AUTODISCOVERED_VIA_RESPONSE_REDIRECT = 14; |
||
87 | |||
88 | /** |
||
89 | * The email address to attempt autodiscovery against. |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $email; |
||
94 | |||
95 | /** |
||
96 | * The password to present during autodiscovery. |
||
97 | * |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $password; |
||
101 | |||
102 | /** |
||
103 | * The Exchange username to use during authentication. If unspecified, |
||
104 | * the provided email address will be used as the username. |
||
105 | * |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $username; |
||
109 | |||
110 | /** |
||
111 | * The top-level domain name, extracted from the provided email address. |
||
112 | * |
||
113 | * @var string |
||
114 | */ |
||
115 | protected $tld; |
||
116 | |||
117 | /** |
||
118 | * The Autodiscover XML request. Since it's used repeatedly, it's cached |
||
119 | * in this property to avoid redundant re-generation. |
||
120 | * |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $requestxml; |
||
124 | |||
125 | /** |
||
126 | * The Certificate Authority path. Should point to a directory containing |
||
127 | * one or more certificates to use in SSL verification. |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $capath; |
||
132 | |||
133 | /** |
||
134 | * The path to a specific Certificate Authority file. Get one and use it |
||
135 | * for full Autodiscovery compliance. |
||
136 | * |
||
137 | * @var string |
||
138 | * |
||
139 | * @link http://curl.haxx.se/ca/cacert.pem |
||
140 | * @link http://curl.haxx.se/ca/ |
||
141 | */ |
||
142 | protected $cainfo; |
||
143 | |||
144 | /** |
||
145 | * Skip SSL verification. Bad idea, and violates the strict Autodiscover |
||
146 | * protocol. But, here in case you have no other option. |
||
147 | * Defaults to FALSE. |
||
148 | * |
||
149 | * @var boolean |
||
150 | */ |
||
151 | protected $skip_ssl_verification = false; |
||
152 | |||
153 | /** |
||
154 | * The body of the last response. |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | public $last_response; |
||
159 | |||
160 | /** |
||
161 | * An associative array of response headers that resulted from the |
||
162 | * last request. Keys are lowercased for easy checking. |
||
163 | * |
||
164 | * @var array |
||
165 | */ |
||
166 | public $last_response_headers; |
||
167 | |||
168 | /** |
||
169 | * The output of curl_info() relating to the most recent cURL request. |
||
170 | * |
||
171 | * @var array |
||
172 | */ |
||
173 | public $last_info; |
||
174 | |||
175 | /** |
||
176 | * The cURL error code associated with the most recent cURL request. |
||
177 | * |
||
178 | * @var integer |
||
179 | */ |
||
180 | public $last_curl_errno; |
||
181 | |||
182 | /** |
||
183 | * Human-readable description of the most recent cURL error. |
||
184 | * |
||
185 | * @var string |
||
186 | */ |
||
187 | public $last_curl_error; |
||
188 | |||
189 | /** |
||
190 | * The value in seconds to use for Autodiscover host connection timeouts. |
||
191 | * Default connection timeout is 2 seconds, so that unresponsive methods |
||
192 | * can be bypassed quickly. |
||
193 | * |
||
194 | * @var integer |
||
195 | */ |
||
196 | public $connection_timeout = 2; |
||
197 | |||
198 | /** |
||
199 | * Information about an Autodiscover Response containing an error will |
||
200 | * be stored here. |
||
201 | * |
||
202 | * @var mixed |
||
203 | */ |
||
204 | public $error = false; |
||
205 | |||
206 | /** |
||
207 | * Information about an Autodiscover Response with a redirect will be |
||
208 | * retained here. |
||
209 | * |
||
210 | * @var mixed |
||
211 | */ |
||
212 | public $redirect = false; |
||
213 | |||
214 | /** |
||
215 | * A successful, non-error and non-redirect parsed Autodiscover response |
||
216 | * will be stored here. |
||
217 | * |
||
218 | * @var mixed |
||
219 | */ |
||
220 | public $discovered = null; |
||
221 | |||
222 | /** |
||
223 | * Constructor for the EWSAutodiscover class. |
||
224 | * |
||
225 | * @param string $email |
||
226 | * @param string $password |
||
227 | * @param string $username |
||
228 | * If left blank, the email provided will be used. |
||
229 | */ |
||
230 | public function __construct($email, $password, $username = null) |
||
242 | |||
243 | /** |
||
244 | * Execute the full discovery chain of events in the correct sequence |
||
245 | * until a valid response is received, or all methods have failed. |
||
246 | * |
||
247 | * @return integer |
||
248 | * One of the AUTODISCOVERED_VIA_* constants. |
||
249 | * |
||
250 | * @throws \RuntimeException |
||
251 | * When all autodiscovery methods fail. |
||
252 | */ |
||
253 | public function discover() |
||
275 | |||
276 | /** |
||
277 | * Return the settings discovered from the Autodiscover process. |
||
278 | * |
||
279 | * NULL indicates discovery has not completed (or been attempted) |
||
280 | * FALSE indicates discovery was not successful. Check for errors |
||
281 | * or redirects. |
||
282 | * An array will be returned with discovered settings on success. |
||
283 | * |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function discoveredSettings() |
||
290 | |||
291 | /** |
||
292 | * Toggle skipping of SSL verification in cURL requests. |
||
293 | * |
||
294 | * @param boolean $skip |
||
295 | * Whether or not to skip SSL certificate verification. |
||
296 | * @return self |
||
297 | * |
||
298 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
299 | */ |
||
300 | public function skipSSLVerification($skip = true) |
||
306 | |||
307 | /** |
||
308 | * Parse the hex ServerVersion value and return a valid |
||
309 | * Client::VERSION_* constant. |
||
310 | * |
||
311 | * @return string|boolean A known version constant, or FALSE if it could not |
||
312 | * be determined. |
||
313 | * |
||
314 | * @link http://msdn.microsoft.com/en-us/library/bb204122(v=exchg.140).aspx |
||
315 | * @link http://blogs.msdn.com/b/pcreehan/archive/2009/09/21/parsing-serverversion-when-an-int-is-really-5-ints.aspx |
||
316 | * @link http://office.microsoft.com/en-us/outlook-help/determine-the-version-of-microsoft-exchange-server-my-account-connects-to-HA001191800.aspx |
||
317 | * |
||
318 | * @param string $version_hex |
||
319 | * Hexadecimal version string. |
||
320 | */ |
||
321 | public function parseServerVersion($version_hex) |
||
348 | |||
349 | /** |
||
350 | * Method to return a new Client object, auto-configured |
||
351 | * with the proper hostname. |
||
352 | * |
||
353 | * @return mixed Client object on success, FALSE on failure. |
||
354 | */ |
||
355 | public function newEWS() |
||
402 | |||
403 | /** |
||
404 | * Static method may fail if there are issues surrounding SSL certificates. |
||
405 | * In such cases, set up the object as needed, and then call newEWS(). |
||
406 | * |
||
407 | * @param string $email |
||
408 | * @param string $password |
||
409 | * @param string $username |
||
410 | * If left blank, the email provided will be used. |
||
411 | * @return mixed |
||
412 | */ |
||
413 | public static function getEWS($email, $password, $username = null) |
||
418 | |||
419 | /** |
||
420 | * Perform an NTLM authenticated HTTPS POST to the top-level |
||
421 | * domain of the email address. |
||
422 | * |
||
423 | * @return integer|boolean |
||
424 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
425 | */ |
||
426 | public function tryTLD() |
||
431 | |||
432 | /** |
||
433 | * Perform an NTLM authenticated HTTPS POST to the 'autodiscover' |
||
434 | * subdomain of the email address' TLD. |
||
435 | * |
||
436 | * @return integer|boolean |
||
437 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
438 | */ |
||
439 | public function trySubdomain() |
||
446 | |||
447 | /** |
||
448 | * Perform an unauthenticated HTTP GET in an attempt to get redirected |
||
449 | * via 302 to the correct location to perform the HTTPS POST. |
||
450 | * |
||
451 | * @return integer|boolean |
||
452 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
453 | */ |
||
454 | public function trySubdomainUnauthenticatedGet() |
||
486 | |||
487 | /** |
||
488 | * Attempt to retrieve the autodiscover host from an SRV DNS record. |
||
489 | * |
||
490 | * @link http://support.microsoft.com/kb/940881 |
||
491 | * |
||
492 | * @return integer|boolean |
||
493 | * The value of self::AUTODISCOVERED_VIA_SRV_RECORD or false. |
||
494 | */ |
||
495 | public function trySRVRecord() |
||
509 | |||
510 | /** |
||
511 | * Set the path to the file to be used by CURLOPT_CAINFO. |
||
512 | * |
||
513 | * @param string $path |
||
514 | * Path to a certificate file such as cacert.pem |
||
515 | * @return self |
||
516 | */ |
||
517 | public function setCAInfo($path) |
||
525 | |||
526 | /** |
||
527 | * Set the path to the file to be used by CURLOPT_CAPATH. |
||
528 | * |
||
529 | * @param string $path |
||
530 | * Path to a directory containing one or more CA certificates. |
||
531 | * @return self |
||
532 | */ |
||
533 | public function setCAPath($path) |
||
541 | |||
542 | /** |
||
543 | * Set a connection timeout for the POST methods. |
||
544 | * |
||
545 | * @param integer $seconds |
||
546 | * Seconds to wait for a connection. |
||
547 | * @return self |
||
548 | */ |
||
549 | public function setConnectionTimeout($seconds) |
||
555 | |||
556 | /** |
||
557 | * Perform the NTLM authenticated post against one of the chosen |
||
558 | * endpoints. |
||
559 | * |
||
560 | * @param string $url |
||
561 | * URL to try posting to. |
||
562 | * @param integer $timeout |
||
563 | * Number of seconds before the request should timeout. |
||
564 | * @return boolean |
||
565 | */ |
||
566 | public function doNTLMPost($url, $timeout = 6) |
||
617 | |||
618 | /** |
||
619 | * Parse the Autoresponse Payload, particularly to determine if an |
||
620 | * additional request is necessary. |
||
621 | * |
||
622 | * @return boolean|array FALSE if response isn't XML or parsed response |
||
623 | * array. |
||
624 | */ |
||
625 | protected function parseAutodiscoverResponse() |
||
657 | |||
658 | /** |
||
659 | * Set the top-level domain to be used with autodiscover attempts based |
||
660 | * on the provided email address. |
||
661 | * |
||
662 | * @return boolean |
||
663 | */ |
||
664 | protected function setTLD() |
||
674 | |||
675 | /** |
||
676 | * Reset the response-related structures. Called before making a new |
||
677 | * request. |
||
678 | * |
||
679 | * @return self |
||
680 | */ |
||
681 | public function reset() |
||
690 | |||
691 | /** |
||
692 | * Return the generated Autodiscover XML request body. |
||
693 | * |
||
694 | * @return string |
||
695 | * |
||
696 | * @suppress PhanTypeMismatchArgumentInternal |
||
697 | */ |
||
698 | public function getAutodiscoverRequest() |
||
726 | |||
727 | /** |
||
728 | * Utility function to pick headers off of the incoming cURL response. |
||
729 | * Used with CURLOPT_HEADERFUNCTION. |
||
730 | * |
||
731 | * @param resource $_ch |
||
732 | * cURL handle. |
||
733 | * @param string $str |
||
734 | * Header string to read. |
||
735 | * @return integer |
||
736 | * Bytes read. |
||
737 | * |
||
738 | * @todo Determine if we can remove $_ch here. |
||
739 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
740 | */ |
||
741 | public function readHeaders($_ch, $str) |
||
752 | |||
753 | /** |
||
754 | * Utility function to parse XML payloads from the response into easier |
||
755 | * to manage associative arrays. |
||
756 | * |
||
757 | * @param string $xml |
||
758 | * XML to parse. |
||
759 | * @return array |
||
760 | */ |
||
761 | public function responseToArray($xml) |
||
769 | |||
770 | /** |
||
771 | * Recursive method for parsing DOM nodes. |
||
772 | * |
||
773 | * @param \DOMElement $node |
||
774 | * DOMNode object. |
||
775 | * @return mixed |
||
776 | * |
||
777 | * @link https://github.com/gaarf/XML-string-to-PHP-array |
||
778 | * |
||
779 | * @suppress PhanTypeMismatchArgument, PhanUndeclaredProperty |
||
780 | */ |
||
781 | protected function nodeToArray($node) |
||
830 | |||
831 | /** |
||
832 | * Parses the version of an Exchange 2007 server. |
||
833 | * |
||
834 | * @param integer $minorversion |
||
835 | * Minor server version. |
||
836 | * @return string Server version. |
||
837 | */ |
||
838 | View Code Duplication | protected function parseVersion2007($minorversion) |
|
851 | |||
852 | /** |
||
853 | * Parses the version of an Exchange 2010 server. |
||
854 | * |
||
855 | * @param integer $minorversion |
||
856 | * Minor server version. |
||
857 | * @return string Server version. |
||
858 | */ |
||
859 | View Code Duplication | protected function parseVersion2010($minorversion) |
|
872 | |||
873 | /** |
||
874 | * Parses the version of an Exchange 2013 server. |
||
875 | * |
||
876 | * @param integer $majorbuild |
||
877 | * Major build version. |
||
878 | * @return string Server version. |
||
879 | */ |
||
880 | protected function parseVersion2013($majorbuild) |
||
886 | |||
887 | /** |
||
888 | * Parses the version of an Exchange 2016 server. |
||
889 | * |
||
890 | * @return string Server version. |
||
891 | */ |
||
892 | protected function parseVersion2016() |
||
896 | |||
897 | /** |
||
898 | * Attempts an autodiscover via a URL. |
||
899 | * |
||
900 | * @param string $url |
||
901 | * Url to attempt an autodiscover. |
||
902 | * @param integer $timeout |
||
903 | * Number of seconds before the request should timeout. |
||
904 | * @return boolean |
||
905 | */ |
||
906 | protected function tryViaUrl($url, $timeout = 6) |
||
911 | } |
||
912 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..