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 |
||
43 | class WSClient |
||
44 | { |
||
45 | // HTTP Client instance |
||
46 | protected $httpClient = null; |
||
47 | |||
48 | // Service URL to which client connects to |
||
49 | protected $serviceBaseURL = 'webservice.php'; |
||
50 | |||
51 | // Webservice login validity |
||
52 | private $serviceServerTime = false; |
||
53 | private $serviceExpireTime = false; |
||
54 | private $serviceToken = false; |
||
55 | |||
56 | // Webservice user credentials |
||
57 | private $userName = false; |
||
58 | private $accessKey = false; |
||
59 | |||
60 | // Webservice login credentials |
||
61 | private $userID = false; |
||
62 | private $sessionName = false; |
||
63 | |||
64 | // Vtiger CRM and WebServices API version |
||
65 | private $apiVersion = false; |
||
66 | private $vtigerVersion = false; |
||
67 | |||
68 | // Last operation error information |
||
69 | protected $lastErrorMessage = false; |
||
70 | |||
71 | /** |
||
72 | * Class constructor |
||
73 | * @param string $url The URL of the remote WebServices server |
||
74 | */ |
||
75 | public function __construct($url) |
||
89 | |||
90 | /** |
||
91 | * Check if server response contains an error, therefore the requested operation has failed |
||
92 | * @access private |
||
93 | * @param array $jsonResult Server response object to check for errors |
||
94 | * @return boolean True if response object contains an error |
||
95 | */ |
||
96 | private function checkForError(array $jsonResult) // TODO move checkForError body to sendHttpRequest method's body |
||
110 | |||
111 | /** |
||
112 | * Checks and performs a login operation if requried and repeats login if needed |
||
113 | * @access private |
||
114 | */ |
||
115 | private function checkLogin() |
||
122 | |||
123 | /** |
||
124 | * Sends HTTP request to VTiger web service API endpoint |
||
125 | * @access private |
||
126 | * @param array $requestData HTTP request data |
||
127 | * @param string $method HTTP request method (GET, POST etc) |
||
128 | * @return array Returns request result object (null in case of failure) |
||
129 | */ |
||
130 | private function sendHttpRequest(array $requestData, $method = 'POST') |
||
159 | |||
160 | /** |
||
161 | * Gets a challenge token from the server and stores for future requests |
||
162 | * @access private |
||
163 | * @param string $username VTiger user name |
||
164 | * @return bool Returns false in case of failure |
||
165 | */ |
||
166 | private function passChallenge($username) |
||
184 | |||
185 | /** |
||
186 | * Login to the server using username and VTiger access key token |
||
187 | * @access public |
||
188 | * @param string $username VTiger user name |
||
189 | * @param string $accessKey VTiger access key token (visible on user profile/settings page) |
||
190 | * @return boolean Returns true if login operation has been successful |
||
191 | */ |
||
192 | public function login($username, $accessKey) |
||
224 | |||
225 | /** |
||
226 | * Allows you to login using username and password instead of access key (works on some VTige forks) |
||
227 | * @access public |
||
228 | * @param string $username VTiger user name |
||
229 | * @param string $password VTiger password (used to access CRM using the standard login page) |
||
230 | * @param string $accessKey This parameter will be filled with user's VTiger access key |
||
231 | * @return boolean Returns true if login operation has been successful |
||
232 | */ |
||
233 | public function loginPassword($username, $password, &$accessKey = null) |
||
257 | |||
258 | /** |
||
259 | * Gets last operation error, if any |
||
260 | * @access public |
||
261 | * @return WSClientError The error object |
||
262 | */ |
||
263 | public function getLastError() |
||
267 | |||
268 | /** |
||
269 | * Returns the client library version. |
||
270 | * @access public |
||
271 | * @return string Client library version |
||
272 | */ |
||
273 | public function getVersion() |
||
278 | |||
279 | /** |
||
280 | * Lists all the Vtiger entity types available through the API |
||
281 | * @access public |
||
282 | * @return array List of entity types |
||
283 | */ |
||
284 | public function getTypes() |
||
303 | |||
304 | /** |
||
305 | * Get the type information about a given VTiger entity type. |
||
306 | * @access public |
||
307 | * @param string $moduleName Name of the module / entity type |
||
308 | * @return array Result object |
||
309 | */ |
||
310 | public function getType($moduleName) |
||
323 | |||
324 | /** |
||
325 | * Gets the entity ID prepended with module / entity type ID |
||
326 | * @access private |
||
327 | * @param string $moduleName Name of the module / entity type |
||
328 | * @param string $entityID Numeric entity ID |
||
329 | * @return boolean|string Returns false if it is not possible to retrieve module / entity type ID |
||
330 | */ |
||
331 | private function getTypedID($moduleName, $entityID) |
||
346 | |||
347 | /** |
||
348 | * Invokes custom operation (defined in vtiger_ws_operation table) |
||
349 | * @access public |
||
350 | * @param string $operation Name of the webservice to invoke |
||
351 | * @param array [$params = null] Parameter values to operation |
||
352 | * @param string [$method = 'POST'] HTTP request method (GET, POST etc) |
||
353 | * @return array Result object |
||
354 | */ |
||
355 | public function invokeOperation($operation, array $params = null, $method = 'POST') |
||
377 | |||
378 | /** |
||
379 | * VTiger provides a simple query language for fetching data. |
||
380 | * This language is quite similar to select queries in SQL. |
||
381 | * There are limitations, the queries work on a single Module, |
||
382 | * embedded queries are not supported, and does not support joins. |
||
383 | * But this is still a powerful way of getting data from Vtiger. |
||
384 | * Query always limits its output to 100 records, |
||
385 | * Client application can use limit operator to get different records. |
||
386 | * @access public |
||
387 | * @param string $query SQL-like expression |
||
388 | * @return array Query results |
||
389 | */ |
||
390 | public function query($query) |
||
408 | |||
409 | /** |
||
410 | * Retrieves an entity by ID |
||
411 | * @param string $moduleName The name of the module / entity type |
||
412 | * @param string $entityID The ID of the entity to retrieve |
||
413 | * @return boolean Entity data |
||
414 | */ |
||
415 | View Code Duplication | public function entityRetrieveByID($moduleName, $entityID) |
|
431 | |||
432 | /** |
||
433 | * Uses VTiger queries to retrieve the entity matching a list of constraints |
||
434 | * @param string $moduleName The name of the module / entity type |
||
435 | * @param array $params Data used to find a matching entry |
||
436 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
437 | * @return int The matching record |
||
438 | */ |
||
439 | public function entityRetrieve($moduleName, array $params, array $select = []) |
||
447 | |||
448 | /** |
||
449 | * Uses VTiger queries to retrieve the ID of the entity matching a list of constraints |
||
450 | * @param string $moduleName The name of the module / entity type |
||
451 | * @param array $params Data used to find a matching entry |
||
452 | * @return int Numeric ID |
||
453 | */ |
||
454 | public function entityRetrieveID($moduleName, array $params) // TODO check if params is an assoc array |
||
467 | |||
468 | /** |
||
469 | * Creates an entity for the giving module |
||
470 | * @param string $moduleName Name of the module / entity type for which the entry has to be created |
||
471 | * @param array $params Entity data |
||
472 | * @return array Entity creation results |
||
473 | */ |
||
474 | public function entityCreate($moduleName, array $params) // TODO check if params is an assoc array |
||
493 | |||
494 | /** |
||
495 | * Updates an entity |
||
496 | * @param string $moduleName The name of the module / entity type |
||
497 | * @param array $params Entity data |
||
498 | * @return array Entity update result |
||
499 | */ |
||
500 | public function entityUpdate($moduleName, array $params) // TODO check if params is an assoc array |
||
532 | |||
533 | /** |
||
534 | * Provides entity removal functionality |
||
535 | * @param string $moduleName The name of the module / entity type |
||
536 | * @param string $entityID The ID of the entity to delete |
||
537 | * @return array Removal status object |
||
538 | */ |
||
539 | View Code Duplication | public function entityDelete($moduleName, $entityID) |
|
555 | |||
556 | /** |
||
557 | * Retrieves multiple records using module name and a set of constraints |
||
558 | * @param string $moduleName The name of the module / entity type |
||
559 | * @param array $params Data used to find the matching entries |
||
560 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
561 | * @return int $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
562 | * @return bool|array The array containing the matching entries or false if nothing was found |
||
563 | */ |
||
564 | public function entitiesRetrieve($moduleName, array $params, array $select = [], $limit = 0) |
||
586 | |||
587 | /** |
||
588 | * Sync will return a sync result object containing details of changes after modifiedTime |
||
589 | * @param int [$modifiedTime = null] The date of the first change |
||
590 | * @param string [$moduleName = null] The name of the module / entity type |
||
591 | * @return array Sync result object |
||
592 | */ |
||
593 | public function entitiesSync($modifiedTime = null, $moduleName = null) |
||
614 | |||
615 | /** |
||
616 | * Builds the query using the supplied parameters |
||
617 | * @param string $moduleName The name of the module / entity type |
||
618 | * @param array $params Data used to find the matching entries |
||
619 | * @return array $select The list of fields to select (defaults to SQL-like '*' - all the fields) |
||
620 | * @return int $limit limit the list of entries to N records (acts like LIMIT in SQL) |
||
621 | * @return string The query build out of the supplied parameters |
||
622 | */ |
||
623 | private function buildQuery($moduleName, array $params, array $select = [], $limit = 0) |
||
638 | |||
639 | /** |
||
640 | * A helper method, used to check in an array is associative or not |
||
641 | * @param string Array to test |
||
642 | * @return bool Returns true in a given array is associative and false if it's not |
||
643 | */ |
||
644 | private function isAssocArray(array $array) |
||
653 | } |
||
654 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..