Complex classes like Orders 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 Orders, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Orders extends DolibarrApi |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 34 | */ |
||
| 35 | static $FIELDS = array( |
||
| 36 | 'socid' |
||
| 37 | ); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Commande $commande {@type Commande} |
||
| 41 | */ |
||
| 42 | public $commande; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | */ |
||
| 47 | function __construct() |
||
| 48 | { |
||
| 49 | global $db, $conf; |
||
| 50 | $this->db = $db; |
||
| 51 | $this->commande = new Commande($this->db); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get properties of a commande object |
||
| 56 | * |
||
| 57 | * Return an array with commande informations |
||
| 58 | * |
||
| 59 | * @param int $id ID of order |
||
| 60 | * @return array|mixed data without useless information |
||
| 61 | * |
||
| 62 | * @throws RestException |
||
| 63 | */ |
||
| 64 | function get($id) |
||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * List orders |
||
| 87 | * |
||
| 88 | * Get a list of orders |
||
| 89 | * |
||
| 90 | * @param string $sortfield Sort field |
||
| 91 | * @param string $sortorder Sort order |
||
| 92 | * @param int $limit Limit for list |
||
| 93 | * @param int $page Page number |
||
| 94 | * @param string $thirdparty_ids Thirdparty ids to filter orders of. {@example '1' or '1,2,3'} {@pattern /^[0-9,]*$/i} |
||
| 95 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 96 | * @return array Array of order objects |
||
| 97 | * |
||
| 98 | * @throws RestException |
||
| 99 | */ |
||
| 100 | function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') { |
||
| 101 | global $db, $conf; |
||
| 102 | |||
| 103 | $obj_ret = array(); |
||
| 104 | |||
| 105 | // case of external user, $thirdparty_ids param is ignored and replaced by user's socid |
||
| 106 | $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids; |
||
| 107 | |||
| 108 | // If the internal user must only see his customers, force searching by him |
||
| 109 | $search_sale = 0; |
||
| 110 | if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; |
||
| 111 | |||
| 112 | $sql = "SELECT t.rowid"; |
||
| 113 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) |
||
| 114 | $sql.= " FROM ".MAIN_DB_PREFIX."commande as t"; |
||
| 115 | |||
| 116 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale |
||
| 117 | |||
| 118 | $sql.= ' WHERE t.entity IN ('.getEntity('commande', 1).')'; |
||
| 119 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc"; |
||
| 120 | if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")"; |
||
| 121 | if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
||
| 122 | // Insert sale filter |
||
| 123 | if ($search_sale > 0) |
||
| 124 | { |
||
| 125 | $sql .= " AND sc.fk_user = ".$search_sale; |
||
| 126 | } |
||
| 127 | // Add sql filters |
||
| 128 | if ($sqlfilters) |
||
| 129 | { |
||
| 130 | if (! DolibarrApi::_checkFilters($sqlfilters)) |
||
| 131 | { |
||
| 132 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 133 | } |
||
| 134 | $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 135 | $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 136 | } |
||
| 137 | |||
| 138 | $sql.= $db->order($sortfield, $sortorder); |
||
| 139 | if ($limit) { |
||
| 140 | if ($page < 0) |
||
| 141 | { |
||
| 142 | $page = 0; |
||
| 143 | } |
||
| 144 | $offset = $limit * $page; |
||
| 145 | |||
| 146 | $sql.= $db->plimit($limit + 1, $offset); |
||
| 147 | } |
||
| 148 | |||
| 149 | dol_syslog("API Rest request"); |
||
| 150 | $result = $db->query($sql); |
||
| 151 | |||
| 152 | if ($result) |
||
| 153 | { |
||
| 154 | $num = $db->num_rows($result); |
||
| 155 | while ($i < min($num, ($limit <= 0 ? $num : $limit))) |
||
|
|
|||
| 156 | { |
||
| 157 | $obj = $db->fetch_object($result); |
||
| 158 | $commande_static = new Commande($db); |
||
| 159 | if($commande_static->fetch($obj->rowid)) { |
||
| 160 | $obj_ret[] = $this->_cleanObjectDatas($commande_static); |
||
| 161 | } |
||
| 162 | $i++; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | else { |
||
| 166 | throw new RestException(503, 'Error when retrieve commande list : '.$db->lasterror()); |
||
| 167 | } |
||
| 168 | if( ! count($obj_ret)) { |
||
| 169 | throw new RestException(404, 'No order found'); |
||
| 170 | } |
||
| 171 | return $obj_ret; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Create order object |
||
| 176 | * |
||
| 177 | * @param array $request_data Request data |
||
| 178 | * @return int ID of commande |
||
| 179 | */ |
||
| 180 | function post($request_data = NULL) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get lines of an order |
||
| 207 | * |
||
| 208 | * @param int $id Id of order |
||
| 209 | * |
||
| 210 | * @url GET {id}/lines |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | function getLines($id) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add a line to given order |
||
| 237 | * |
||
| 238 | * @param int $id Id of commande to update |
||
| 239 | * @param array $request_data Orderline data |
||
| 240 | * |
||
| 241 | * @url POST {id}/lines |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | function postLine($id, $request_data = NULL) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Update a line to given order |
||
| 296 | * |
||
| 297 | * @param int $id Id of commande to update |
||
| 298 | * @param int $lineid Id of line to update |
||
| 299 | * @param array $request_data Orderline data |
||
| 300 | * |
||
| 301 | * @url PUT {id}/lines/{lineid} |
||
| 302 | * |
||
| 303 | * @return object |
||
| 304 | */ |
||
| 305 | function putLine($id, $lineid, $request_data = NULL) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Delete a line to given order |
||
| 353 | * |
||
| 354 | * |
||
| 355 | * @param int $id Id of commande to update |
||
| 356 | * @param int $lineid Id of line to delete |
||
| 357 | * |
||
| 358 | * @url DELETE {id}/lines/{lineid} |
||
| 359 | * |
||
| 360 | * @return int |
||
| 361 | */ |
||
| 362 | function delLine($id, $lineid) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Update order general fields (won't touch lines of order) |
||
| 385 | * |
||
| 386 | * @param int $id Id of commande to update |
||
| 387 | * @param array $request_data Datas |
||
| 388 | * |
||
| 389 | * @return int |
||
| 390 | */ |
||
| 391 | function put($id, $request_data = NULL) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Delete order |
||
| 417 | * |
||
| 418 | * @param int $id Order ID |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | function delete($id) |
||
| 423 | { |
||
| 424 | if(! DolibarrApiAccess::$user->rights->commande->supprimer) { |
||
| 425 | throw new RestException(401); |
||
| 426 | } |
||
| 427 | $result = $this->commande->fetch($id); |
||
| 428 | if( ! $result ) { |
||
| 429 | throw new RestException(404, 'Order not found'); |
||
| 430 | } |
||
| 431 | |||
| 432 | if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { |
||
| 433 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 434 | } |
||
| 435 | |||
| 436 | if( ! $this->commande->delete(DolibarrApiAccess::$user)) { |
||
| 437 | throw new RestException(500, 'Error when delete order : '.$this->commande->error); |
||
| 438 | } |
||
| 439 | |||
| 440 | return array( |
||
| 441 | 'success' => array( |
||
| 442 | 'code' => 200, |
||
| 443 | 'message' => 'Order deleted' |
||
| 444 | ) |
||
| 445 | ); |
||
| 446 | |||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Validate an order |
||
| 451 | * |
||
| 452 | * @param int $id Order ID |
||
| 453 | * @param int $idwarehouse Warehouse ID |
||
| 454 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 455 | * |
||
| 456 | * @url POST {id}/validate |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | * FIXME An error 403 is returned if the request has an empty body. |
||
| 460 | * Error message: "Forbidden: Content type `text/plain` is not supported." |
||
| 461 | * Workaround: send this in the body |
||
| 462 | * { |
||
| 463 | * "idwarehouse": 0, |
||
| 464 | * "notrigger": 0 |
||
| 465 | * } |
||
| 466 | */ |
||
| 467 | function validate($id, $idwarehouse=0, $notrigger=0) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Clean sensible object datas |
||
| 499 | * |
||
| 500 | * @param object $object Object to clean |
||
| 501 | * @return array Array of cleaned object properties |
||
| 502 | */ |
||
| 503 | function _cleanObjectDatas($object) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Validate fields before create or update object |
||
| 514 | * |
||
| 515 | * @param array $data Array with data to verify |
||
| 516 | * @return array |
||
| 517 | * @throws RestException |
||
| 518 | */ |
||
| 519 | function _validate($data) |
||
| 530 | } |
||
| 531 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: