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 |
||
35 | class Autodiscover |
||
36 | { |
||
37 | /** |
||
38 | * The path appended to the various schemes and hostnames used during |
||
39 | * autodiscovery. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | const AUTODISCOVER_PATH = '/autodiscover/autodiscover.xml'; |
||
44 | |||
45 | /** |
||
46 | * Server was discovered using the TLD method. |
||
47 | * |
||
48 | * @var integer |
||
49 | */ |
||
50 | const AUTODISCOVERED_VIA_TLD = 10; |
||
51 | |||
52 | /** |
||
53 | * Server was discovered using the subdomain method. |
||
54 | * |
||
55 | * @var integer |
||
56 | */ |
||
57 | const AUTODISCOVERED_VIA_SUBDOMAIN = 11; |
||
58 | |||
59 | /** |
||
60 | * Server was discovered using the unauthenticated GET method. |
||
61 | * |
||
62 | * @var integer |
||
63 | */ |
||
64 | const AUTODISCOVERED_VIA_UNAUTHENTICATED_GET = 12; |
||
65 | |||
66 | /** |
||
67 | * Server was discovered using the DNS SRV redirect method. |
||
68 | * |
||
69 | * @var integer |
||
70 | */ |
||
71 | const AUTODISCOVERED_VIA_SRV_RECORD = 13; |
||
72 | |||
73 | /** |
||
74 | * Server was discovered using the HTTP redirect method. |
||
75 | * |
||
76 | * @var integer |
||
77 | * |
||
78 | * @todo We do not currently support this. |
||
79 | */ |
||
80 | const AUTODISCOVERED_VIA_RESPONSE_REDIRECT = 14; |
||
81 | |||
82 | /** |
||
83 | * The email address to attempt autodiscovery against. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $email; |
||
88 | |||
89 | /** |
||
90 | * The password to present during autodiscovery. |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $password; |
||
95 | |||
96 | /** |
||
97 | * The Exchange username to use during authentication. If unspecified, |
||
98 | * the provided email address will be used as the username. |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $username; |
||
103 | |||
104 | /** |
||
105 | * The top-level domain name, extracted from the provided email address. |
||
106 | * |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $tld; |
||
110 | |||
111 | /** |
||
112 | * The Autodiscover XML request. Since it's used repeatedly, it's cached |
||
113 | * in this property to avoid redundant re-generation. |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $requestxml; |
||
118 | |||
119 | /** |
||
120 | * The Certificate Authority path. Should point to a directory containing |
||
121 | * one or more certificates to use in SSL verification. |
||
122 | * |
||
123 | * @var string |
||
124 | */ |
||
125 | protected $capath; |
||
126 | |||
127 | /** |
||
128 | * The path to a specific Certificate Authority file. Get one and use it |
||
129 | * for full Autodiscovery compliance. |
||
130 | * |
||
131 | * @var string |
||
132 | * |
||
133 | * @link http://curl.haxx.se/ca/cacert.pem |
||
134 | * @link http://curl.haxx.se/ca/ |
||
135 | */ |
||
136 | protected $cainfo; |
||
137 | |||
138 | /** |
||
139 | * Skip SSL verification. Bad idea, and violates the strict Autodiscover |
||
140 | * protocol. But, here in case you have no other option. |
||
141 | * Defaults to FALSE. |
||
142 | * |
||
143 | * @var boolean |
||
144 | */ |
||
145 | protected $skip_ssl_verification = false; |
||
146 | |||
147 | /** |
||
148 | * The body of the last response. |
||
149 | * |
||
150 | * @var string |
||
151 | */ |
||
152 | public $last_response; |
||
153 | |||
154 | /** |
||
155 | * An associative array of response headers that resulted from the |
||
156 | * last request. Keys are lowercased for easy checking. |
||
157 | * |
||
158 | * @var array |
||
159 | */ |
||
160 | public $last_response_headers; |
||
161 | |||
162 | /** |
||
163 | * The output of curl_info() relating to the most recent cURL request. |
||
164 | * |
||
165 | * @var array |
||
166 | */ |
||
167 | public $last_info; |
||
168 | |||
169 | /** |
||
170 | * The cURL error code associated with the most recent cURL request. |
||
171 | * |
||
172 | * @var integer |
||
173 | */ |
||
174 | public $last_curl_errno; |
||
175 | |||
176 | /** |
||
177 | * Human-readable description of the most recent cURL error. |
||
178 | * |
||
179 | * @var string |
||
180 | */ |
||
181 | public $last_curl_error; |
||
182 | |||
183 | /** |
||
184 | * The value in seconds to use for Autodiscover host connection timeouts. |
||
185 | * Default connection timeout is 2 seconds, so that unresponsive methods |
||
186 | * can be bypassed quickly. |
||
187 | * |
||
188 | * @var integer |
||
189 | */ |
||
190 | public $connection_timeout = 2; |
||
191 | |||
192 | /** |
||
193 | * Information about an Autodiscover Response containing an error will |
||
194 | * be stored here. |
||
195 | * |
||
196 | * @var mixed |
||
197 | */ |
||
198 | public $error = false; |
||
199 | |||
200 | /** |
||
201 | * Information about an Autodiscover Response with a redirect will be |
||
202 | * retained here. |
||
203 | * |
||
204 | * @var mixed |
||
205 | */ |
||
206 | public $redirect = false; |
||
207 | |||
208 | /** |
||
209 | * A successful, non-error and non-redirect parsed Autodiscover response |
||
210 | * will be stored here. |
||
211 | * |
||
212 | * @var mixed |
||
213 | */ |
||
214 | public $discovered = null; |
||
215 | |||
216 | /** |
||
217 | * Constructor for the EWSAutodiscover class. |
||
218 | * |
||
219 | * @param string $email |
||
220 | * @param string $password |
||
221 | * @param string $username |
||
222 | * If left blank, the email provided will be used. |
||
223 | */ |
||
224 | public function __construct($email, $password, $username = null) |
||
236 | |||
237 | /** |
||
238 | * Execute the full discovery chain of events in the correct sequence |
||
239 | * until a valid response is received, or all methods have failed. |
||
240 | * |
||
241 | * @return integer |
||
242 | * One of the AUTODISCOVERED_VIA_* constants. |
||
243 | * |
||
244 | * @throws \RuntimeException |
||
245 | * When all autodiscovery methods fail. |
||
246 | */ |
||
247 | public function discover() |
||
269 | |||
270 | /** |
||
271 | * Return the settings discovered from the Autodiscover process. |
||
272 | * |
||
273 | * NULL indicates discovery has not completed (or been attempted) |
||
274 | * FALSE indicates discovery was not successful. Check for errors |
||
275 | * or redirects. |
||
276 | * An array will be returned with discovered settings on success. |
||
277 | * |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public function discoveredSettings() |
||
284 | |||
285 | /** |
||
286 | * Toggle skipping of SSL verification in cURL requests. |
||
287 | * |
||
288 | * @param boolean $skip |
||
289 | * Whether or not to skip SSL certificate verification. |
||
290 | * @return self |
||
291 | */ |
||
292 | public function skipSSLVerification($skip = true) |
||
298 | |||
299 | /** |
||
300 | * Parse the hex ServerVersion value and return a valid |
||
301 | * Client::VERSION_* constant. |
||
302 | * |
||
303 | * @return string|boolean A known version constant, or FALSE if it could not |
||
304 | * be determined. |
||
305 | * |
||
306 | * @link http://msdn.microsoft.com/en-us/library/bb204122(v=exchg.140).aspx |
||
307 | * @link http://blogs.msdn.com/b/pcreehan/archive/2009/09/21/parsing-serverversion-when-an-int-is-really-5-ints.aspx |
||
308 | * @link http://office.microsoft.com/en-us/outlook-help/determine-the-version-of-microsoft-exchange-server-my-account-connects-to-HA001191800.aspx |
||
309 | * |
||
310 | * @param string $version_hex |
||
311 | * Hexadecimal version string. |
||
312 | */ |
||
313 | public function parseServerVersion($version_hex) |
||
340 | |||
341 | /** |
||
342 | * Method to return a new Client object, auto-configured |
||
343 | * with the proper hostname. |
||
344 | * |
||
345 | * @return mixed Client object on success, FALSE on failure. |
||
346 | */ |
||
347 | public function newEWS() |
||
396 | |||
397 | /** |
||
398 | * Static method may fail if there are issues surrounding SSL certificates. |
||
399 | * In such cases, set up the object as needed, and then call newEWS(). |
||
400 | * |
||
401 | * @param string $email |
||
402 | * @param string $password |
||
403 | * @param string $username |
||
404 | * If left blank, the email provided will be used. |
||
405 | * @return mixed |
||
406 | */ |
||
407 | public static function getEWS($email, $password, $username = null) |
||
412 | |||
413 | /** |
||
414 | * Perform an NTLM authenticated HTTPS POST to the top-level |
||
415 | * domain of the email address. |
||
416 | * |
||
417 | * @return integer|boolean |
||
418 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
419 | */ |
||
420 | public function tryTLD() |
||
425 | |||
426 | /** |
||
427 | * Perform an NTLM authenticated HTTPS POST to the 'autodiscover' |
||
428 | * subdomain of the email address' TLD. |
||
429 | * |
||
430 | * @return integer|boolean |
||
431 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
432 | */ |
||
433 | public function trySubdomain() |
||
440 | |||
441 | /** |
||
442 | * Perform an unauthenticated HTTP GET in an attempt to get redirected |
||
443 | * via 302 to the correct location to perform the HTTPS POST. |
||
444 | * |
||
445 | * @return integer|boolean |
||
446 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
447 | */ |
||
448 | public function trySubdomainUnauthenticatedGet() |
||
482 | |||
483 | /** |
||
484 | * Attempt to retrieve the autodiscover host from an SRV DNS record. |
||
485 | * |
||
486 | * @link http://support.microsoft.com/kb/940881 |
||
487 | * |
||
488 | * @return integer|boolean |
||
489 | * The value of self::AUTODISCOVERED_VIA_SRV_RECORD or false. |
||
490 | */ |
||
491 | public function trySRVRecord() |
||
505 | |||
506 | /** |
||
507 | * Set the path to the file to be used by CURLOPT_CAINFO. |
||
508 | * |
||
509 | * @param string $path |
||
510 | * Path to a certificate file such as cacert.pem |
||
511 | * @return self |
||
512 | */ |
||
513 | public function setCAInfo($path) |
||
521 | |||
522 | /** |
||
523 | * Set the path to the file to be used by CURLOPT_CAPATH. |
||
524 | * |
||
525 | * @param string $path |
||
526 | * Path to a directory containing one or more CA certificates. |
||
527 | * @return self |
||
528 | */ |
||
529 | public function setCAPath($path) |
||
537 | |||
538 | /** |
||
539 | * Set a connection timeout for the POST methods. |
||
540 | * |
||
541 | * @param integer $seconds |
||
542 | * Seconds to wait for a connection. |
||
543 | * @return self |
||
544 | */ |
||
545 | public function setConnectionTimeout($seconds) |
||
551 | |||
552 | /** |
||
553 | * Perform the NTLM authenticated post against one of the chosen |
||
554 | * endpoints. |
||
555 | * |
||
556 | * @param string $url |
||
557 | * URL to try posting to. |
||
558 | * @param integer $timeout |
||
559 | * Number of seconds before the request should timeout. |
||
560 | * @return boolean |
||
561 | */ |
||
562 | public function doNTLMPost($url, $timeout = 6) |
||
613 | |||
614 | /** |
||
615 | * Parse the Autoresponse Payload, particularly to determine if an |
||
616 | * additional request is necessary. |
||
617 | * |
||
618 | * @return boolean|array FALSE if response isn't XML or parsed response |
||
619 | * array. |
||
620 | */ |
||
621 | protected function parseAutodiscoverResponse() |
||
653 | |||
654 | /** |
||
655 | * Set the top-level domain to be used with autodiscover attempts based |
||
656 | * on the provided email address. |
||
657 | * |
||
658 | * @return boolean |
||
659 | */ |
||
660 | protected function setTLD() |
||
670 | |||
671 | /** |
||
672 | * Reset the response-related structures. Called before making a new |
||
673 | * request. |
||
674 | * |
||
675 | * @return self |
||
676 | */ |
||
677 | public function reset() |
||
686 | |||
687 | /** |
||
688 | * Return the generated Autodiscover XML request body. |
||
689 | * |
||
690 | * @return string |
||
691 | */ |
||
692 | public function getAutodiscoverRequest() |
||
720 | |||
721 | /** |
||
722 | * Utility function to pick headers off of the incoming cURL response. |
||
723 | * Used with CURLOPT_HEADERFUNCTION. |
||
724 | * |
||
725 | * @param resource $_ch |
||
726 | * cURL handle. |
||
727 | * @param string $str |
||
728 | * Header string to read. |
||
729 | * @return integer |
||
730 | * Bytes read. |
||
731 | */ |
||
732 | public function readHeaders($_ch, $str) |
||
743 | |||
744 | /** |
||
745 | * Utility function to parse XML payloads from the response into easier |
||
746 | * to manage associative arrays. |
||
747 | * |
||
748 | * @param string $xml |
||
749 | * XML to parse. |
||
750 | * @return array |
||
751 | */ |
||
752 | public function responseToArray($xml) |
||
760 | |||
761 | /** |
||
762 | * Recursive method for parsing DOM nodes. |
||
763 | * |
||
764 | * @param \DOMElement $node |
||
765 | * DOMNode object. |
||
766 | * @return mixed |
||
767 | * |
||
768 | * @link https://github.com/gaarf/XML-string-to-PHP-array |
||
769 | */ |
||
770 | protected function nodeToArray($node) |
||
819 | |||
820 | /** |
||
821 | * Parses the version of an Exchange 2007 server. |
||
822 | * |
||
823 | * @param integer $minorversion |
||
824 | * Minor server version. |
||
825 | * @return string Server version. |
||
826 | */ |
||
827 | protected function parseVersion2007($minorversion) { |
||
839 | |||
840 | /** |
||
841 | * Parses the version of an Exchange 2010 server. |
||
842 | * |
||
843 | * @param integer $minorversion |
||
844 | * Minor server version. |
||
845 | * @return string Server version. |
||
846 | */ |
||
847 | protected function parseVersion2010($minorversion) { |
||
859 | |||
860 | /** |
||
861 | * Parses the version of an Exchange 2013 server. |
||
862 | * |
||
863 | * @param integer $majorbuild |
||
864 | * Major build version. |
||
865 | * @return string Server version. |
||
866 | */ |
||
867 | protected function parseVersion2013($majorbuild) { |
||
872 | |||
873 | /** |
||
874 | * Parses the version of an Exchange 2016 server. |
||
875 | * |
||
876 | * @return string Server version. |
||
877 | */ |
||
878 | protected function parseVersion2016() { |
||
881 | |||
882 | /** |
||
883 | * Attempts an autodiscover via a URL. |
||
884 | * |
||
885 | * @param string $url |
||
886 | * Url to attempt an autodiscover. |
||
887 | * @param integer $timeout |
||
888 | * Number of seconds before the request should timeout. |
||
889 | * @return boolean |
||
890 | */ |
||
891 | protected function tryViaUrl($url, $timeout = 6) |
||
896 | } |
||
897 |
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..