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() |
||
285 | |||
286 | /** |
||
287 | * Return the settings discovered from the Autodiscover process. |
||
288 | * |
||
289 | * NULL indicates discovery has not completed (or been attempted) |
||
290 | * FALSE indicates discovery was not successful. Check for errors |
||
291 | * or redirects. |
||
292 | * An array will be returned with discovered settings on success. |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public function discoveredSettings() |
||
300 | |||
301 | /** |
||
302 | * Toggle skipping of SSL verification in cURL requests. |
||
303 | * |
||
304 | * @param boolean $skip |
||
305 | * Whether or not to skip SSL certificate verification. |
||
306 | * @return self |
||
307 | * |
||
308 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
309 | */ |
||
310 | public function skipSSLVerification($skip = true) |
||
316 | |||
317 | /** |
||
318 | * Parse the hex ServerVersion value and return a valid |
||
319 | * Client::VERSION_* constant. |
||
320 | * |
||
321 | * @return string|boolean A known version constant, or FALSE if it could not |
||
322 | * be determined. |
||
323 | * |
||
324 | * @link http://msdn.microsoft.com/en-us/library/bb204122(v=exchg.140).aspx |
||
325 | * @link http://blogs.msdn.com/b/pcreehan/archive/2009/09/21/parsing-serverversion-when-an-int-is-really-5-ints.aspx |
||
326 | * @link http://office.microsoft.com/en-us/outlook-help/determine-the-version-of-microsoft-exchange-server-my-account-connects-to-HA001191800.aspx |
||
327 | * |
||
328 | * @param string $version_hex |
||
329 | * Hexadecimal version string. |
||
330 | */ |
||
331 | public function parseServerVersion($version_hex) |
||
358 | |||
359 | /** |
||
360 | * Method to return a new Client object, auto-configured |
||
361 | * with the proper hostname. |
||
362 | * |
||
363 | * @return mixed Client object on success, FALSE on failure. |
||
364 | */ |
||
365 | public function newEWS() |
||
412 | |||
413 | /** |
||
414 | * Static method may fail if there are issues surrounding SSL certificates. |
||
415 | * In such cases, set up the object as needed, and then call newEWS(). |
||
416 | * |
||
417 | * @param string $email |
||
418 | * @param string $password |
||
419 | * @param string $username |
||
420 | * If left blank, the email provided will be used. |
||
421 | * @return mixed |
||
422 | */ |
||
423 | public static function getEWS($email, $password, $username = null) |
||
428 | |||
429 | /** |
||
430 | * Perform an NTLM authenticated HTTPS POST to the top-level |
||
431 | * domain of the email address. |
||
432 | * |
||
433 | * @return integer|boolean |
||
434 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
435 | */ |
||
436 | public function tryTLD() |
||
441 | |||
442 | /** |
||
443 | * Perform an NTLM authenticated HTTPS POST to the 'autodiscover' |
||
444 | * subdomain of the email address' TLD. |
||
445 | * |
||
446 | * @return integer|boolean |
||
447 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
448 | */ |
||
449 | public function trySubdomain() |
||
456 | |||
457 | /** |
||
458 | * Perform an unauthenticated HTTP GET in an attempt to get redirected |
||
459 | * via 302 to the correct location to perform the HTTPS POST. |
||
460 | * |
||
461 | * @return integer|boolean |
||
462 | * One of the AUTODISCOVERED_VIA_* constants or false on failure. |
||
463 | */ |
||
464 | public function trySubdomainUnauthenticatedGet() |
||
496 | |||
497 | /** |
||
498 | * Attempt to retrieve the autodiscover host from an SRV DNS record. |
||
499 | * |
||
500 | * @link http://support.microsoft.com/kb/940881 |
||
501 | * |
||
502 | * @return integer|boolean |
||
503 | * The value of self::AUTODISCOVERED_VIA_SRV_RECORD or false. |
||
504 | */ |
||
505 | public function trySRVRecord() |
||
519 | |||
520 | /** |
||
521 | * Set the path to the file to be used by CURLOPT_CAINFO. |
||
522 | * |
||
523 | * @param string $path |
||
524 | * Path to a certificate file such as cacert.pem |
||
525 | * @return self |
||
526 | */ |
||
527 | public function setCAInfo($path) |
||
535 | |||
536 | /** |
||
537 | * Set the path to the file to be used by CURLOPT_CAPATH. |
||
538 | * |
||
539 | * @param string $path |
||
540 | * Path to a directory containing one or more CA certificates. |
||
541 | * @return self |
||
542 | */ |
||
543 | public function setCAPath($path) |
||
551 | |||
552 | /** |
||
553 | * Set a connection timeout for the POST methods. |
||
554 | * |
||
555 | * @param integer $seconds |
||
556 | * Seconds to wait for a connection. |
||
557 | * @return self |
||
558 | */ |
||
559 | public function setConnectionTimeout($seconds) |
||
565 | |||
566 | /** |
||
567 | * Perform the NTLM authenticated post against one of the chosen |
||
568 | * endpoints. |
||
569 | * |
||
570 | * @param string $url |
||
571 | * URL to try posting to. |
||
572 | * @param integer $timeout |
||
573 | * Number of seconds before the request should timeout. |
||
574 | * @return boolean |
||
575 | */ |
||
576 | public function doNTLMPost($url, $timeout = 6) |
||
627 | |||
628 | /** |
||
629 | * Parse the Autoresponse Payload, particularly to determine if an |
||
630 | * additional request is necessary. |
||
631 | * |
||
632 | * @return boolean|array FALSE if response isn't XML or parsed response |
||
633 | * array. |
||
634 | */ |
||
635 | protected function parseAutodiscoverResponse() |
||
667 | |||
668 | /** |
||
669 | * Set the top-level domain to be used with autodiscover attempts based |
||
670 | * on the provided email address. |
||
671 | * |
||
672 | * @return boolean |
||
673 | */ |
||
674 | protected function setTLD() |
||
684 | |||
685 | /** |
||
686 | * Reset the response-related structures. Called before making a new |
||
687 | * request. |
||
688 | * |
||
689 | * @return self |
||
690 | */ |
||
691 | public function reset() |
||
700 | |||
701 | /** |
||
702 | * Return the generated Autodiscover XML request body. |
||
703 | * |
||
704 | * @return string |
||
705 | * |
||
706 | * @suppress PhanTypeMismatchArgumentInternal |
||
707 | */ |
||
708 | public function getAutodiscoverRequest() |
||
736 | |||
737 | /** |
||
738 | * Utility function to pick headers off of the incoming cURL response. |
||
739 | * Used with CURLOPT_HEADERFUNCTION. |
||
740 | * |
||
741 | * @param resource $_ch |
||
742 | * cURL handle. |
||
743 | * @param string $str |
||
744 | * Header string to read. |
||
745 | * @return integer |
||
746 | * Bytes read. |
||
747 | * |
||
748 | * @todo Determine if we can remove $_ch here. |
||
749 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
750 | */ |
||
751 | public function readHeaders($_ch, $str) |
||
762 | |||
763 | /** |
||
764 | * Utility function to parse XML payloads from the response into easier |
||
765 | * to manage associative arrays. |
||
766 | * |
||
767 | * @param string $xml |
||
768 | * XML to parse. |
||
769 | * @return array |
||
770 | */ |
||
771 | public function responseToArray($xml) |
||
779 | |||
780 | /** |
||
781 | * Recursive method for parsing DOM nodes. |
||
782 | * |
||
783 | * @param \DOMElement $node |
||
784 | * DOMNode object. |
||
785 | * @return mixed |
||
786 | * |
||
787 | * @link https://github.com/gaarf/XML-string-to-PHP-array |
||
788 | * |
||
789 | * @suppress PhanTypeMismatchArgument, PhanUndeclaredProperty |
||
790 | */ |
||
791 | protected function nodeToArray($node) |
||
840 | |||
841 | /** |
||
842 | * Parses the version of an Exchange 2007 server. |
||
843 | * |
||
844 | * @param integer $minorversion |
||
845 | * Minor server version. |
||
846 | * @return string Server version. |
||
847 | */ |
||
848 | View Code Duplication | protected function parseVersion2007($minorversion) |
|
861 | |||
862 | /** |
||
863 | * Parses the version of an Exchange 2010 server. |
||
864 | * |
||
865 | * @param integer $minorversion |
||
866 | * Minor server version. |
||
867 | * @return string Server version. |
||
868 | */ |
||
869 | View Code Duplication | protected function parseVersion2010($minorversion) |
|
882 | |||
883 | /** |
||
884 | * Parses the version of an Exchange 2013 server. |
||
885 | * |
||
886 | * @param integer $majorbuild |
||
887 | * Major build version. |
||
888 | * @return string Server version. |
||
889 | */ |
||
890 | protected function parseVersion2013($majorbuild) |
||
896 | |||
897 | /** |
||
898 | * Parses the version of an Exchange 2016 server. |
||
899 | * |
||
900 | * @return string Server version. |
||
901 | */ |
||
902 | protected function parseVersion2016() |
||
906 | |||
907 | /** |
||
908 | * Attempts an autodiscover via a URL. |
||
909 | * |
||
910 | * @param string $url |
||
911 | * Url to attempt an autodiscover. |
||
912 | * @param integer $timeout |
||
913 | * Number of seconds before the request should timeout. |
||
914 | * @return boolean |
||
915 | */ |
||
916 | protected function tryViaUrl($url, $timeout = 6) |
||
921 | } |
||
922 |
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..