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 |
||
19 | class WC_API { |
||
20 | |||
21 | /** |
||
22 | * This is the major version for the REST API and takes |
||
23 | * first-order position in endpoint URLs. |
||
24 | * |
||
25 | * @deprecated 2.6.0 |
||
26 | * @var string |
||
27 | */ |
||
28 | const VERSION = '3.1.0'; |
||
29 | |||
30 | /** |
||
31 | * The REST API server. |
||
32 | * |
||
33 | * @deprecated 2.6.0 |
||
34 | * @var WC_API_Server |
||
35 | */ |
||
36 | public $server; |
||
37 | |||
38 | /** |
||
39 | * REST API authentication class instance. |
||
40 | * |
||
41 | * @deprecated 2.6.0 |
||
42 | * @var WC_API_Authentication |
||
43 | */ |
||
44 | public $authentication; |
||
45 | |||
46 | /** |
||
47 | * Setup class. |
||
48 | * |
||
49 | * @since 2.0 |
||
50 | * @return WC_API |
||
|
|||
51 | */ |
||
52 | public function __construct() { |
||
71 | |||
72 | /** |
||
73 | * Add new query vars. |
||
74 | * |
||
75 | * @since 2.0 |
||
76 | * @param array $vars |
||
77 | * @return string[] |
||
78 | */ |
||
79 | public function add_query_vars( $vars ) { |
||
86 | |||
87 | /** |
||
88 | * Add new endpoints. |
||
89 | * |
||
90 | * @since 2.0 |
||
91 | */ |
||
92 | public static function add_endpoint() { |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Handle REST API requests. |
||
105 | * |
||
106 | * @since 2.2 |
||
107 | * @deprecated 2.6.0 |
||
108 | */ |
||
109 | public function handle_rest_api_requests() { |
||
146 | |||
147 | /** |
||
148 | * Include required files for REST API request. |
||
149 | * |
||
150 | * @since 2.1 |
||
151 | * @deprecated 2.6.0 |
||
152 | */ |
||
153 | public function includes() { |
||
177 | |||
178 | /** |
||
179 | * Register available API resources. |
||
180 | * |
||
181 | * @since 2.1 |
||
182 | * @deprecated 2.6.0 |
||
183 | * @param WC_API_Server $server the REST server |
||
184 | */ |
||
185 | public function register_resources( $server ) { |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Handle legacy v1 REST API requests. |
||
207 | * |
||
208 | * @since 2.2 |
||
209 | * @deprecated 2.6.0 |
||
210 | */ |
||
211 | View Code Duplication | private function handle_v1_rest_api_request() { |
|
252 | |||
253 | /** |
||
254 | * Handle legacy v2 REST API requests. |
||
255 | * |
||
256 | * @since 2.4 |
||
257 | * @deprecated 2.6.0 |
||
258 | */ |
||
259 | View Code Duplication | private function handle_v2_rest_api_request() { |
|
300 | |||
301 | /** |
||
302 | * API request - Trigger any API requests. |
||
303 | * |
||
304 | * @since 2.0 |
||
305 | * @version 2.4 |
||
306 | */ |
||
307 | public function handle_api_requests() { |
||
340 | |||
341 | /** |
||
342 | * Init WP REST API. |
||
343 | * |
||
344 | * @since 2.6.0 |
||
345 | */ |
||
346 | private function rest_api_init() { |
||
359 | |||
360 | /** |
||
361 | * Include REST API classes. |
||
362 | * |
||
363 | * @since 2.6.0 |
||
364 | */ |
||
365 | private function rest_api_includes() { |
||
366 | // Exception handler. |
||
367 | include_once( 'api/class-wc-rest-exception.php' ); |
||
368 | |||
369 | // Authentication. |
||
370 | include_once( 'api/class-wc-rest-authentication.php' ); |
||
371 | |||
372 | // WP-API classes and functions. |
||
373 | include_once( 'vendor/wp-rest-functions.php' ); |
||
374 | if ( ! class_exists( 'WP_REST_Controller' ) ) { |
||
375 | include_once( 'vendor/class-wp-rest-controller.php' ); |
||
376 | } |
||
377 | |||
378 | // Abstract controllers. |
||
379 | include_once( 'abstracts/abstract-wc-rest-controller.php' ); |
||
380 | include_once( 'abstracts/abstract-wc-rest-posts-controller.php' ); |
||
381 | include_once( 'abstracts/abstract-wc-rest-terms-controller.php' ); |
||
382 | |||
383 | // REST API controllers. |
||
384 | include_once( 'api/class-wc-rest-coupons-controller.php' ); |
||
385 | include_once( 'api/class-wc-rest-customer-downloads-controller.php' ); |
||
386 | include_once( 'api/class-wc-rest-customers-controller.php' ); |
||
387 | include_once( 'api/class-wc-rest-order-notes-controller.php' ); |
||
388 | include_once( 'api/class-wc-rest-order-refunds-controller.php' ); |
||
389 | include_once( 'api/class-wc-rest-orders-controller.php' ); |
||
390 | include_once( 'api/class-wc-rest-product-attribute-terms-controller.php' ); |
||
391 | include_once( 'api/class-wc-rest-product-attributes-controller.php' ); |
||
392 | include_once( 'api/class-wc-rest-product-categories-controller.php' ); |
||
393 | include_once( 'api/class-wc-rest-product-reviews-controller.php' ); |
||
394 | include_once( 'api/class-wc-rest-product-shipping-classes-controller.php' ); |
||
395 | include_once( 'api/class-wc-rest-product-tags-controller.php' ); |
||
396 | include_once( 'api/class-wc-rest-products-controller.php' ); |
||
397 | include_once( 'api/class-wc-rest-report-sales-controller.php' ); |
||
398 | include_once( 'api/class-wc-rest-report-top-sellers-controller.php' ); |
||
399 | include_once( 'api/class-wc-rest-reports-controller.php' ); |
||
400 | include_once( 'api/class-wc-rest-tax-classes-controller.php' ); |
||
401 | include_once( 'api/class-wc-rest-taxes-controller.php' ); |
||
402 | include_once( 'api/class-wc-rest-webhook-deliveries.php' ); |
||
403 | include_once( 'api/class-wc-rest-webhooks-controller.php' ); |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Register REST API routes. |
||
408 | * |
||
409 | * @since 2.6.0 |
||
410 | */ |
||
411 | public function register_rest_routes() { |
||
440 | } |
||
441 | |||
445 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.