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) |
||
| 112 | { |
||
| 113 | $params = array( |
||
| 114 | 'id' => $id, |
||
| 115 | 'newFormat' => 1, |
||
| 116 | ); |
||
| 117 | |||
| 118 | return $this->call($module, 'deleteRecords', $params); |
||
| 119 | } |
||
| 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) |
||
| 159 | { |
||
| 160 | $params['newFormat'] = 1; |
||
| 161 | $params['version'] = 1; |
||
| 162 | if ($selectColumns) { |
||
| 163 | $params['selectColumns'] = $selectColumns; |
||
| 164 | } |
||
| 165 | if ($fromIndex) { |
||
| 166 | $params['fromIndex'] = $fromIndex; |
||
| 167 | } |
||
| 168 | if ($toIndex) { |
||
| 169 | $params['toIndex'] = $toIndex; |
||
| 170 | } |
||
| 171 | if ($sortColumnString) { |
||
| 172 | $params['sortColumnString'] = $sortColumnString; |
||
| 173 | } |
||
| 174 | if ($sortOrderString) { |
||
| 175 | $params['sortOrderString'] = $sortOrderString; |
||
| 176 | } |
||
| 177 | if ($lastModifiedTime) { |
||
| 178 | $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s'); |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this->call($module, 'getRecords', $params); |
||
| 182 | } |
||
| 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) |
||
| 198 | { |
||
| 199 | $params = []; |
||
| 200 | if ($fromIndex) { |
||
| 201 | $params['fromIndex'] = $fromIndex; |
||
| 202 | } |
||
| 203 | if ($toIndex) { |
||
| 204 | $params['toIndex'] = $toIndex; |
||
| 205 | } |
||
| 206 | if ($lastModifiedTime) { |
||
| 207 | $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s'); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $this->call($module, 'getDeletedRecordIds', $params); |
||
| 211 | } |
||
| 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) |
||
| 227 | { |
||
| 228 | $params['id'] = $id; |
||
| 229 | $params['parentModule'] = $parentModule; |
||
| 230 | $params['newFormat'] = 1; |
||
| 231 | if ($fromIndex) { |
||
| 232 | $params['fromIndex'] = $fromIndex; |
||
| 233 | } |
||
| 234 | if ($toIndex) { |
||
| 235 | $params['toIndex'] = $toIndex; |
||
| 236 | } |
||
| 237 | |||
| 238 | return $this->call($module, 'getRelatedRecords', $params); |
||
| 239 | } |
||
| 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') |
||
| 290 | { |
||
| 291 | switch ($type) { |
||
| 292 | case 'AllUsers': |
||
| 293 | case 'ActiveUsers': |
||
| 294 | case 'DeactiveUsers': |
||
| 295 | case 'AdminUsers': |
||
| 296 | case 'ActiveConfirmedAdmins': |
||
| 297 | $params['type'] = $type; |
||
| 298 | break; |
||
| 299 | default : |
||
| 300 | $params['type'] = 'AllUsers'; |
||
| 301 | break; |
||
| 302 | } |
||
| 303 | $params['newFormat'] = 1; |
||
| 304 | |||
| 305 | return $this->call('Users', 'getUsers', $params); |
||
| 306 | } |
||
| 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) |
||
| 322 | { |
||
| 323 | if ($wfTrigger) { |
||
| 324 | $params['wfTrigger'] = 'true'; |
||
| 325 | } |
||
| 326 | if ($duplicateCheck) { |
||
| 327 | $params['duplicateCheck'] = $duplicateCheck; |
||
| 328 | } |
||
| 329 | if ($isApproval) { |
||
| 330 | $params['isApproval'] = 'true'; |
||
| 331 | } |
||
| 332 | $params['newFormat'] = $newFormat; |
||
| 333 | $params['version'] = $version; |
||
| 334 | |||
| 335 | return $this->call($module, 'insertRecords', $params, ['xmlData' => $xmlData->asXML()]); |
||
| 336 | } |
||
| 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) |
|
| 351 | { |
||
| 352 | $params['newFormat'] = $newFormat; |
||
| 353 | $params['version'] = $version; |
||
| 354 | if ($wfTrigger) { |
||
| 355 | $params['wfTrigger'] = 'true'; |
||
| 356 | } |
||
| 357 | if ($id) { |
||
| 358 | $params['id'] = $id; |
||
| 359 | } |
||
| 360 | |||
| 361 | return $this->call($module, 'updateRecords', $params, ['xmlData' => $xmlData->asXML()]); |
||
| 362 | } |
||
| 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) |
||
| 404 | { |
||
| 405 | $params['id'] = $id; |
||
| 406 | $params['content'] = $content; |
||
| 407 | |||
| 408 | return $this->call($module, 'uploadFile', $params); |
||
| 409 | } |
||
| 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.