Complex classes like Invoices 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 Invoices, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Invoices extends DolibarrApi |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * |
||
| 32 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 33 | */ |
||
| 34 | static $FIELDS = array( |
||
| 35 | 'socid' |
||
| 36 | ); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Facture $invoice {@type Facture} |
||
| 40 | */ |
||
| 41 | public $invoice; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | */ |
||
| 46 | function __construct() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get properties of a invoice object |
||
| 55 | * |
||
| 56 | * Return an array with invoice informations |
||
| 57 | * |
||
| 58 | * @param int $id ID of invoice |
||
| 59 | * @return array|mixed data without useless information |
||
| 60 | * |
||
| 61 | * @throws RestException |
||
| 62 | */ |
||
| 63 | function get($id) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * List invoices |
||
| 83 | * |
||
| 84 | * Get a list of invoices |
||
| 85 | * |
||
| 86 | * @param string $sortfield Sort field |
||
| 87 | * @param string $sortorder Sort order |
||
| 88 | * @param int $limit Limit for list |
||
| 89 | * @param int $page Page number |
||
| 90 | * @param string $thirdparty_ids Thirdparty ids to filter orders of. {@example '1' or '1,2,3'} {@pattern /^[0-9,]*$/i} |
||
| 91 | * @param string $status Filter by invoice status : draft | unpaid | paid | cancelled |
||
| 92 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 93 | * @return array Array of invoice objects |
||
| 94 | * |
||
| 95 | * @throws RestException |
||
| 96 | */ |
||
| 97 | function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $thirdparty_ids='', $status='', $sqlfilters = '') { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Create invoice object |
||
| 179 | * |
||
| 180 | * @param array $request_data Request datas |
||
| 181 | * @return int ID of invoice |
||
| 182 | */ |
||
| 183 | function post($request_data = NULL) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Update invoice |
||
| 214 | * |
||
| 215 | * @param int $id Id of invoice to update |
||
| 216 | * @param array $request_data Datas |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | function put($id, $request_data = NULL) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Delete invoice |
||
| 247 | * |
||
| 248 | * @param int $id Invoice ID |
||
| 249 | * @return type |
||
| 250 | */ |
||
| 251 | function delete($id) |
||
| 252 | { |
||
| 253 | if(! DolibarrApiAccess::$user->rights->facture->supprimer) { |
||
| 254 | throw new RestException(401); |
||
| 255 | } |
||
| 256 | $result = $this->invoice->fetch($id); |
||
| 257 | if( ! $result ) { |
||
| 258 | throw new RestException(404, 'Invoice not found'); |
||
| 259 | } |
||
| 260 | |||
| 261 | if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { |
||
| 262 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 263 | } |
||
| 264 | |||
| 265 | if( $this->invoice->delete($id) < 0) |
||
| 266 | { |
||
| 267 | throw new RestException(500); |
||
| 268 | } |
||
| 269 | |||
| 270 | return array( |
||
| 271 | 'success' => array( |
||
| 272 | 'code' => 200, |
||
| 273 | 'message' => 'Invoice deleted' |
||
| 274 | ) |
||
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Validate fields before create or update object |
||
| 280 | * |
||
| 281 | * @param array|null $data Datas to validate |
||
| 282 | * @return array |
||
| 283 | * |
||
| 284 | * @throws RestException |
||
| 285 | */ |
||
| 286 | function _validate($data) |
||
| 296 | |||
| 297 | } |
||
| 298 |