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 Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Client |
||
| 11 | { |
||
| 12 | /// @todo: do these need to be public? |
||
| 13 | public $method = 'http'; |
||
| 14 | public $server; |
||
| 15 | public $port = 0; |
||
| 16 | public $path; |
||
| 17 | |||
| 18 | public $errno; |
||
| 19 | public $errstr; |
||
| 20 | public $debug = 0; |
||
| 21 | |||
| 22 | public $username = ''; |
||
| 23 | public $password = ''; |
||
| 24 | public $authtype = 1; |
||
| 25 | |||
| 26 | public $cert = ''; |
||
| 27 | public $certpass = ''; |
||
| 28 | public $cacert = ''; |
||
| 29 | public $cacertdir = ''; |
||
| 30 | public $key = ''; |
||
| 31 | public $keypass = ''; |
||
| 32 | public $verifypeer = true; |
||
| 33 | public $verifyhost = 2; |
||
| 34 | public $sslversion = 0; // corresponds to CURL_SSLVERSION_DEFAULT |
||
| 35 | |||
| 36 | public $proxy = ''; |
||
| 37 | public $proxyport = 0; |
||
| 38 | public $proxy_user = ''; |
||
| 39 | public $proxy_pass = ''; |
||
| 40 | public $proxy_authtype = 1; |
||
| 41 | |||
| 42 | public $cookies = array(); |
||
| 43 | public $extracurlopts = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | * |
||
| 48 | * This determines whether the multicall() method will try to take advantage of the system.multicall xmlrpc method |
||
| 49 | * to dispatch to the server an array of requests in a single http roundtrip or simply execute many consecutive http |
||
| 50 | * calls. Defaults to FALSE, but it will be enabled automatically on the first failure of execution of |
||
| 51 | * system.multicall. |
||
| 52 | */ |
||
| 53 | public $no_multicall = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * List of http compression methods accepted by the client for responses. |
||
| 57 | * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib. |
||
| 58 | * |
||
| 59 | * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since |
||
| 60 | * in those cases it will be up to CURL to decide the compression methods |
||
| 61 | * it supports. You might check for the presence of 'zlib' in the output of |
||
| 62 | * curl_version() to determine wheter compression is supported or not |
||
| 63 | */ |
||
| 64 | public $accepted_compression = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Name of compression scheme to be used for sending requests. |
||
| 68 | * Either null, gzip or deflate. |
||
| 69 | */ |
||
| 70 | |||
| 71 | public $request_compression = ''; |
||
| 72 | /** |
||
| 73 | * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see: |
||
| 74 | * http://curl.haxx.se/docs/faq.html#7.3). |
||
| 75 | */ |
||
| 76 | public $xmlrpc_curl_handle = null; |
||
| 77 | |||
| 78 | /// Whether to use persistent connections for http 1.1 and https |
||
| 79 | public $keepalive = false; |
||
| 80 | |||
| 81 | /// Charset encodings that can be decoded without problems by the client |
||
| 82 | public $accepted_charset_encodings = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The charset encoding that will be used for serializing request sent by the client. |
||
| 86 | * If defaults to NULL, which means using US-ASCII and encoding all characters outside of the ASCII range using |
||
| 87 | * their xml character entity representation (this has the benefit that line end characters will not be mangled in |
||
| 88 | * the transfer, a CR-LF will be preserved as well as a singe LF). |
||
| 89 | * Valid values are 'US-ASCII', 'UTF-8' and 'ISO-8859-1' |
||
| 90 | */ |
||
| 91 | public $request_charset_encoding = ''; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Decides the content of Response objects returned by calls to send() and multicall(). |
||
| 95 | * Valid values are 'xmlrpcvals', 'phpvals' or 'xml'. |
||
| 96 | * |
||
| 97 | * Determines whether the value returned inside an Response object as results of calls to the send() and multicall() |
||
| 98 | * methods will be a Value object, a plain php value or a raw xml string. |
||
| 99 | * Allowed values are 'xmlrpcvals' (the default), 'phpvals' and 'xml'. |
||
| 100 | * To allow the user to differentiate between a correct and a faulty response, fault responses will be returned as |
||
| 101 | * Response objects in any case. |
||
| 102 | * Note that the 'phpvals' setting will yield faster execution times, but some of the information from the original |
||
| 103 | * response will be lost. It will be e.g. impossible to tell whether a particular php string value was sent by the |
||
| 104 | * server as an xmlrpc string or base64 value. |
||
| 105 | */ |
||
| 106 | public $return_type = 'xmlrpcvals'; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Sent to servers in http headers. |
||
| 110 | */ |
||
| 111 | public $user_agent; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $path either the PATH part of the xmlrpc server URL, or complete server URL (in which case you |
||
| 115 | * should use and empty string for all other parameters) |
||
| 116 | * e.g. /xmlrpc/server.php |
||
| 117 | * e.g. http://phpxmlrpc.sourceforge.net/server.php |
||
| 118 | * e.g. https://james:[email protected]:443/xmlrpcserver?agent=007 |
||
| 119 | * @param string $server the server name / ip address |
||
| 120 | * @param integer $port the port the server is listening on, when omitted defaults to 80 or 443 depending on |
||
| 121 | * protocol used |
||
| 122 | * @param string $method the http protocol variant: defaults to 'http'; 'https' and 'http11' can be used if CURL is |
||
| 123 | * installed. The value set here can be overridden in any call to $this->send(). |
||
| 124 | */ |
||
| 125 | public function __construct($path, $server = '', $port = '', $method = '') |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Enable/disable the echoing to screen of the xmlrpc responses received. The default is not no output anything. |
||
| 195 | * |
||
| 196 | * The debugging information at level 1 includes the raw data returned from the XML-RPC server it was querying |
||
| 197 | * (including bot HTTP headers and the full XML payload), and the PHP value the client attempts to create to |
||
| 198 | * represent the value returned by the server |
||
| 199 | * At level2, the complete payload of the xmlrpc request is also printed, before being sent t the server. |
||
| 200 | * |
||
| 201 | * This option can be very useful when debugging servers as it allows you to see exactly what the client sends and |
||
| 202 | * the server returns. |
||
| 203 | * |
||
| 204 | * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) |
||
| 205 | */ |
||
| 206 | public function setDebug($level) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets the username and password for authorizing the client to the server. |
||
| 213 | * |
||
| 214 | * With the default (HTTP) transport, this information is used for HTTP Basic authorization. |
||
| 215 | * Note that username and password can also be set using the class constructor. |
||
| 216 | * With HTTP 1.1 and HTTPS transport, NTLM and Digest authentication protocols are also supported. To enable them use |
||
| 217 | * the constants CURLAUTH_DIGEST and CURLAUTH_NTLM as values for the auth type parameter. |
||
| 218 | * |
||
| 219 | * @param string $user username |
||
| 220 | * @param string $password password |
||
| 221 | * @param integer $authType auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC |
||
| 222 | * (basic auth). Note that auth types NTLM and Digest will only work if the Curl php |
||
| 223 | * extension is enabled. |
||
| 224 | */ |
||
| 225 | public function setCredentials($user, $password, $authType = 1) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set the optional certificate and passphrase used in SSL-enabled communication with a remote server. |
||
| 234 | * |
||
| 235 | * Note: to retrieve information about the client certificate on the server side, you will need to look into the |
||
| 236 | * environment variables which are set up by the webserver. Different webservers will typically set up different |
||
| 237 | * variables. |
||
| 238 | * |
||
| 239 | * @param string $cert the name of a file containing a PEM formatted certificate |
||
| 240 | * @param string $certPass the password required to use it |
||
| 241 | */ |
||
| 242 | public function setCertificate($cert, $certPass = '') |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add a CA certificate to verify server with in SSL-enabled communication when SetSSLVerifypeer has been set to TRUE. |
||
| 250 | * |
||
| 251 | * See the php manual page about CURLOPT_CAINFO for more details. |
||
| 252 | * |
||
| 253 | * @param string $caCert certificate file name (or dir holding certificates) |
||
| 254 | * @param bool $isDir set to true to indicate cacert is a dir. defaults to false |
||
| 255 | */ |
||
| 256 | public function setCaCertificate($caCert, $isDir = false) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Set attributes for SSL communication: private SSL key. |
||
| 267 | * |
||
| 268 | * NB: does not work in older php/curl installs. |
||
| 269 | * Thanks to Daniel Convissor. |
||
| 270 | * |
||
| 271 | * @param string $key The name of a file containing a private SSL key |
||
| 272 | * @param string $keyPass The secret password needed to use the private SSL key |
||
| 273 | */ |
||
| 274 | public function setKey($key, $keyPass) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Set attributes for SSL communication: verify the remote host's SSL certificate, and cause the connection to fail |
||
| 282 | * if the cert verification fails. |
||
| 283 | * |
||
| 284 | * By default, verification is enabled. |
||
| 285 | * To specify custom SSL certificates to validate the server with, use the setCaCertificate method. |
||
| 286 | * |
||
| 287 | * @param bool $i enable/disable verification of peer certificate |
||
| 288 | */ |
||
| 289 | public function setSSLVerifyPeer($i) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Set attributes for SSL communication: verify the remote host's SSL certificate's common name (CN). |
||
| 296 | * |
||
| 297 | * Note that support for value 1 has been removed in cURL 7.28.1 |
||
| 298 | * |
||
| 299 | * @param int $i Set to 1 to only the existence of a CN, not that it matches |
||
| 300 | */ |
||
| 301 | public function setSSLVerifyHost($i) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value ): let cURL decide |
||
| 308 | * |
||
| 309 | * @param int $i |
||
| 310 | */ |
||
| 311 | public function setSSLVersion($i) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set proxy info. |
||
| 318 | * |
||
| 319 | * NB: CURL versions before 7.11.10 cannot use a proxy to communicate with https servers. |
||
| 320 | * |
||
| 321 | * @param string $proxyHost |
||
| 322 | * @param string $proxyPort Defaults to 8080 for HTTP and 443 for HTTPS |
||
| 323 | * @param string $proxyUsername Leave blank if proxy has public access |
||
| 324 | * @param string $proxyPassword Leave blank if proxy has public access |
||
| 325 | * @param int $proxyAuthType defaults to CURLAUTH_BASIC (Basic authentication protocol); set to constant CURLAUTH_NTLM |
||
| 326 | * to use NTLM auth with proxy (has effect only when the client uses the HTTP 1.1 protocol) |
||
| 327 | */ |
||
| 328 | public function setProxy($proxyHost, $proxyPort, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Enables/disables reception of compressed xmlrpc responses. |
||
| 339 | * |
||
| 340 | * This requires the "zlib" extension to be enabled in your php install. If it is, by default xmlrpc_client |
||
| 341 | * instances will enable reception of compressed content. |
||
| 342 | * Note that enabling reception of compressed responses merely adds some standard http headers to xmlrpc requests. |
||
| 343 | * It is up to the xmlrpc server to return compressed responses when receiving such requests. |
||
| 344 | * |
||
| 345 | * @param string $compMethod either 'gzip', 'deflate', 'any' or '' |
||
| 346 | */ |
||
| 347 | public function setAcceptedCompression($compMethod) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Enables/disables http compression of xmlrpc request. |
||
| 360 | * |
||
| 361 | * This requires the "zlib" extension to be enabled in your php install. |
||
| 362 | * Take care when sending compressed requests: servers might not support them (and automatic fallback to |
||
| 363 | * uncompressed requests is not yet implemented). |
||
| 364 | * |
||
| 365 | * @param string $compMethod either 'gzip', 'deflate' or '' |
||
| 366 | */ |
||
| 367 | public function setRequestCompression($compMethod) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Adds a cookie to list of cookies that will be sent to server with every further request (useful e.g. for keeping |
||
| 374 | * session info outside of the xml-rpc payload). |
||
| 375 | * |
||
| 376 | * NB: By default cookies are sent using the 'original/netscape' format, which is also the same as the RFC 2965; |
||
| 377 | * setting any param but name and value will turn the cookie into a 'version 1' cookie (i.e. RFC 2109 cookie) that |
||
| 378 | * might not be fully supported by the server. Note that RFC 2109 has currently 'historic' status... |
||
| 379 | * |
||
| 380 | * @param string $name nb: will not be escaped in the request's http headers. Take care not to use CTL chars or |
||
| 381 | * separators! |
||
| 382 | * @param string $value |
||
| 383 | * @param string $path leave this empty unless the xml-rpc server only accepts RFC 2109 cookies |
||
| 384 | * @param string $domain leave this empty unless the xml-rpc server only accepts RFC 2109 cookies |
||
| 385 | * @param int $port leave this empty unless the xml-rpc server only accepts RFC 2109 cookies |
||
| 386 | * |
||
| 387 | * @todo check correctness of urlencoding cookie value (copied from php way of doing it, but php is generally sending |
||
| 388 | * response not requests. We do the opposite...) |
||
| 389 | * @todo strip invalid chars from cookie name? As per RFC6265, we should follow RFC2616, Section 2.2 |
||
| 390 | */ |
||
| 391 | public function setCookie($name, $value = '', $path = '', $domain = '', $port = null) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Directly set cURL options, for extra flexibility (when in cURL mode). |
||
| 406 | * |
||
| 407 | * It allows eg. to bind client to a specific IP interface / address. |
||
| 408 | * |
||
| 409 | * @param array $options |
||
| 410 | */ |
||
| 411 | public function setCurlOptions($options) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set user-agent string that will be used by this client instance in http headers sent to the server. |
||
| 418 | * |
||
| 419 | * The default user agent string includes the name of this library and the version number. |
||
| 420 | * |
||
| 421 | * @param string $agentString |
||
| 422 | */ |
||
| 423 | public function setUserAgent($agentString) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Send an xmlrpc request to the server. |
||
| 430 | * |
||
| 431 | * @param Request|Request[]|string $req The Request object, or an array of requests for using multicall, or the |
||
| 432 | * complete xml representation of a request. |
||
| 433 | * When sending an array of Request objects, the client will try to make use of |
||
| 434 | * a single 'system.multicall' xml-rpc method call to forward to the server all |
||
| 435 | * the requests in a single HTTP round trip, unless $this->no_multicall has |
||
| 436 | * been previously set to TRUE (see the multicall method below), in which case |
||
| 437 | * many consecutive xmlrpc requests will be sent. The method will return an |
||
| 438 | * array of Response objects in both cases. |
||
| 439 | * The third variant allows to build by hand (or any other means) a complete |
||
| 440 | * xmlrpc request message, and send it to the server. $req should be a string |
||
| 441 | * containing the complete xml representation of the request. It is e.g. useful |
||
| 442 | * when, for maximal speed of execution, the request is serialized into a |
||
| 443 | * string using the native php xmlrpc functions (see http://www.php.net/xmlrpc) |
||
| 444 | * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply. |
||
| 445 | * This timeout value is passed to fsockopen(). It is also used for detecting server |
||
| 446 | * timeouts during communication (i.e. if the server does not send anything to the client |
||
| 447 | * for $timeout seconds, the connection will be closed). |
||
| 448 | * @param string $method valid values are 'http', 'http11' and 'https'. If left unspecified, the http protocol |
||
| 449 | * chosen during creation of the object will be used. |
||
| 450 | * |
||
| 451 | * |
||
| 452 | * @return Response|Response[] Note that the client will always return a Response object, even if the call fails |
||
| 453 | */ |
||
| 454 | public function send($req, $timeout = 0, $method = '') |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param Request $req |
||
| 543 | * @param string $server |
||
| 544 | * @param int $port |
||
| 545 | * @param int $timeout |
||
| 546 | * @param string $username |
||
| 547 | * @param string $password |
||
| 548 | * @param int $authType |
||
| 549 | * @param string $proxyHost |
||
| 550 | * @param int $proxyPort |
||
| 551 | * @param string $proxyUsername |
||
| 552 | * @param string $proxyPassword |
||
| 553 | * @param int $proxyAuthType |
||
| 554 | * @param string $method |
||
| 555 | * @return Response |
||
| 556 | */ |
||
| 557 | protected function sendPayloadHTTP10($req, $server, $port, $timeout = 0, $username = '', $password = '', |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @param Request $req |
||
| 714 | * @param string $server |
||
| 715 | * @param int $port |
||
| 716 | * @param int $timeout |
||
| 717 | * @param string $username |
||
| 718 | * @param string $password |
||
| 719 | * @param int $authType |
||
| 720 | * @param string $cert |
||
| 721 | * @param string $certPass |
||
| 722 | * @param string $caCert |
||
| 723 | * @param string $caCertDir |
||
| 724 | * @param string $proxyHost |
||
| 725 | * @param int $proxyPort |
||
| 726 | * @param string $proxyUsername |
||
| 727 | * @param string $proxyPassword |
||
| 728 | * @param int $proxyAuthType |
||
| 729 | * @param bool $keepAlive |
||
| 730 | * @param string $key |
||
| 731 | * @param string $keyPass |
||
| 732 | * @param int $sslVersion |
||
| 733 | * @return Response |
||
| 734 | */ |
||
| 735 | protected function sendPayloadHTTPS($req, $server, $port, $timeout = 0, $username = '', $password = '', |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Contributed by Justin Miller <[email protected]> |
||
| 747 | * Requires curl to be built into PHP |
||
| 748 | * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers! |
||
| 749 | * |
||
| 750 | * @param Request $req |
||
| 751 | * @param string $server |
||
| 752 | * @param int $port |
||
| 753 | * @param int $timeout |
||
| 754 | * @param string $username |
||
| 755 | * @param string $password |
||
| 756 | * @param int $authType |
||
| 757 | * @param string $cert |
||
| 758 | * @param string $certPass |
||
| 759 | * @param string $caCert |
||
| 760 | * @param string $caCertDir |
||
| 761 | * @param string $proxyHost |
||
| 762 | * @param int $proxyPort |
||
| 763 | * @param string $proxyUsername |
||
| 764 | * @param string $proxyPassword |
||
| 765 | * @param int $proxyAuthType |
||
| 766 | * @param string $method |
||
| 767 | * @param bool $keepAlive |
||
| 768 | * @param string $key |
||
| 769 | * @param string $keyPass |
||
| 770 | * @param int $sslVersion |
||
| 771 | * @return Response |
||
| 772 | */ |
||
| 773 | protected function sendPayloadCURL($req, $server, $port, $timeout = 0, $username = '', $password = '', |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Send an array of requests and return an array of responses. |
||
| 994 | * |
||
| 995 | * Unless $this->no_multicall has been set to true, it will try first to use one single xmlrpc call to server method |
||
| 996 | * system.multicall, and revert to sending many successive calls in case of failure. |
||
| 997 | * This failure is also stored in $this->no_multicall for subsequent calls. |
||
| 998 | * Unfortunately, there is no server error code universally used to denote the fact that multicall is unsupported, |
||
| 999 | * so there is no way to reliably distinguish between that and a temporary failure. |
||
| 1000 | * If you are sure that server supports multicall and do not want to fallback to using many single calls, set the |
||
| 1001 | * fourth parameter to FALSE. |
||
| 1002 | * |
||
| 1003 | * NB: trying to shoehorn extra functionality into existing syntax has resulted |
||
| 1004 | * in pretty much convoluted code... |
||
| 1005 | * |
||
| 1006 | * @param Request[] $reqs an array of Request objects |
||
| 1007 | * @param integer $timeout connection timeout (in seconds). See the details in the docs for the send() method |
||
| 1008 | * @param string $method the http protocol variant to be used. See the details in the docs for the send() method |
||
| 1009 | * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be |
||
| 1010 | * attempted |
||
| 1011 | * |
||
| 1012 | * @return Response[] |
||
| 1013 | */ |
||
| 1014 | public function multicall($reqs, $timeout = 0, $method = '', $fallback = true) |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Attempt to boxcar $reqs via system.multicall. |
||
| 1065 | * |
||
| 1066 | * Returns either an array of Response, a single error Response or false (when received response does not respect |
||
| 1067 | * valid multicall syntax). |
||
| 1068 | * |
||
| 1069 | * @param Request[] $reqs |
||
| 1070 | * @param int $timeout |
||
| 1071 | * @param string $method |
||
| 1072 | * @return Response[]|bool|mixed|Response |
||
| 1073 | */ |
||
| 1074 | private function _try_multicall($reqs, $timeout, $method) |
||
| 1188 | } |
||
| 1189 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.