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 RpcClient 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 RpcClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Comodojo\RpcClient; |
||
| 32 | class RpcClient { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Default disabled: native funcs are faster but are not compatible with base64, datetime |
||
| 36 | * and cdata values (as implemented here) |
||
| 37 | * |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | static private $useNativeEncoder = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Enable comodojo encrypted transport |
||
| 44 | * |
||
| 45 | * @var mixed |
||
| 46 | */ |
||
| 47 | private $encrypt = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * RPC protocol |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $protocol = 'XML'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Characters encoding |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $encoding = 'utf-8'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Autoclean requests |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $autoclean = true; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Supported RPC protocols |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $supported_protocols = array("XML", "JSON"); |
||
| 76 | |||
| 77 | // internals |
||
| 78 | |||
| 79 | private $sender = null; |
||
| 80 | |||
| 81 | private $requests = array(); |
||
| 82 | |||
| 83 | private $special_types = array(); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Class constructor |
||
| 87 | * |
||
| 88 | * @param string $server Remote RPC server address |
||
| 89 | * |
||
| 90 | * @throws \Comodojo\Exception\HttpException |
||
| 91 | */ |
||
| 92 | 51 | public function __construct($server) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Set RPC protocol |
||
| 112 | * |
||
| 113 | * @param string $protocol RPC protocol |
||
| 114 | * |
||
| 115 | * @return \Comodojo\RpcClient\RpcClient |
||
| 116 | * |
||
| 117 | * @throws \Exception |
||
| 118 | */ |
||
| 119 | 12 | final public function setProtocol($protocol) { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Set encryption key; this will enable the NOT-STANDARD payload encryption |
||
| 133 | * |
||
| 134 | * @param string $key Encryption key |
||
| 135 | * |
||
| 136 | * @return \Comodojo\RpcClient\RpcClient |
||
| 137 | * |
||
| 138 | * @throws \Exception |
||
| 139 | */ |
||
| 140 | final public function setEncryption($key) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set encoding (default to utf-8) |
||
| 152 | * |
||
| 153 | * @param string $encoding Characters encoding |
||
| 154 | * |
||
| 155 | * @return \Comodojo\RpcClient\RpcClient |
||
| 156 | */ |
||
| 157 | final public function setEncoding($encoding) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the XML encoder |
||
| 167 | * |
||
| 168 | * If true, the comodojo xmlrpc encoder will be used (default). Otherwise |
||
| 169 | * message will be encoded using PHP XML-RPC Functions. |
||
| 170 | * |
||
| 171 | * WARNING: using PHP XML-RPC Functions does not support special value |
||
| 172 | * types support! |
||
| 173 | * |
||
| 174 | * @param bool $mode |
||
| 175 | * |
||
| 176 | * @return \Comodojo\RpcClient\RpcClient |
||
| 177 | */ |
||
| 178 | final public function setXmlEncoder($mode = true) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set special type for a given values (referenced) |
||
| 190 | * |
||
| 191 | * @param mixed $value The given value (referenced) |
||
| 192 | * @param string $type The value type (base64, datetime or cdata) |
||
| 193 | * |
||
| 194 | * @return \Comodojo\RpcClient\RpcClient |
||
| 195 | * |
||
| 196 | * @throws \Exception |
||
| 197 | */ |
||
| 198 | 6 | final public function setValueType(&$value, $type) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Add request in queue |
||
| 210 | * |
||
| 211 | * @param string $method RPC method |
||
| 212 | * @param array $parameters Request parameters |
||
| 213 | * @param mixed $id Id (only for JSON RPC) |
||
| 214 | * |
||
| 215 | * @return \Comodojo\RpcClient\RpcClient |
||
| 216 | * |
||
| 217 | * @throws \Exception |
||
| 218 | */ |
||
| 219 | 51 | final public function addRequest($method, $parameters = array(), $id = true) { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Set autoclean on/off |
||
| 237 | * |
||
| 238 | * @param bool $mode If true, requests will be removed from queue at each send() |
||
| 239 | * |
||
| 240 | * @return \Comodojo\RpcClient\RpcClient |
||
| 241 | */ |
||
| 242 | final public function setAutoclean($mode = true) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get the transport layer |
||
| 252 | * |
||
| 253 | * This method will return the Httprequest object in order to customize transport |
||
| 254 | * options before sending request(s) |
||
| 255 | * |
||
| 256 | * @return \Comodojo\Httprequest\Httprequest |
||
| 257 | */ |
||
| 258 | 51 | final public function getTransport() { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Send request(s) to server |
||
| 266 | * |
||
| 267 | * @return mixed |
||
| 268 | * |
||
| 269 | * @throws \Comodojo\Exception\RpcException |
||
| 270 | * @throws \Comodojo\Exception\HttpException |
||
| 271 | * @throws \Comodojo\Exception\XmlrpcException |
||
| 272 | * @throws \Exception |
||
| 273 | */ |
||
| 274 | 51 | public function send() { |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Cleanup requests |
||
| 328 | * |
||
| 329 | * @return \Comodojo\RpcClient\RpcClient |
||
| 330 | */ |
||
| 331 | 51 | final public function cleanRequests() { |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Perform a json call |
||
| 343 | * |
||
| 344 | * @param array $request |
||
| 345 | * |
||
| 346 | * @return mixed |
||
| 347 | * |
||
| 348 | * @throws \Comodojo\Exception\RpcException |
||
| 349 | * @throws \Comodojo\Exception\HttpException |
||
| 350 | * @throws \Exception |
||
| 351 | */ |
||
| 352 | 9 | private function jsonCall($request) { |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Perform a json multicall |
||
| 394 | * |
||
| 395 | * @param array $requests |
||
| 396 | * |
||
| 397 | * @return array |
||
| 398 | * |
||
| 399 | * @throws \Comodojo\Exception\RpcException |
||
| 400 | * @throws \Comodojo\Exception\HttpException |
||
| 401 | * @throws \Exception |
||
| 402 | */ |
||
| 403 | 3 | private function jsonMulticall($requests) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Perform an xml call |
||
| 461 | * |
||
| 462 | * @param array $request |
||
| 463 | * |
||
| 464 | * @return mixed |
||
| 465 | * |
||
| 466 | * @throws \Comodojo\Exception\RpcException |
||
| 467 | * @throws \Comodojo\Exception\HttpException |
||
| 468 | * @throws \Comodojo\Exception\XmlrpcException |
||
| 469 | * @throws \Exception |
||
| 470 | */ |
||
| 471 | 36 | private function xmlCall($request) { |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Perform an xml multicall |
||
| 542 | * |
||
| 543 | * @param array $requests |
||
| 544 | * |
||
| 545 | * @return array |
||
| 546 | * |
||
| 547 | * @throws \Comodojo\Exception\RpcException |
||
| 548 | * @throws \Comodojo\Exception\HttpException |
||
| 549 | * @throws \Comodojo\Exception\XmlrpcException |
||
| 550 | * @throws \Exception |
||
| 551 | */ |
||
| 552 | 3 | private function xmlMulticall($requests) { |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Send pre-econded request to server |
||
| 621 | * |
||
| 622 | * @param string $data |
||
| 623 | * @param string $content_type |
||
| 624 | * |
||
| 625 | * @return string |
||
| 626 | * |
||
| 627 | * @throws \Comodojo\Exception\RpcException |
||
| 628 | * @throws \Comodojo\Exception\HttpException |
||
| 629 | */ |
||
| 630 | 51 | private function performCall($data, $content_type) { |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Check native encoder availability |
||
| 664 | * |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | 39 | private static function phpXmlrpcAvailable() { |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Split multicall xml requests |
||
| 675 | * |
||
| 676 | * @param array $requests |
||
| 677 | * |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | 3 | private static function splitMulticallXmlRequests($requests) { |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Compose a json request |
||
| 707 | * |
||
| 708 | * @param array $request |
||
| 709 | * |
||
| 710 | * @return array |
||
| 711 | */ |
||
| 712 | 12 | private static function composeJsonRequest($request) { |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Check if an encrypted envelop is consisent or not |
||
| 740 | * |
||
| 741 | * @param string $data |
||
| 742 | * |
||
| 743 | * @return bool |
||
| 744 | */ |
||
| 745 | private static function checkEncryptedResponseConsistency($data) { |
||
| 750 | |||
| 751 | } |
||
| 752 |
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.