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)  | 
            ||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * Invokes custom operation (defined in vtiger_ws_operation table)  | 
            ||
| 354 | * @access public  | 
            ||
| 355 | * @param string $operation Name of the webservice to invoke  | 
            ||
| 356 | * @param array [$params = null] Parameter values to operation  | 
            ||
| 357 | * @param string [$method = 'POST'] HTTP request method (GET, POST etc)  | 
            ||
| 358 | * @return array Result object  | 
            ||
| 359 | */  | 
            ||
| 360 | public function invokeOperation($operation, array $params = null, $method = 'POST')  | 
            ||
| 384 | |||
| 385 | /**  | 
            ||
| 386 | * VTiger provides a simple query language for fetching data.  | 
            ||
| 387 | * This language is quite similar to select queries in SQL.  | 
            ||
| 388 | * There are limitations, the queries work on a single Module,  | 
            ||
| 389 | * embedded queries are not supported, and does not support joins.  | 
            ||
| 390 | * But this is still a powerful way of getting data from Vtiger.  | 
            ||
| 391 | * Query always limits its output to 100 records,  | 
            ||
| 392 | * Client application can use limit operator to get different records.  | 
            ||
| 393 | * @access public  | 
            ||
| 394 | * @param string $query SQL-like expression  | 
            ||
| 395 | * @return array Query results  | 
            ||
| 396 | */  | 
            ||
| 397 | public function query($query)  | 
            ||
| 415 | |||
| 416 | /**  | 
            ||
| 417 | * Retrieves an entity by ID  | 
            ||
| 418 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 419 | * @param string $entityID The ID of the entity to retrieve  | 
            ||
| 420 | * @return boolean Entity data  | 
            ||
| 421 | */  | 
            ||
| 422 | View Code Duplication | public function entityRetrieveByID($moduleName, $entityID)  | 
            |
| 438 | |||
| 439 | /**  | 
            ||
| 440 | * Uses VTiger queries to retrieve the entity matching a list of constraints  | 
            ||
| 441 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 442 | * @param array $params Data used to find a matching entry  | 
            ||
| 443 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields)  | 
            ||
| 444 | * @return int The matching record  | 
            ||
| 445 | */  | 
            ||
| 446 | public function entityRetrieve($moduleName, array $params, array $select = [])  | 
            ||
| 454 | |||
| 455 | /**  | 
            ||
| 456 | * Retrieves the ID of the entity matching a list of constraints + prepends '<module_id>x' string to it  | 
            ||
| 457 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 458 | * @param array $params Data used to find a matching entry  | 
            ||
| 459 | * @return int Type ID (a numeric ID + '<module_id>x')  | 
            ||
| 460 | */  | 
            ||
| 461 | public function entityRetrieveTypeID($moduleName, array $params)  | 
            ||
| 469 | |||
| 470 | /**  | 
            ||
| 471 | * Retrieve a numeric ID of the entity matching a list of constraints  | 
            ||
| 472 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 473 | * @param array $params Data used to find a matching entry  | 
            ||
| 474 | * @return int Numeric ID  | 
            ||
| 475 | */  | 
            ||
| 476 | public function entityRetrieveID($moduleName, array $params)  | 
            ||
| 484 | |||
| 485 | /**  | 
            ||
| 486 | * Creates an entity for the giving module  | 
            ||
| 487 | * @param string $moduleName Name of the module / entity type for which the entry has to be created  | 
            ||
| 488 | * @param array $params Entity data  | 
            ||
| 489 | * @return array Entity creation results  | 
            ||
| 490 | */  | 
            ||
| 491 | public function entityCreate($moduleName, array $params)  | 
            ||
| 514 | |||
| 515 | /**  | 
            ||
| 516 | * Updates an entity  | 
            ||
| 517 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 518 | * @param array $params Entity data  | 
            ||
| 519 | * @return array Entity update result  | 
            ||
| 520 | */  | 
            ||
| 521 | public function entityUpdate($moduleName, array $params)  | 
            ||
| 564 | |||
| 565 | /**  | 
            ||
| 566 | * Provides entity removal functionality  | 
            ||
| 567 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 568 | * @param string $entityID The ID of the entity to delete  | 
            ||
| 569 | * @return array Removal status object  | 
            ||
| 570 | */  | 
            ||
| 571 | View Code Duplication | public function entityDelete($moduleName, $entityID)  | 
            |
| 587 | |||
| 588 | /**  | 
            ||
| 589 | * Retrieves multiple records using module name and a set of constraints  | 
            ||
| 590 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 591 | * @param array $params Data used to find matching entries  | 
            ||
| 592 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields)  | 
            ||
| 593 | * @return int $limit limit the list of entries to N records (acts like LIMIT in SQL)  | 
            ||
| 594 | * @return bool|array The array containing matching entries or false if nothing was found  | 
            ||
| 595 | */  | 
            ||
| 596 | public function entitiesRetrieve($moduleName, array $params, array $select = [], $limit = 0)  | 
            ||
| 616 | |||
| 617 | /**  | 
            ||
| 618 | * Sync will return a sync result object containing details of changes after modifiedTime  | 
            ||
| 619 | * @param int [$modifiedTime = null] The date of the first change  | 
            ||
| 620 | * @param string [$moduleName = null] The name of the module / entity type  | 
            ||
| 621 | * @return array Sync result object  | 
            ||
| 622 | */  | 
            ||
| 623 | public function entitiesSync($modifiedTime = null, $moduleName = null)  | 
            ||
| 644 | |||
| 645 | /**  | 
            ||
| 646 | * Builds the query using the supplied parameters  | 
            ||
| 647 | * @param string $moduleName The name of the module / entity type  | 
            ||
| 648 | * @param array $params Data used to find matching entries  | 
            ||
| 649 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields)  | 
            ||
| 650 | * @return int $limit limit the list of entries to N records (acts like LIMIT in SQL)  | 
            ||
| 651 | * @return string The query build out of the supplied parameters  | 
            ||
| 652 | */  | 
            ||
| 653 | private function buildQuery($moduleName, array $params, array $select = [], $limit = 0)  | 
            ||
| 668 | |||
| 669 | /**  | 
            ||
| 670 | * Checks if if params holds valid entity data/search constraints, otherwise returns false  | 
            ||
| 671 | * @param array $params Array holding entity data/search constraints  | 
            ||
| 672 | * @return boolean Returns true if params holds valid entity data/search constraints, otherwise returns false  | 
            ||
| 673 | */  | 
            ||
| 674 | private function checkParams(array $params, $paramsPurpose)  | 
            ||
| 685 | |||
| 686 | /**  | 
            ||
| 687 | * A helper method, used to check in an array is associative or not  | 
            ||
| 688 | * @param string Array to test  | 
            ||
| 689 | * @return boolean Returns true in a given array is associative and false if it's not  | 
            ||
| 690 | */  | 
            ||
| 691 | private function isAssocArray(array $array)  | 
            ||
| 700 | }  | 
            ||
| 701 | 
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: