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 | const 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 | /** |
||
| 66 | * Implements convertLead API method. |
||
| 67 | * |
||
| 68 | * @param $leadId |
||
| 69 | * @param $data |
||
| 70 | * @param array $params |
||
| 71 | * |
||
| 72 | * @return Response The Response object |
||
| 73 | * |
||
| 74 | * @throws ZohoCRMResponseException |
||
| 75 | */ |
||
| 76 | public function convertLead($leadId, $data, $params = array()) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Implements getFields API method. |
||
| 87 | * |
||
| 88 | * @return Response The Response object |
||
| 89 | */ |
||
| 90 | public function getFields($module) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Implements deleteRecords API method. |
||
| 99 | * |
||
| 100 | * @param string $module |
||
| 101 | * @param string $id Id of the record |
||
| 102 | * |
||
| 103 | * @return Response The Response object |
||
| 104 | * |
||
| 105 | * @throws ZohoCRMResponseException |
||
| 106 | */ |
||
| 107 | public function deleteRecords($module, $id) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Implements getRecordById API method. |
||
| 119 | * |
||
| 120 | * @param string $module The module to use |
||
| 121 | * @param string $id Id of the record or a list of IDs separated by a semicolon |
||
| 122 | * |
||
| 123 | * @return Response The Response object |
||
| 124 | * |
||
| 125 | * @throws ZohoCRMResponseException |
||
| 126 | */ |
||
| 127 | public function getRecordById($module, $id) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Implements getRecords API method. |
||
| 141 | * |
||
| 142 | * @param $module |
||
| 143 | * @param $sortColumnString |
||
| 144 | * @param $sortOrderString |
||
| 145 | * @param \DateTime $lastModifiedTime |
||
| 146 | * @param $selectColumns |
||
| 147 | * @param $fromIndex |
||
| 148 | * @param $toIndex |
||
| 149 | * |
||
| 150 | * @return Response The Response object |
||
| 151 | * |
||
| 152 | * @throws ZohoCRMResponseException |
||
| 153 | */ |
||
| 154 | public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Implements getDeletedRecordIds API method. |
||
| 182 | |||
| 183 | * |
||
| 184 | * @param string $module |
||
| 185 | * @param \DateTimeInterface $lastModifiedTime |
||
| 186 | * @param int $fromIndex |
||
| 187 | * @param int $toIndex |
||
| 188 | * |
||
| 189 | * @return Response |
||
| 190 | * |
||
| 191 | * @throws ZohoCRMResponseException |
||
| 192 | */ |
||
| 193 | public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Implements getRecords API method. |
||
| 211 | * |
||
| 212 | * @param $module |
||
| 213 | * @param $id |
||
| 214 | * @param $parentModule |
||
| 215 | * @param null $fromIndex |
||
| 216 | * @param null $toIndex |
||
| 217 | * |
||
| 218 | * @return Response |
||
| 219 | * |
||
| 220 | * @throws ZohoCRMResponseException |
||
| 221 | */ |
||
| 222 | public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Implements searchRecords API method. |
||
| 239 | * |
||
| 240 | * @param string $module |
||
| 241 | * @param string $searchCondition |
||
| 242 | * @param int $fromIndex |
||
| 243 | * @param int $toIndex |
||
| 244 | * @param \DateTime $lastModifiedTime |
||
| 245 | * @param null $selectColumns |
||
| 246 | * |
||
| 247 | * @return Response |
||
| 248 | * |
||
| 249 | * @throws ZohoCRMResponseException |
||
| 250 | */ |
||
| 251 | public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Implements getUsers API method. |
||
| 278 | * |
||
| 279 | * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins) |
||
| 280 | * |
||
| 281 | * @return Response The array of Zoho Beans parsed from the response |
||
| 282 | * |
||
| 283 | * @throws ZohoCRMResponseException |
||
| 284 | */ |
||
| 285 | public function getUsers($type = 'AllUsers') |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Implements insertRecords API method. |
||
| 306 | * |
||
| 307 | * @param $module |
||
| 308 | * @param \SimpleXMLElement$xmlData |
||
| 309 | * @param bool $wfTrigger |
||
| 310 | * @param int $duplicateCheck |
||
| 311 | * @param bool $isApproval |
||
| 312 | * |
||
| 313 | * @return Response |
||
| 314 | * |
||
| 315 | * @throws ZohoCRMResponseException |
||
| 316 | */ |
||
| 317 | public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Implements updateRecords API method. |
||
| 336 | * |
||
| 337 | * @param $module |
||
| 338 | * @param \SimpleXMLElement $xmlData |
||
| 339 | * @param string $id |
||
| 340 | * @param bool $wfTrigger |
||
| 341 | * |
||
| 342 | * @return Response |
||
| 343 | * |
||
| 344 | * @throws ZohoCRMResponseException |
||
| 345 | */ |
||
| 346 | public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Implements uploadFile API method. |
||
| 362 | * |
||
| 363 | * @param $module |
||
| 364 | * @param $id |
||
| 365 | * @param $content |
||
| 366 | * @param resource|string $content |
||
| 367 | * |
||
| 368 | * @return Response |
||
| 369 | * |
||
| 370 | * @throws ZohoCRMResponseException |
||
| 371 | */ |
||
| 372 | public function uploadFile($module, $id, $content, $filename=null) |
||
| 373 | { |
||
| 374 | $getParams['id'] = $id; |
||
| 375 | $postParams = [ |
||
| 376 | [ |
||
| 377 | 'name' => 'content', |
||
| 378 | 'contents' => $content, |
||
| 379 | 'filename' => $filename, |
||
| 380 | ] |
||
| 381 | ]; |
||
| 382 | return $this->call($module, 'uploadFile', $getParams, $postParams); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param $module |
||
| 387 | * @param $id |
||
| 388 | * @param $attachmentUrl |
||
| 389 | * |
||
| 390 | * @return Response |
||
| 391 | * |
||
| 392 | * @throws ZohoCRMResponseException |
||
| 393 | */ |
||
| 394 | public function attachFileUrl($module, $id, $attachmentUrl) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Implements downloadFile API method. |
||
| 403 | * |
||
| 404 | * @param $module |
||
| 405 | * @param $id |
||
| 406 | * |
||
| 407 | * @return Response |
||
| 408 | * |
||
| 409 | * @throws ZohoCRMResponseException |
||
| 410 | */ |
||
| 411 | public function downloadFile($module, $id) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Returns a list of modules from Zoho. |
||
| 420 | */ |
||
| 421 | public function getModules() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Make the call using the client. |
||
| 428 | * |
||
| 429 | * @param string $module The module to use |
||
| 430 | * @param string $command Command to call |
||
| 431 | * @param array $params Options |
||
| 432 | * @param \SimpleXMLElement|string $data Data to send [optional] |
||
| 433 | * @param array $options Options to add for configurations [optional] |
||
| 434 | * |
||
| 435 | * @return Response |
||
| 436 | */ |
||
| 437 | public function call($module, $command, $getParams = array(), $postParams = array()) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the current request uri. |
||
| 462 | * |
||
| 463 | * @param string $module The module to use |
||
| 464 | * @param string $command Command for get uri |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | protected function getRequestURI($module, $command) |
||
| 477 | } |
||
| 478 |
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.