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 |
||
22 | abstract class CMB2_REST_Controller extends WP_REST_Controller { |
||
23 | |||
24 | /** |
||
25 | * The namespace of this controller's route. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $namespace = CMB2_REST::NAME_SPACE; |
||
30 | |||
31 | /** |
||
32 | * The base of this controller's route. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $rest_base; |
||
37 | |||
38 | /** |
||
39 | * The current request object |
||
40 | * @var WP_REST_Request $request |
||
41 | * @since 2.2.4 |
||
42 | */ |
||
43 | public $request; |
||
44 | |||
45 | /** |
||
46 | * The current server object |
||
47 | * @var WP_REST_Server $server |
||
48 | * @since 2.2.4 |
||
49 | */ |
||
50 | public $server; |
||
51 | |||
52 | /** |
||
53 | * Box object id |
||
54 | * @var mixed |
||
55 | * @since 2.2.4 |
||
56 | */ |
||
57 | public $object_id = null; |
||
58 | |||
59 | /** |
||
60 | * Box object type |
||
61 | * @var string |
||
62 | * @since 2.2.4 |
||
63 | */ |
||
64 | public $object_type = ''; |
||
65 | |||
66 | /** |
||
67 | * CMB2 Instance |
||
68 | * |
||
69 | * @var CMB2_REST |
||
70 | */ |
||
71 | protected $rest_box; |
||
72 | |||
73 | /** |
||
74 | * The initial route |
||
75 | * @var string |
||
76 | * @since 2.2.4 |
||
77 | */ |
||
78 | protected static $route = ''; |
||
79 | |||
80 | /** |
||
81 | * Defines which endpoint the initial request is. |
||
82 | * @var string $request_type |
||
83 | * @since 2.2.4 |
||
84 | */ |
||
85 | protected static $request_type = ''; |
||
86 | |||
87 | /** |
||
88 | * Constructor |
||
89 | * @since 2.2.4 |
||
90 | */ |
||
91 | 1 | public function __construct( WP_REST_Server $wp_rest_server ) { |
|
94 | |||
95 | /** |
||
96 | * Check if a given request has access to get items. |
||
97 | * |
||
98 | * @since 2.2.4 |
||
99 | * |
||
100 | * @param WP_REST_Request $request Full data about the request. |
||
101 | * @return WP_Error|boolean |
||
102 | */ |
||
103 | 2 | public function get_items_permissions_check( $request ) { |
|
119 | |||
120 | /** |
||
121 | * Check if a given request has access to a field or box. |
||
122 | * By default, no special permissions needed, but filtering return value. |
||
123 | * |
||
124 | * @since 2.2.4 |
||
125 | * |
||
126 | * @param WP_REST_Request $request Full details about the request. |
||
127 | * @return WP_Error|boolean |
||
128 | */ |
||
129 | 6 | public function get_item_permissions_check( $request ) { |
|
145 | |||
146 | /** |
||
147 | * Check if a given request has access to update a field value. |
||
148 | * By default, requires 'edit_others_posts' capability, but filtering return value. |
||
149 | * |
||
150 | * @since 2.2.4 |
||
151 | * |
||
152 | * @param WP_REST_Request $request Full details about the request. |
||
153 | * @return WP_Error|boolean |
||
154 | */ |
||
155 | 5 | public function update_field_value_permissions_check( $request ) { |
|
171 | |||
172 | /** |
||
173 | * Check if a given request has access to delete a field value. |
||
174 | * By default, requires 'delete_others_posts' capability, but filtering return value. |
||
175 | * |
||
176 | * @since 2.2.4 |
||
177 | * |
||
178 | * @param WP_REST_Request $request Full details about the request. |
||
179 | * @return WP_Error|boolean |
||
180 | */ |
||
181 | 3 | public function delete_field_value_permissions_check( $request ) { |
|
197 | |||
198 | /** |
||
199 | * Check if a CMB object callback property exists, and if it does, |
||
200 | * hook it to the permissions filter. |
||
201 | * |
||
202 | * @since 2.2.4 |
||
203 | * |
||
204 | * @param string $to_check The callback property to check. |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | 13 | public function maybe_hook_callback( $to_check ) { |
|
220 | |||
221 | /** |
||
222 | * Prepare a CMB2 object for serialization |
||
223 | * |
||
224 | * @since 2.2.4 |
||
225 | * |
||
226 | * @param mixed $data |
||
227 | * @return array $data |
||
228 | */ |
||
229 | 7 | public function prepare_item( $post ) { |
|
232 | |||
233 | /** |
||
234 | * Output buffers a callback and returns the results. |
||
235 | * |
||
236 | * @since 2.2.4 |
||
237 | * |
||
238 | * @param mixed $cb Callable function/method. |
||
239 | * @return mixed Results of output buffer after calling function/method. |
||
240 | */ |
||
241 | 5 | public function get_cb_results( $cb ) { |
|
249 | |||
250 | /** |
||
251 | * Prepare the CMB2 item for the REST response. |
||
252 | * |
||
253 | * @since 2.2.4 |
||
254 | * |
||
255 | * @param mixed $item WordPress representation of the item. |
||
256 | * @param WP_REST_Request $request Request object. |
||
257 | * @return WP_REST_Response $response |
||
258 | */ |
||
259 | 7 | public function prepare_item_for_response( $data, $request = null ) { |
|
273 | |||
274 | /** |
||
275 | * Initiates the request property and the rest_box property if box is readable. |
||
276 | * |
||
277 | * @since 2.2.4 |
||
278 | * |
||
279 | * @param WP_REST_Request $request Request object. |
||
280 | * @param string $request_type A description of the type of request being made. |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | 7 | View Code Duplication | protected function initiate_rest_read_box( $request, $request_type ) { |
291 | |||
292 | /** |
||
293 | * Initiates the request property and the rest_box property if box is writeable. |
||
294 | * |
||
295 | * @since 2.2.4 |
||
296 | * |
||
297 | * @param WP_REST_Request $request Request object. |
||
298 | * @param string $request_type A description of the type of request being made. |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | View Code Duplication | protected function initiate_rest_edit_box( $request, $request_type ) { |
|
309 | |||
310 | /** |
||
311 | * Initiates the request property and the rest_box property. |
||
312 | * |
||
313 | * @since 2.2.4 |
||
314 | * |
||
315 | * @param WP_REST_Request $request Request object. |
||
316 | * @param string $request_type A description of the type of request being made. |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | 7 | protected function initiate_rest_box( $request, $request_type ) { |
|
340 | |||
341 | /** |
||
342 | * Initiates the request property and sets up the initial static properties. |
||
343 | * |
||
344 | * @since 2.2.4 |
||
345 | * |
||
346 | * @param WP_REST_Request $request Request object. |
||
347 | * @param string $request_type A description of the type of request being made. |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | 13 | public function initiate_request( $request, $request_type ) { |
|
366 | |||
367 | /** |
||
368 | * Useful when getting `_embed`-ed items |
||
369 | * |
||
370 | * @since 2.2.4 |
||
371 | * |
||
372 | * @return string Initial requested type. |
||
373 | */ |
||
374 | public static function get_intial_request_type() { |
||
377 | |||
378 | /** |
||
379 | * Useful when getting `_embed`-ed items |
||
380 | * |
||
381 | * @since 2.2.4 |
||
382 | * |
||
383 | * @return string Initial requested route. |
||
384 | */ |
||
385 | 5 | public static function get_intial_route() { |
|
388 | |||
389 | /** |
||
390 | * Get CMB2 fields schema, conforming to JSON Schema |
||
391 | * |
||
392 | * @since 2.2.4 |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | 8 | public function get_item_schema() { |
|
422 | |||
423 | /** |
||
424 | * Return an array of contextual links for endpoint/object |
||
425 | * @link http://v2.wp-api.org/extending/linking/ |
||
426 | * @link http://www.iana.org/assignments/link-relations/link-relations.xhtml |
||
427 | * |
||
428 | * @since 2.2.4 |
||
429 | * |
||
430 | * @param mixed $object Object to build links from. |
||
431 | * |
||
432 | * @return array Array of links |
||
433 | */ |
||
434 | abstract protected function prepare_links( $object ); |
||
435 | |||
436 | /** |
||
437 | * Get whitelisted query strings from URL for appending to link URLS. |
||
438 | * |
||
439 | * @since 2.2.4 |
||
440 | * |
||
441 | * @return string URL query stringl |
||
442 | */ |
||
443 | 7 | public function get_query_string() { |
|
465 | |||
466 | } |
||
467 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.