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 |
||
15 | class ZohoClient |
||
16 | { |
||
17 | /** |
||
18 | * URL for call request. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | const BASE_URI = 'https://crm.zoho.com/crm/private'; |
||
23 | |||
24 | /** |
||
25 | * Token used for session of request. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $authtoken; |
||
30 | |||
31 | /** |
||
32 | * Instance of the client. |
||
33 | * |
||
34 | * @var Client |
||
35 | */ |
||
36 | protected $zohoRestClient; |
||
37 | |||
38 | /** |
||
39 | * Format selected for get request. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $format; |
||
44 | |||
45 | /** |
||
46 | * Module selected for get request. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $module; |
||
51 | |||
52 | /** |
||
53 | * Construct. |
||
54 | * |
||
55 | * @param string $authtoken Token for connection |
||
56 | * @param Client $zohoRestClient Guzzl Client for connection [optional] |
||
57 | */ |
||
58 | public function __construct($authtoken, Client $zohoRestClient = null) |
||
65 | |||
66 | /** |
||
67 | * Implements convertLead API method. |
||
68 | * |
||
69 | * @param $leadId |
||
70 | * @param $data |
||
71 | * @param array $params |
||
72 | * |
||
73 | * @return Response The Response object |
||
74 | * |
||
75 | * @throws ZohoCRMResponseException |
||
76 | */ |
||
77 | public function convertLead($leadId, $data, $params = array()) |
||
85 | |||
86 | /** |
||
87 | * Implements getFields API method. |
||
88 | * |
||
89 | * @return Response The Response object |
||
90 | */ |
||
91 | public function getFields($module) |
||
97 | |||
98 | /** |
||
99 | * Implements deleteRecords API method. |
||
100 | * |
||
101 | * @param string $module |
||
102 | * @param string $id Id of the record |
||
103 | * |
||
104 | * @return Response The Response object |
||
105 | * |
||
106 | * @throws ZohoCRMResponseException |
||
107 | */ |
||
108 | public function deleteRecords($module, $id) |
||
117 | |||
118 | /** |
||
119 | * Implements getRecordById API method. |
||
120 | * |
||
121 | * @param string $module The module to use |
||
122 | * @param string $id Id of the record or a list of IDs separated by a semicolon |
||
123 | * |
||
124 | * @return Response The Response object |
||
125 | * |
||
126 | * @throws ZohoCRMResponseException |
||
127 | */ |
||
128 | public function getRecordById($module, $id) |
||
139 | |||
140 | /** |
||
141 | * Implements getRecords API method. |
||
142 | * |
||
143 | * @param $module |
||
144 | * @param $sortColumnString |
||
145 | * @param $sortOrderString |
||
146 | * @param \DateTime $lastModifiedTime |
||
147 | * @param $selectColumns |
||
148 | * @param $fromIndex |
||
149 | * @param $toIndex |
||
150 | * |
||
151 | * @return Response The Response object |
||
152 | * |
||
153 | * @throws ZohoCRMResponseException |
||
154 | */ |
||
155 | public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null) |
||
180 | |||
181 | /** |
||
182 | * Implements getDeletedRecordIds API method. |
||
183 | |||
184 | * |
||
185 | * @param string $module |
||
186 | * @param \DateTimeInterface $lastModifiedTime |
||
187 | * @param int $fromIndex |
||
188 | * @param int $toIndex |
||
189 | * |
||
190 | * @return Response |
||
191 | * |
||
192 | * @throws ZohoCRMResponseException |
||
193 | */ |
||
194 | public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null) |
||
209 | |||
210 | /** |
||
211 | * Implements getRecords API method. |
||
212 | * |
||
213 | * @param $module |
||
214 | * @param $id |
||
215 | * @param $parentModule |
||
216 | * @param null $fromIndex |
||
217 | * @param null $toIndex |
||
218 | * |
||
219 | * @return Response |
||
220 | * |
||
221 | * @throws ZohoCRMResponseException |
||
222 | */ |
||
223 | public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null) |
||
237 | |||
238 | /** |
||
239 | * Implements searchRecords API method. |
||
240 | * |
||
241 | * @param string $module |
||
242 | * @param string $searchCondition |
||
243 | * @param int $fromIndex |
||
244 | * @param int $toIndex |
||
245 | * @param \DateTime $lastModifiedTime |
||
246 | * @param null $selectColumns |
||
247 | * |
||
248 | * @return Response |
||
249 | * |
||
250 | * @throws ZohoCRMResponseException |
||
251 | */ |
||
252 | public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null) |
||
276 | |||
277 | /** |
||
278 | * Implements getUsers API method. |
||
279 | * |
||
280 | * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins) |
||
281 | * |
||
282 | * @return Response The array of Zoho Beans parsed from the response |
||
283 | * |
||
284 | * @throws ZohoCRMResponseException |
||
285 | */ |
||
286 | public function getUsers($type = 'AllUsers') |
||
304 | |||
305 | /** |
||
306 | * Implements insertRecords API method. |
||
307 | * |
||
308 | * @param $module |
||
309 | * @param \SimpleXMLElement$xmlData |
||
310 | * @param bool $wfTrigger |
||
311 | * @param int $duplicateCheck |
||
312 | * @param bool $isApproval |
||
313 | * |
||
314 | * @return Response |
||
315 | * |
||
316 | * @throws ZohoCRMResponseException |
||
317 | */ |
||
318 | public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2) |
||
334 | |||
335 | /** |
||
336 | * Implements updateRecords API method. |
||
337 | * |
||
338 | * @param $module |
||
339 | * @param \SimpleXMLElement $xmlData |
||
340 | * @param string $id |
||
341 | * @param bool $wfTrigger |
||
342 | * |
||
343 | * @return Response |
||
344 | * |
||
345 | * @throws ZohoCRMResponseException |
||
346 | */ |
||
347 | public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2) |
||
360 | |||
361 | /** |
||
362 | * Implements uploadFile API method. |
||
363 | * |
||
364 | * @param $module |
||
365 | * @param $id |
||
366 | * @param $content |
||
367 | * |
||
368 | * @return Response |
||
369 | * |
||
370 | * @throws ZohoCRMResponseException |
||
371 | */ |
||
372 | public function uploadFile($module, $id, $content) |
||
379 | |||
380 | /** |
||
381 | * Implements downloadFile API method. |
||
382 | * |
||
383 | * @param $module |
||
384 | * @param $id |
||
385 | * |
||
386 | * @return Response |
||
387 | * |
||
388 | * @throws ZohoCRMResponseException |
||
389 | */ |
||
390 | public function downloadFile($module, $id) |
||
396 | |||
397 | /** |
||
398 | * Returns a list of modules from Zoho. |
||
399 | */ |
||
400 | public function getModules() |
||
404 | |||
405 | |||
406 | /** |
||
407 | * Get the body of the request |
||
408 | * |
||
409 | * @param array $params Params |
||
410 | * @param Object $data Data |
||
411 | * @return string |
||
412 | */ |
||
413 | protected function getRequestBody($params, $data, $options) |
||
420 | |||
421 | |||
422 | /** |
||
423 | * Make the call using the client. |
||
424 | * |
||
425 | * @param string $module The module to use |
||
426 | * @param string $command Command to call |
||
427 | * @param array $params Options |
||
428 | * @param \SimpleXMLElement|string $data Data to send [optional] |
||
429 | * @param array $options Options to add for configurations [optional] |
||
430 | * |
||
431 | * @return Response |
||
432 | */ |
||
433 | public function call($module, $command, $getParams = array(), $postParams = array()) |
||
450 | |||
451 | /** |
||
452 | * Get the current request uri. |
||
453 | * |
||
454 | * @param $module The module to use |
||
455 | * @param string $command Command for get uri |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | protected function getRequestURI($module, $command) |
||
468 | } |
||
469 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.