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_Reports_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'; |
||
38 | |||
39 | /** |
||
40 | * Register the routes for reports. |
||
41 | */ |
||
42 | View Code Duplication | public function register_routes() { |
|
53 | |||
54 | /** |
||
55 | * Check whether a given request has permission to read reports. |
||
56 | * |
||
57 | * @param WP_REST_Request $request Full details about the request. |
||
58 | * @return WP_Error|boolean |
||
59 | */ |
||
60 | View Code Duplication | public function get_items_permissions_check( $request ) { |
|
67 | |||
68 | /** |
||
69 | * Get all reports. |
||
70 | * |
||
71 | * @param WP_REST_Request $request |
||
72 | * @return array|WP_Error |
||
73 | */ |
||
74 | public function get_items( $request ) { |
||
94 | |||
95 | /** |
||
96 | * Prepare a report object for serialization. |
||
97 | * |
||
98 | * @param stdClass $report Report data. |
||
99 | * @param WP_REST_Request $request Request object. |
||
100 | * @return WP_REST_Response $response Response data. |
||
101 | */ |
||
102 | View Code Duplication | public function prepare_item_for_response( $report, $request ) { |
|
134 | |||
135 | /** |
||
136 | * Get the Report's schema, conforming to JSON Schema. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | public function get_item_schema() { |
||
163 | |||
164 | /** |
||
165 | * Get the query params for collections. |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | public function get_collection_params() { |
||
174 | } |
||
175 |
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.