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 ZohoClient 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 ZohoClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ZohoClient |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * .com URL for call request. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | const COM_BASE_URI = 'https://crm.zoho.com/crm/private'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * .eu URL for call request. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | const EU_BASE_URI = 'https://crm.zoho.eu/crm/private'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Configurable URL for call request. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $baseUri; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Token used for session of request. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $authtoken; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Instance of the client. |
||
| 48 | * |
||
| 49 | * @var Client |
||
| 50 | */ |
||
| 51 | protected $zohoRestClient; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Format selected for get request. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $format; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Module selected for get request. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $module; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Construct. |
||
| 69 | * |
||
| 70 | * @param string $authtoken Token for connection |
||
| 71 | * @param Client $zohoRestClient Guzzl Client for connection [optional] |
||
| 72 | * @param string $baseUri Configurable URL for call request |
||
| 73 | */ |
||
| 74 | public function __construct(string $authtoken, Client $zohoRestClient = null, string $baseUri = self::COM_BASE_URI) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Implements convertLead API method. |
||
| 85 | * |
||
| 86 | * @param $leadId |
||
| 87 | * @param $data |
||
| 88 | * @param array $params |
||
| 89 | * |
||
| 90 | * @return Response The Response object |
||
| 91 | * |
||
| 92 | * @throws ZohoCRMResponseException |
||
| 93 | */ |
||
| 94 | public function convertLead($leadId, $data, $params = array()) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Implements getFields API method. |
||
| 105 | * |
||
| 106 | * @return Response The Response object |
||
| 107 | */ |
||
| 108 | public function getFields($module) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Implements deleteRecords API method. |
||
| 117 | * |
||
| 118 | * @param string $module |
||
| 119 | * @param string $id Id of the record |
||
| 120 | * |
||
| 121 | * @return Response The Response object |
||
| 122 | * |
||
| 123 | * @throws ZohoCRMResponseException |
||
| 124 | */ |
||
| 125 | public function deleteRecords($module, $id) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Implements getRecordById API method. |
||
| 137 | * |
||
| 138 | * @param string $module The module to use |
||
| 139 | * @param string $id Id of the record or a list of IDs separated by a semicolon |
||
| 140 | * |
||
| 141 | * @return Response The Response object |
||
| 142 | * |
||
| 143 | * @throws ZohoCRMResponseException |
||
| 144 | */ |
||
| 145 | public function getRecordById($module, $id) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Implements getRecords API method. |
||
| 159 | * |
||
| 160 | * @param $module |
||
| 161 | * @param $sortColumnString |
||
| 162 | * @param $sortOrderString |
||
| 163 | * @param \DateTime $lastModifiedTime |
||
| 164 | * @param $selectColumns |
||
| 165 | * @param $fromIndex |
||
| 166 | * @param $toIndex |
||
| 167 | * |
||
| 168 | * @return Response The Response object |
||
| 169 | * |
||
| 170 | * @throws ZohoCRMResponseException |
||
| 171 | */ |
||
| 172 | public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Implements getDeletedRecordIds API method. |
||
| 200 | |||
| 201 | * |
||
| 202 | * @param string $module |
||
| 203 | * @param \DateTimeInterface $lastModifiedTime |
||
| 204 | * @param int $fromIndex |
||
| 205 | * @param int $toIndex |
||
| 206 | * |
||
| 207 | * @return Response |
||
| 208 | * |
||
| 209 | * @throws ZohoCRMResponseException |
||
| 210 | */ |
||
| 211 | public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Implements getRecords API method. |
||
| 229 | * |
||
| 230 | * @param $module |
||
| 231 | * @param $id |
||
| 232 | * @param $parentModule |
||
| 233 | * @param null $fromIndex |
||
| 234 | * @param null $toIndex |
||
| 235 | * |
||
| 236 | * @return Response |
||
| 237 | * |
||
| 238 | * @throws ZohoCRMResponseException |
||
| 239 | */ |
||
| 240 | public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Implements searchRecords API method. |
||
| 257 | * |
||
| 258 | * @param string $module |
||
| 259 | * @param string $searchCondition |
||
| 260 | * @param int $fromIndex |
||
| 261 | * @param int $toIndex |
||
| 262 | * @param \DateTime $lastModifiedTime |
||
| 263 | * @param null $selectColumns |
||
| 264 | * |
||
| 265 | * @return Response |
||
| 266 | * |
||
| 267 | * @throws ZohoCRMResponseException |
||
| 268 | */ |
||
| 269 | public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Implements getUsers API method. |
||
| 296 | * |
||
| 297 | * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins) |
||
| 298 | * |
||
| 299 | * @return Response The array of Zoho Beans parsed from the response |
||
| 300 | * |
||
| 301 | * @throws ZohoCRMResponseException |
||
| 302 | */ |
||
| 303 | public function getUsers($type = 'AllUsers') |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Implements insertRecords API method. |
||
| 324 | * |
||
| 325 | * @param $module |
||
| 326 | * @param \SimpleXMLElement$xmlData |
||
| 327 | * @param bool $wfTrigger |
||
| 328 | * @param int $duplicateCheck |
||
| 329 | * @param bool $isApproval |
||
| 330 | * |
||
| 331 | * @return Response |
||
| 332 | * |
||
| 333 | * @throws ZohoCRMResponseException |
||
| 334 | */ |
||
| 335 | public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Implements updateRecords API method. |
||
| 354 | * |
||
| 355 | * @param $module |
||
| 356 | * @param \SimpleXMLElement $xmlData |
||
| 357 | * @param string $id |
||
| 358 | * @param bool $wfTrigger |
||
| 359 | * |
||
| 360 | * @return Response |
||
| 361 | * |
||
| 362 | * @throws ZohoCRMResponseException |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Implements updateRelatedRecords API method. |
||
| 380 | * |
||
| 381 | * @param $module |
||
| 382 | * @param $relatedModule |
||
| 383 | * @param \SimpleXMLElement $xmlData |
||
| 384 | * @param string $id |
||
| 385 | * @param bool $wfTrigger |
||
| 386 | * |
||
| 387 | * @return Response |
||
| 388 | * |
||
| 389 | * @throws ZohoCRMResponseException |
||
| 390 | */ |
||
| 391 | View Code Duplication | public function updateRelatedRecords($module, $relatedModule, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Implements uploadFile API method. |
||
| 408 | * |
||
| 409 | * @param $module |
||
| 410 | * @param $id |
||
| 411 | * @param $content |
||
| 412 | * @param $filename |
||
| 413 | * |
||
| 414 | * @return Response |
||
| 415 | * |
||
| 416 | * @throws ZohoCRMResponseException |
||
| 417 | */ |
||
| 418 | public function uploadFile($module, $id, $content, $filename = null) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Implements downloadFile API method. |
||
| 449 | * |
||
| 450 | * @param $module |
||
| 451 | * @param $id |
||
| 452 | * |
||
| 453 | * @return Response |
||
| 454 | * |
||
| 455 | * @throws ZohoCRMResponseException |
||
| 456 | */ |
||
| 457 | public function downloadFile($module, $id) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns a list of modules from Zoho. |
||
| 466 | */ |
||
| 467 | public function getModules() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Make the call using the client. |
||
| 474 | * |
||
| 475 | * @param string $module The module to use |
||
| 476 | * @param string $command Command to call |
||
| 477 | * @param array $params Options |
||
| 478 | * @param \SimpleXMLElement|string $data Data to send [optional] |
||
| 479 | * @param array $options Options to add for configurations [optional] |
||
| 480 | * |
||
| 481 | * @return Response |
||
| 482 | */ |
||
| 483 | public function call($module, $command, $getParams = array(), $postParams = array()) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the current request uri. |
||
| 508 | * |
||
| 509 | * @param $module The module to use |
||
| 510 | * @param string $command Command for get uri |
||
| 511 | * |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | protected function getRequestURI($module, $command) |
||
| 523 | } |
||
| 524 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.