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 |
||
| 14 | class ZohoClient |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * URL for call request. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $BASE_URI = 'https://crm.zoho.com/crm/private'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Token used for session of request. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $authtoken; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Instance of the client. |
||
| 32 | * |
||
| 33 | * @var Client |
||
| 34 | */ |
||
| 35 | protected $zohoRestClient; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Format selected for get request. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $format; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Module selected for get request. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $module; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Construct. |
||
| 53 | * |
||
| 54 | * @param string $authtoken Token for connection |
||
| 55 | * @param Client $zohoRestClient Guzzl Client for connection [optional] |
||
| 56 | */ |
||
| 57 | public function __construct($authtoken, Client $zohoRestClient = null) |
||
| 64 | |||
| 65 | public function setEuDomain() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Implements convertLead API method. |
||
| 71 | * |
||
| 72 | * @param $leadId |
||
| 73 | * @param $data |
||
| 74 | * @param array $params |
||
| 75 | * |
||
| 76 | * @return Response The Response object |
||
| 77 | * |
||
| 78 | * @throws ZohoCRMResponseException |
||
| 79 | */ |
||
| 80 | public function convertLead($leadId, $data, $params = array()) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Implements getFields API method. |
||
| 91 | * |
||
| 92 | * @return Response The Response object |
||
| 93 | */ |
||
| 94 | public function getFields($module) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Implements deleteRecords API method. |
||
| 103 | * |
||
| 104 | * @param string $module |
||
| 105 | * @param string $id Id of the record |
||
| 106 | * |
||
| 107 | * @return Response The Response object |
||
| 108 | * |
||
| 109 | * @throws ZohoCRMResponseException |
||
| 110 | */ |
||
| 111 | public function deleteRecords($module, $id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Implements getRecordById API method. |
||
| 123 | * |
||
| 124 | * @param string $module The module to use |
||
| 125 | * @param string $id Id of the record or a list of IDs separated by a semicolon |
||
| 126 | * |
||
| 127 | * @return Response The Response object |
||
| 128 | * |
||
| 129 | * @throws ZohoCRMResponseException |
||
| 130 | */ |
||
| 131 | public function getRecordById($module, $id) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Implements getRecords API method. |
||
| 145 | * |
||
| 146 | * @param $module |
||
| 147 | * @param $sortColumnString |
||
| 148 | * @param $sortOrderString |
||
| 149 | * @param \DateTime $lastModifiedTime |
||
| 150 | * @param $selectColumns |
||
| 151 | * @param $fromIndex |
||
| 152 | * @param $toIndex |
||
| 153 | * |
||
| 154 | * @return Response The Response object |
||
| 155 | * |
||
| 156 | * @throws ZohoCRMResponseException |
||
| 157 | */ |
||
| 158 | public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Implements getDeletedRecordIds API method. |
||
| 186 | |||
| 187 | * |
||
| 188 | * @param string $module |
||
| 189 | * @param \DateTimeInterface $lastModifiedTime |
||
| 190 | * @param int $fromIndex |
||
| 191 | * @param int $toIndex |
||
| 192 | * |
||
| 193 | * @return Response |
||
| 194 | * |
||
| 195 | * @throws ZohoCRMResponseException |
||
| 196 | */ |
||
| 197 | public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Implements getRecords API method. |
||
| 215 | * |
||
| 216 | * @param $module |
||
| 217 | * @param $id |
||
| 218 | * @param $parentModule |
||
| 219 | * @param null $fromIndex |
||
| 220 | * @param null $toIndex |
||
| 221 | * |
||
| 222 | * @return Response |
||
| 223 | * |
||
| 224 | * @throws ZohoCRMResponseException |
||
| 225 | */ |
||
| 226 | public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Implements searchRecords API method. |
||
| 243 | * |
||
| 244 | * @param string $module |
||
| 245 | * @param string $searchCondition |
||
| 246 | * @param int $fromIndex |
||
| 247 | * @param int $toIndex |
||
| 248 | * @param \DateTime $lastModifiedTime |
||
| 249 | * @param null $selectColumns |
||
| 250 | * |
||
| 251 | * @return Response |
||
| 252 | * |
||
| 253 | * @throws ZohoCRMResponseException |
||
| 254 | */ |
||
| 255 | public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Implements getUsers API method. |
||
| 282 | * |
||
| 283 | * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins) |
||
| 284 | * |
||
| 285 | * @return Response The array of Zoho Beans parsed from the response |
||
| 286 | * |
||
| 287 | * @throws ZohoCRMResponseException |
||
| 288 | */ |
||
| 289 | public function getUsers($type = 'AllUsers') |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Implements insertRecords API method. |
||
| 310 | * |
||
| 311 | * @param $module |
||
| 312 | * @param \SimpleXMLElement$xmlData |
||
| 313 | * @param bool $wfTrigger |
||
| 314 | * @param int $duplicateCheck |
||
| 315 | * @param bool $isApproval |
||
| 316 | * |
||
| 317 | * @return Response |
||
| 318 | * |
||
| 319 | * @throws ZohoCRMResponseException |
||
| 320 | */ |
||
| 321 | public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Implements updateRecords API method. |
||
| 340 | * |
||
| 341 | * @param $module |
||
| 342 | * @param \SimpleXMLElement $xmlData |
||
| 343 | * @param string $id |
||
| 344 | * @param bool $wfTrigger |
||
| 345 | * |
||
| 346 | * @return Response |
||
| 347 | * |
||
| 348 | * @throws ZohoCRMResponseException |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Implements updateRelatedRecords API method. |
||
| 366 | * |
||
| 367 | * @param $module |
||
| 368 | * @param $relatedModule |
||
| 369 | * @param \SimpleXMLElement $xmlData |
||
| 370 | * @param string $id |
||
| 371 | * @param bool $wfTrigger |
||
| 372 | * |
||
| 373 | * @return Response |
||
| 374 | * |
||
| 375 | * @throws ZohoCRMResponseException |
||
| 376 | */ |
||
| 377 | View Code Duplication | public function updateRelatedRecords($module, $relatedModule, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Implements uploadFile API method. |
||
| 394 | * |
||
| 395 | * @param $module |
||
| 396 | * @param $id |
||
| 397 | * @param $content |
||
| 398 | * |
||
| 399 | * @return Response |
||
| 400 | * |
||
| 401 | * @throws ZohoCRMResponseException |
||
| 402 | */ |
||
| 403 | public function uploadFile($module, $id, $content) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Implements downloadFile API method. |
||
| 413 | * |
||
| 414 | * @param $module |
||
| 415 | * @param $id |
||
| 416 | * |
||
| 417 | * @return Response |
||
| 418 | * |
||
| 419 | * @throws ZohoCRMResponseException |
||
| 420 | */ |
||
| 421 | public function downloadFile($module, $id) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Returns a list of modules from Zoho. |
||
| 430 | */ |
||
| 431 | public function getModules() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Make the call using the client. |
||
| 438 | * |
||
| 439 | * @param string $module The module to use |
||
| 440 | * @param string $command Command to call |
||
| 441 | * @param array $params Options |
||
| 442 | * @param \SimpleXMLElement|string $data Data to send [optional] |
||
| 443 | * @param array $options Options to add for configurations [optional] |
||
| 444 | * |
||
| 445 | * @return Response |
||
| 446 | */ |
||
| 447 | public function call($module, $command, $getParams = array(), $postParams = array()) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get the current request uri. |
||
| 472 | * |
||
| 473 | * @param $module The module to use |
||
| 474 | * @param string $command Command for get uri |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | protected function getRequestURI($module, $command) |
||
| 487 | } |
||
| 488 |
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.