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 WSClient 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 WSClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class WSClient |
||
45 | { |
||
46 | // HTTP Client instance |
||
47 | protected $httpClient = null; |
||
48 | |||
49 | // Service URL to which client connects to |
||
50 | protected $serviceBaseURL = 'webservice.php'; |
||
51 | |||
52 | // Webservice login validity |
||
53 | private $serviceServerTime = false; |
||
54 | private $serviceExpireTime = false; |
||
55 | private $serviceToken = false; |
||
56 | |||
57 | // Webservice user credentials |
||
58 | private $userName = false; |
||
59 | private $accessKey = false; |
||
60 | |||
61 | // Webservice login credentials |
||
62 | private $userID = false; |
||
63 | private $sessionName = false; |
||
64 | |||
65 | // Vtiger CRM and WebServices API version |
||
66 | private $apiVersion = false; |
||
67 | private $vtigerVersion = false; |
||
68 | |||
69 | // Last operation error information |
||
70 | protected $lastErrorMessage = null; |
||
71 | |||
72 | /** |
||
73 | * Class constructor |
||
74 | * @param string $url The URL of the remote WebServices server |
||
75 | */ |
||
76 | public function __construct($url) |
||
90 | |||
91 | /** |
||
92 | * Check if server response contains an error, therefore the requested operation has failed |
||
93 | * @access private |
||
94 | * @param array $jsonResult Server response object to check for errors |
||
95 | * @return boolean True if response object contains an error |
||
96 | */ |
||
97 | private function checkForError(array $jsonResult) |
||
111 | |||
112 | /** |
||
113 | * Checks and performs a login operation if requried and repeats login if needed |
||
114 | * @access private |
||
115 | */ |
||
116 | private function checkLogin() |
||
123 | |||
124 | /** |
||
125 | * Sends HTTP request to VTiger web service API endpoint |
||
126 | * @access private |
||
127 | * @param array $requestData HTTP request data |
||
128 | * @param string $method HTTP request method (GET, POST etc) |
||
129 | * @return array Returns request result object (null in case of failure) |
||
130 | */ |
||
131 | private function sendHttpRequest(array $requestData, $method = 'POST') |
||
160 | |||
161 | /** |
||
162 | * Gets a challenge token from the server and stores for future requests |
||
163 | * @access private |
||
164 | * @param string $username VTiger user name |
||
165 | * @return booleanReturns false in case of failure |
||
166 | */ |
||
167 | private function passChallenge($username) |
||
185 | |||
186 | /** |
||
187 | * Login to the server using username and VTiger access key token |
||
188 | * @access public |
||
189 | * @param string $username VTiger user name |
||
190 | * @param string $accessKey VTiger access key token (visible on user profile/settings page) |
||
191 | * @return boolean Returns true if login operation has been successful |
||
192 | */ |
||
193 | public function login($username, $accessKey) |
||
225 | |||
226 | /** |
||
227 | * Allows you to login using username and password instead of access key (works on some VTige forks) |
||
228 | * @access public |
||
229 | * @param string $username VTiger user name |
||
230 | * @param string $password VTiger password (used to access CRM using the standard login page) |
||
231 | * @param string $accessKey This parameter will be filled with user's VTiger access key |
||
232 | * @return boolean Returns true if login operation has been successful |
||
233 | */ |
||
234 | public function loginPassword($username, $password, &$accessKey = null) |
||
258 | |||
259 | /** |
||
260 | * Gets last operation error, if any |
||
261 | * @access public |
||
262 | * @return WSClientError The error object |
||
263 | */ |
||
264 | public function getLastError() |
||
268 | |||
269 | /** |
||
270 | * Returns the client library version. |
||
271 | * @access public |
||
272 | * @return string Client library version |
||
273 | */ |
||
274 | public function getVersion() |
||
279 | |||
280 | /** |
||
281 | * Lists all the Vtiger entity types available through the API |
||
282 | * @access public |
||
283 | * @return array List of entity types |
||
284 | */ |
||
285 | public function getTypes() |
||
304 | |||
305 | /** |
||
306 | * Get the type information about a given VTiger entity type. |
||
307 | * @access public |
||
308 | * @param string $moduleName Name of the module / entity type |
||
309 | * @return array Result object |
||
310 | */ |
||
311 | public function getType($moduleName) |
||
324 | |||
325 | /** |
||
326 | * Gets the entity ID prepended with module / entity type ID |
||
327 | * @access private |
||
328 | * @param string $moduleName Name of the module / entity type |
||
329 | * @param string $entityID Numeric entity ID |
||
330 | * @return boolean|string Returns false if it is not possible to retrieve module / entity type ID |
||
331 | */ |
||
332 | private function getTypedID($moduleName, $entityID) |
||
347 | |||
348 | /** |
||
349 | * Invokes custom operation (defined in vtiger_ws_operation table) |
||
350 | * @access public |
||
351 | * @param string $operation Name of the webservice to invoke |
||
352 | * @param array [$params = null] Parameter values to operation |
||
353 | * @param string [$method = 'POST'] HTTP request method (GET, POST etc) |
||
354 | * @return array Result object |
||
355 | */ |
||
356 | public function invokeOperation($operation, array $params = null, $method = 'POST') |
||
380 | |||
381 | /** |
||
382 | * VTiger provides a simple query language for fetching data. |
||
383 | * This language is quite similar to select queries in SQL. |
||
384 | * There are limitations, the queries work on a single Module, |
||
385 | * embedded queries are not supported, and does not support joins. |
||
386 | * But this is still a powerful way of getting data from Vtiger. |
||
387 | * Query always limits its output to 100 records, |
||
388 | * Client application can use limit operator to get different records. |
||
389 | * @access public |
||
390 | * @param string $query SQL-like expression |
||
391 | * @return array Query results |
||
392 | */ |
||
393 | public function query($query) |
||
411 | |||
412 | /** |
||
413 | * Retrieves an entity by ID |
||
414 | * @param string $moduleName The name of the module / entity type |
||
415 | * @param string $entityID The ID of the entity to retrieve |
||
416 | * @return boolean Entity data |
||
417 | */ |
||
418 | View Code Duplication | public function entityRetrieveByID($moduleName, $entityID) |
|
434 | |||
435 | /** |
||
436 | * Uses VTiger queries to retrieve the entity matching a list of constraints |
||
437 | * @param string $moduleName The name of the module / entity type |
||
438 | * @param array $params Data used to find a matching entry |
||
439 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
440 | * @return int The matching record |
||
441 | */ |
||
442 | public function entityRetrieve($moduleName, array $params, array $select = []) |
||
450 | |||
451 | /** |
||
452 | * Uses VTiger queries to retrieve the ID of the entity matching a list of constraints |
||
453 | * @param string $moduleName The name of the module / entity type |
||
454 | * @param array $params Data used to find a matching entry |
||
455 | * @return int Numeric ID |
||
456 | */ |
||
457 | public function entityRetrieveID($moduleName, array $params) |
||
470 | |||
471 | /** |
||
472 | * Creates an entity for the giving module |
||
473 | * @param string $moduleName Name of the module / entity type for which the entry has to be created |
||
474 | * @param array $params Entity data |
||
475 | * @return array Entity creation results |
||
476 | */ |
||
477 | public function entityCreate($moduleName, array $params) |
||
500 | |||
501 | /** |
||
502 | * Updates an entity |
||
503 | * @param string $moduleName The name of the module / entity type |
||
504 | * @param array $params Entity data |
||
505 | * @return array Entity update result |
||
506 | */ |
||
507 | public function entityUpdate($moduleName, array $params) |
||
543 | |||
544 | /** |
||
545 | * Provides entity removal functionality |
||
546 | * @param string $moduleName The name of the module / entity type |
||
547 | * @param string $entityID The ID of the entity to delete |
||
548 | * @return array Removal status object |
||
549 | */ |
||
550 | View Code Duplication | public function entityDelete($moduleName, $entityID) |
|
566 | |||
567 | /** |
||
568 | * Retrieves multiple records using module name and a set of constraints |
||
569 | * @param string $moduleName The name of the module / entity type |
||
570 | * @param array $params Data used to find matching entries |
||
571 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
572 | * @return int $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
573 | * @return bool|array The array containing matching entries or false if nothing was found |
||
574 | */ |
||
575 | public function entitiesRetrieve($moduleName, array $params, array $select = [], $limit = 0) |
||
595 | |||
596 | /** |
||
597 | * Sync will return a sync result object containing details of changes after modifiedTime |
||
598 | * @param int [$modifiedTime = null] The date of the first change |
||
599 | * @param string [$moduleName = null] The name of the module / entity type |
||
600 | * @return array Sync result object |
||
601 | */ |
||
602 | public function entitiesSync($modifiedTime = null, $moduleName = null) |
||
623 | |||
624 | /** |
||
625 | * Builds the query using the supplied parameters |
||
626 | * @param string $moduleName The name of the module / entity type |
||
627 | * @param array $params Data used to find matching entries |
||
628 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
629 | * @return int $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
630 | * @return string The query build out of the supplied parameters |
||
631 | */ |
||
632 | private function buildQuery($moduleName, array $params, array $select = [], $limit = 0) |
||
647 | |||
648 | /** |
||
649 | * Checks if if params holds valid entity data/search constraints, otherwise returns false |
||
650 | * @param array $params Array holding entity data/search constraints |
||
651 | * @return boolean Returns true if params holds valid entity data/search constraints, otherwise returns false |
||
652 | */ |
||
653 | private function checkParams(array $params, $paramsPurpose) |
||
664 | |||
665 | /** |
||
666 | * A helper method, used to check in an array is associative or not |
||
667 | * @param string Array to test |
||
668 | * @return boolean Returns true in a given array is associative and false if it's not |
||
669 | */ |
||
670 | private function isAssocArray(array $array) |
||
679 | } |
||
680 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: