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 |
||
16 | class WC_Rest_Settings_Controller extends WC_REST_Controller { |
||
17 | |||
18 | /** |
||
19 | * WP REST API namespace/version. |
||
20 | */ |
||
21 | protected $namespace = 'wc/v1'; |
||
22 | |||
23 | /** |
||
24 | * Route base. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $rest_base = 'settings'; |
||
29 | |||
30 | /** |
||
31 | * Register routes. |
||
32 | * @since 2.7.0 |
||
33 | */ |
||
34 | View Code Duplication | public function register_routes() { |
|
44 | |||
45 | /** |
||
46 | * Get all settings groups items. |
||
47 | * |
||
48 | * @since 2.7.0 |
||
49 | * @param WP_REST_Request $request |
||
50 | * @return WP_Error|WP_REST_Response |
||
51 | */ |
||
52 | public function get_items( $request ) { |
||
82 | |||
83 | /** |
||
84 | * Prepare links for the request. |
||
85 | * |
||
86 | * @param string $group_id Group ID. |
||
87 | * @return array Links for the given group. |
||
88 | */ |
||
89 | protected function prepare_links( $group_id ) { |
||
100 | |||
101 | /** |
||
102 | * Prepare a report sales object for serialization. |
||
103 | * |
||
104 | * @param array $item Group object. |
||
105 | * @param WP_REST_Request $request Request object. |
||
106 | * @return WP_REST_Response $response Response data. |
||
107 | */ |
||
108 | public function prepare_item_for_response( $item, $request ) { |
||
119 | |||
120 | /** |
||
121 | * Filters out bad values from the groups array/filter so we |
||
122 | * only return known values via the API. |
||
123 | * |
||
124 | * @since 2.7.0 |
||
125 | * @param array $group |
||
126 | * @return array |
||
127 | */ |
||
128 | public function filter_group( $group ) { |
||
134 | |||
135 | /** |
||
136 | * Callback for allowed keys for each group response. |
||
137 | * |
||
138 | * @since 2.7.0 |
||
139 | * @param string $key Key to check |
||
140 | * @return boolean |
||
141 | */ |
||
142 | public function allowed_group_keys( $key ) { |
||
145 | |||
146 | /** |
||
147 | * Returns default settings for groups. null means the field is required. |
||
148 | * |
||
149 | * @since 2.7.0 |
||
150 | * @return array |
||
151 | */ |
||
152 | protected function group_defaults() { |
||
161 | |||
162 | /** |
||
163 | * Makes sure the current user has access to READ the settings APIs. |
||
164 | * |
||
165 | * @since 2.7.0 |
||
166 | * @param WP_REST_Request $request Full data about the request. |
||
167 | * @return WP_Error|boolean |
||
168 | */ |
||
169 | View Code Duplication | public function get_items_permissions_check( $request ) { |
|
176 | |||
177 | /** |
||
178 | * Get the groups schema, conforming to JSON Schema. |
||
179 | * |
||
180 | * @since 2.7.0 |
||
181 | * @return array |
||
182 | */ |
||
183 | public function get_item_schema() { |
||
229 | } |
||
230 |
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.