Complex classes like Queries 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 Queries, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Queries extends Stats |
||
| 27 | { |
||
| 28 | |||
| 29 | public function orders() |
||
| 34 | |||
| 35 | |||
| 36 | public function orders_orderby($orderby) |
||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | public function orders_today() |
||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | public function orders_todayByStatus($status) |
||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | public function orders_thisMonth() |
||
| 60 | |||
| 61 | public function orders_thisMonthByStatus($status) |
||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | public function orders_thisYear() |
||
| 73 | |||
| 74 | |||
| 75 | public function orders_thisYearByStatus($status) |
||
| 80 | |||
| 81 | |||
| 82 | public function get_orders_with_status($status) |
||
| 86 | |||
| 87 | public function pending_orders() |
||
| 91 | |||
| 92 | public function cancelled_orders() |
||
| 96 | |||
| 97 | public function completed_orders() |
||
| 101 | |||
| 102 | public function getOrderById($orderid) |
||
| 106 | |||
| 107 | public function getOrdersOnDate($date) |
||
| 111 | |||
| 112 | |||
| 113 | public function getOrdersOnDateRange($startdate, $enddate) |
||
| 116 | |||
| 117 | |||
| 118 | /** Function to get an invoice */ |
||
| 119 | public function getInvoiceById($invoiceid) |
||
| 123 | |||
| 124 | |||
| 125 | /** Function to get an invoice */ |
||
| 126 | public function getInvoiceByOrderId($orderid) |
||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * |
||
| 135 | * User Queries |
||
| 136 | */ |
||
| 137 | |||
| 138 | |||
| 139 | /** Function to get given user orders */ |
||
| 140 | public function getUserOrders($userid) |
||
| 144 | |||
| 145 | |||
| 146 | /** Function to get given user orders by status */ |
||
| 147 | public function getUserOrdersByStatus($userid, $status) |
||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | /** Function to get orders by status */ |
||
| 156 | public function getOrdersByStatus( $status) |
||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | /** Function to get given user invoices */ |
||
| 164 | public function getUserInvoices($userid) |
||
| 168 | |||
| 169 | |||
| 170 | /** Function that returns total number of user invoices */ |
||
| 171 | public function totalUserInvoices($userid): int |
||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | /** Function to get given user invoices by status */ |
||
| 180 | public function getUserInvoiceByStatus($userid, $status) |
||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** Function to get given all invoices by status */ |
||
| 189 | public function getInvoiceByStatus( $status) |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $userid |
||
| 197 | * @param $status |
||
| 198 | * @return mixed |
||
| 199 | * Function to get given user invoices by status |
||
| 200 | */ |
||
| 201 | public function sumUserInvoiceByStatus($userid, $status) : int |
||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param $status |
||
| 212 | * @return mixed |
||
| 213 | * Function to get given user invoices by status |
||
| 214 | */ |
||
| 215 | public function totalInvoiceByStatus($status) |
||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @param $status |
||
| 226 | * @return mixed |
||
| 227 | * Function to get given user invoices by status |
||
| 228 | */ |
||
| 229 | public function sumInvoiceByStatus($status) |
||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @param $userid |
||
| 239 | * @param $status |
||
| 240 | * @return mixed |
||
| 241 | * Function to get given user invoices by status |
||
| 242 | */ |
||
| 243 | public function totalUserInvoiceByStatus($userid, $status) |
||
| 247 | |||
| 248 | /** Function that gets full billing history */ |
||
| 249 | |||
| 250 | public function getAllBillingHistory() |
||
| 255 | |||
| 256 | |||
| 257 | /** Function that gets full user billing history*/ |
||
| 258 | |||
| 259 | public function getUserBillingHistory($userid) |
||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | public function getUserBillingHistoryByStartDate($userid, $start_date) |
||
| 273 | |||
| 274 | |||
| 275 | /** Function to get payment history of a given user for a given period */ |
||
| 276 | |||
| 277 | public function getUserBillingHistoryBetweenDates($userid, $start_date, $enddate) |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns the model instance to be updated |
||
| 286 | */ |
||
| 287 | public function updateBillingHistory($invoiceid): BillingPayment |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns the model instance to be updated |
||
| 295 | */ |
||
| 296 | public function updateInvoiceByInstance($invoiceid): Invoice |
||
| 300 | |||
| 301 | /** |
||
| 302 | * |
||
| 303 | * Returns updated invoice with new due_date |
||
| 304 | */ |
||
| 305 | public function updateDueDate($date, $invoiceid): Invoice |
||
| 312 | |||
| 313 | |||
| 314 | |||
| 315 | public function getBillingHistoryByStatus($status) |
||
| 320 | |||
| 321 | |||
| 322 | public function getUserBillingHistoryByStatus($userid, $status) |
||
| 327 | |||
| 328 | /** Return OrderItems for particular order |
||
| 329 | *@param $orderid |
||
| 330 | **/ |
||
| 331 | public function getOrderItems($orderid) |
||
| 336 | |||
| 337 | |||
| 338 | /** Return OrderItems for particular order gets invoiceid |
||
| 339 | *@param $invoiceid |
||
| 340 | **/ |
||
| 341 | public function getOrderItemsByInvoiceId($invoiceid) |
||
| 346 | |||
| 347 | public function getPaymentMethods(){ |
||
| 351 | |||
| 352 | public function getPaymentMethodById($pid){ |
||
| 356 | |||
| 357 | public function editPaymentMethod($pmethod_id, $pmethod) |
||
| 364 | |||
| 365 | |||
| 366 | public function addPaymentMethod(string $pmethod) |
||
| 377 | |||
| 378 | |||
| 379 | } |
||
| 380 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.