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:
| 1 | <?php |
||
| 23 | class WC_REST_Report_Sales_Controller extends WC_REST_Controller { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Endpoint namespace. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $namespace = 'wc/v1'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Route base. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $rest_base = 'reports/sales'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Report instance. |
||
| 41 | * |
||
| 42 | * @var WC_Admin_Report |
||
| 43 | */ |
||
| 44 | protected $report; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Register the routes for sales reports. |
||
| 48 | */ |
||
| 49 | View Code Duplication | public function register_routes() { |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Check whether a given request has permission to read report. |
||
| 63 | * |
||
| 64 | * @param WP_REST_Request $request Full details about the request. |
||
| 65 | * @return WP_Error|boolean |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function get_items_permissions_check( $request ) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get sales reports. |
||
| 77 | * |
||
| 78 | * @param WP_REST_Request $request |
||
| 79 | * @return array|WP_Error |
||
| 80 | */ |
||
| 81 | public function get_items( $request ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Prepare a report sales object for serialization. |
||
| 91 | * |
||
| 92 | * @param null $_ |
||
| 93 | * @param WP_REST_Request $request Request object. |
||
| 94 | * @return WP_REST_Response $response Response data. |
||
| 95 | */ |
||
| 96 | public function prepare_item_for_response( $_, $request ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Setup the report object and parse any date filtering. |
||
| 242 | * |
||
| 243 | * @param array $filter date filtering |
||
| 244 | */ |
||
| 245 | protected function setup_report( $filter ) { |
||
| 246 | include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' ); |
||
| 247 | include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php' ); |
||
| 248 | |||
| 249 | $this->report = new WC_Report_Sales_By_Date(); |
||
| 250 | |||
| 251 | if ( empty( $filter['period'] ) ) { |
||
| 252 | |||
| 253 | // Custom date range. |
||
| 254 | $filter['period'] = 'custom'; |
||
| 255 | |||
| 256 | View Code Duplication | if ( ! empty( $filter['date_min'] ) || ! empty( $filter['date_max'] ) ) { |
|
| 257 | |||
| 258 | // Overwrite _GET to make use of WC_Admin_Report::calculate_current_range() for custom date ranges. |
||
| 259 | $_GET['start_date'] = $filter['date_min']; |
||
| 260 | $_GET['end_date'] = isset( $filter['date_max'] ) ? $filter['date_max'] : null; |
||
| 261 | |||
| 262 | } else { |
||
| 263 | |||
| 264 | // Default custom range to today. |
||
| 265 | $_GET['start_date'] = $_GET['end_date'] = date( 'Y-m-d', current_time( 'timestamp' ) ); |
||
| 266 | } |
||
| 267 | |||
| 268 | } else { |
||
| 269 | |||
| 270 | $filter['period'] = empty( $filter['period'] ) ? 'week' : $filter['period']; |
||
| 271 | |||
| 272 | // Change "week" period to "7day". |
||
| 273 | if ( 'week' === $filter['period'] ) { |
||
| 274 | $filter['period'] = '7day'; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->report->calculate_current_range( $filter['period'] ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get the Report's schema, conforming to JSON Schema. |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function get_item_schema() { |
|
| 287 | $schema = array( |
||
| 288 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||
| 289 | 'title' => 'sales_report', |
||
| 290 | 'type' => 'object', |
||
| 291 | 'properties' => array( |
||
| 292 | 'total_sales' => array( |
||
| 293 | 'description' => __( 'Gross sales in the period.', 'woocommerce' ), |
||
| 294 | 'type' => 'string', |
||
| 295 | 'context' => array( 'view' ), |
||
| 296 | 'readonly' => true, |
||
| 297 | ), |
||
| 298 | 'net_sales' => array( |
||
| 299 | 'description' => __( 'Net sales in the period.', 'woocommerce' ), |
||
| 300 | 'type' => 'string', |
||
| 301 | 'context' => array( 'view' ), |
||
| 302 | 'readonly' => true, |
||
| 303 | ), |
||
| 304 | 'average_sales' => array( |
||
| 305 | 'description' => __( 'Average net daily sales.', 'woocommerce' ), |
||
| 306 | 'type' => 'string', |
||
| 307 | 'context' => array( 'view' ), |
||
| 308 | 'readonly' => true, |
||
| 309 | ), |
||
| 310 | 'total_orders' => array( |
||
| 311 | 'description' => __( 'Total of orders placed.', 'woocommerce' ), |
||
| 312 | 'type' => 'integer', |
||
| 313 | 'context' => array( 'view' ), |
||
| 314 | 'readonly' => true, |
||
| 315 | ), |
||
| 316 | 'total_items' => array( |
||
| 317 | 'description' => __( 'Total of items purchased.', 'woocommerce' ), |
||
| 318 | 'type' => 'integer', |
||
| 319 | 'context' => array( 'view' ), |
||
| 320 | 'readonly' => true, |
||
| 321 | ), |
||
| 322 | 'total_tax' => array( |
||
| 323 | 'description' => __( 'Total charged for taxes.', 'woocommerce' ), |
||
| 324 | 'type' => 'string', |
||
| 325 | 'context' => array( 'view' ), |
||
| 326 | 'readonly' => true, |
||
| 327 | ), |
||
| 328 | 'total_shipping' => array( |
||
| 329 | 'description' => __( 'Total charged for shipping.', 'woocommerce' ), |
||
| 330 | 'type' => 'string', |
||
| 331 | 'context' => array( 'view' ), |
||
| 332 | 'readonly' => true, |
||
| 333 | ), |
||
| 334 | 'total_refunds' => array( |
||
| 335 | 'description' => __( 'Total of refunded orders.', 'woocommerce' ), |
||
| 336 | 'type' => 'integer', |
||
| 337 | 'context' => array( 'view' ), |
||
| 338 | 'readonly' => true, |
||
| 339 | ), |
||
| 340 | 'total_discount' => array( |
||
| 341 | 'description' => __( 'Total of coupons used.', 'woocommerce' ), |
||
| 342 | 'type' => 'integer', |
||
| 343 | 'context' => array( 'view' ), |
||
| 344 | 'readonly' => true, |
||
| 345 | ), |
||
| 346 | 'totals_grouped_by' => array( |
||
| 347 | 'description' => __( 'Group type.', 'woocommerce' ), |
||
| 348 | 'type' => 'string', |
||
| 349 | 'context' => array( 'view' ), |
||
| 350 | 'readonly' => true, |
||
| 351 | ), |
||
| 352 | 'totals' => array( |
||
| 353 | 'description' => __( 'Totals.', 'woocommerce' ), |
||
| 354 | 'type' => 'array', |
||
| 355 | 'context' => array( 'view' ), |
||
| 356 | 'readonly' => true, |
||
| 357 | ), |
||
| 358 | ), |
||
| 359 | ); |
||
| 360 | |||
| 361 | return $this->add_additional_fields_schema( $schema ); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get the query params for collections. |
||
| 366 | * |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public function get_collection_params() { |
||
| 395 | } |
||
| 396 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.