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 |
||
17 | class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The base of this controller's route. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $rest_base = 'boxes'; |
||
25 | |||
26 | /** |
||
27 | * The combined $namespace and $rest_base for these routes. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $namespace_base = ''; |
||
32 | |||
33 | /** |
||
34 | * Constructor |
||
35 | * @since 2.2.4 |
||
36 | */ |
||
37 | public function __construct( WP_REST_Server $wp_rest_server ) { |
||
41 | |||
42 | /** |
||
43 | * Register the routes for the objects of the controller. |
||
44 | * |
||
45 | * @since 2.2.4 |
||
46 | */ |
||
47 | View Code Duplication | public function register_routes() { |
|
69 | |||
70 | /** |
||
71 | * Get all public CMB2 boxes. |
||
72 | * |
||
73 | * @since 2.2.4 |
||
74 | * |
||
75 | * @param WP_REST_Request $request Full data about the request. |
||
76 | * @return WP_Error|WP_REST_Response |
||
77 | */ |
||
78 | public function get_items( $request ) { |
||
96 | |||
97 | /** |
||
98 | * Get one CMB2 box from the collection. |
||
99 | * |
||
100 | * @since 2.2.4 |
||
101 | * |
||
102 | * @param WP_REST_Request $request Full data about the request. |
||
103 | * @return WP_Error|WP_REST_Response |
||
104 | */ |
||
105 | public function get_item( $request ) { |
||
114 | |||
115 | /** |
||
116 | * Get a CMB2 box prepared for REST |
||
117 | * |
||
118 | * @since 2.2.4 |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | public function get_rest_box() { |
||
153 | |||
154 | /** |
||
155 | * Return an array of contextual links for box/boxes. |
||
156 | * |
||
157 | * @since 2.2.4 |
||
158 | * |
||
159 | * @param CMB2_REST $cmb CMB2_REST object to build links from. |
||
160 | * |
||
161 | * @return array Array of links |
||
162 | */ |
||
163 | protected function prepare_links( $cmb ) { |
||
183 | |||
184 | } |
||
185 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.