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 | * |
||
41 | * @var WP_REST_Request $request |
||
42 | * @since 2.2.3 |
||
43 | */ |
||
44 | public $request; |
||
45 | |||
46 | /** |
||
47 | * The current server object |
||
48 | * |
||
49 | * @var WP_REST_Server $server |
||
50 | * @since 2.2.3 |
||
51 | */ |
||
52 | public $server; |
||
53 | |||
54 | /** |
||
55 | * Box object id |
||
56 | * |
||
57 | * @var mixed |
||
58 | * @since 2.2.3 |
||
59 | */ |
||
60 | public $object_id = null; |
||
61 | |||
62 | /** |
||
63 | * Box object type |
||
64 | * |
||
65 | * @var string |
||
66 | * @since 2.2.3 |
||
67 | */ |
||
68 | public $object_type = ''; |
||
69 | |||
70 | /** |
||
71 | * CMB2 Instance |
||
72 | * |
||
73 | * @var CMB2_REST |
||
74 | */ |
||
75 | protected $rest_box; |
||
76 | |||
77 | /** |
||
78 | * CMB2_Field Instance |
||
79 | * |
||
80 | * @var CMB2_Field |
||
81 | */ |
||
82 | protected $field; |
||
83 | |||
84 | /** |
||
85 | * The initial route |
||
86 | * |
||
87 | * @var string |
||
88 | * @since 2.2.3 |
||
89 | */ |
||
90 | protected static $route = ''; |
||
91 | |||
92 | /** |
||
93 | * Defines which endpoint the initial request is. |
||
94 | * |
||
95 | * @var string $request_type |
||
96 | * @since 2.2.3 |
||
97 | */ |
||
98 | protected static $request_type = ''; |
||
99 | |||
100 | /** |
||
101 | * Constructor |
||
102 | * |
||
103 | * @since 2.2.3 |
||
104 | */ |
||
105 | public function __construct( WP_REST_Server $wp_rest_server ) { |
||
108 | |||
109 | /** |
||
110 | * A wrapper for `apply_filters` which checks for box/field properties to hook to the filter. |
||
111 | * |
||
112 | * Checks if a CMB object callback property exists, and if it does, |
||
113 | * hook it to the permissions filter. |
||
114 | * |
||
115 | * @since 2.2.3 |
||
116 | * |
||
117 | * @param string $filter The name of the filter to apply. |
||
118 | * @param bool $default_access The default access for this request. |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function maybe_hook_callback_and_apply_filters( $filter, $default_access ) { |
||
143 | |||
144 | /** |
||
145 | * Checks if the CMB2 box has any registered callback parameters for the given filter. |
||
146 | * |
||
147 | * The registered handlers will have a property name which matches the filter, except: |
||
148 | * - The 'cmb2_api' prefix will be removed |
||
149 | * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). |
||
150 | * |
||
151 | * @since 2.2.3 |
||
152 | * |
||
153 | * @param string $filter The filter name. |
||
154 | * @param bool $default_val The default filter value. |
||
155 | * |
||
156 | * @return bool The possibly-modified filter value (if the '*_cb' param is non-callable). |
||
157 | */ |
||
158 | public function maybe_hook_registered_callback( $filter, $default_val ) { |
||
171 | |||
172 | /** |
||
173 | * Unhooks any CMB2 box registered callback parameters for the given filter. |
||
174 | * |
||
175 | * @since 2.2.3 |
||
176 | * |
||
177 | * @param string $filter The filter name. |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | public function maybe_unhook_registered_callback( $filter ) { |
||
189 | |||
190 | /** |
||
191 | * Prepare a CMB2 object for serialization |
||
192 | * |
||
193 | * @since 2.2.3 |
||
194 | * |
||
195 | * @param mixed $data |
||
196 | * @return array $data |
||
197 | */ |
||
198 | public function prepare_item( $data ) { |
||
201 | |||
202 | /** |
||
203 | * Output buffers a callback and returns the results. |
||
204 | * |
||
205 | * @since 2.2.3 |
||
206 | * |
||
207 | * @param mixed $cb Callable function/method. |
||
208 | * @return mixed Results of output buffer after calling function/method. |
||
209 | */ |
||
210 | public function get_cb_results( $cb ) { |
||
218 | |||
219 | /** |
||
220 | * Prepare the CMB2 item for the REST response. |
||
221 | * |
||
222 | * @since 2.2.3 |
||
223 | * |
||
224 | * @param mixed $item WordPress representation of the item. |
||
225 | * @param WP_REST_Request $request Request object. |
||
226 | * @return WP_REST_Response $response |
||
227 | */ |
||
228 | public function prepare_item_for_response( $data, $request = null ) { |
||
242 | |||
243 | /** |
||
244 | * Initiates the request property and the rest_box property if box is readable. |
||
245 | * |
||
246 | * @since 2.2.3 |
||
247 | * |
||
248 | * @param WP_REST_Request $request Request object. |
||
249 | * @param string $request_type A description of the type of request being made. |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | View Code Duplication | protected function initiate_rest_read_box( $request, $request_type ) { |
|
262 | |||
263 | /** |
||
264 | * Initiates the request property and the rest_box property if box is writeable. |
||
265 | * |
||
266 | * @since 2.2.3 |
||
267 | * |
||
268 | * @param WP_REST_Request $request Request object. |
||
269 | * @param string $request_type A description of the type of request being made. |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | View Code Duplication | protected function initiate_rest_edit_box( $request, $request_type ) { |
|
282 | |||
283 | /** |
||
284 | * Initiates the request property and the rest_box property. |
||
285 | * |
||
286 | * @since 2.2.3 |
||
287 | * |
||
288 | * @param WP_REST_Request $request Request object. |
||
289 | * @param string $request_type A description of the type of request being made. |
||
290 | * |
||
291 | * @return void |
||
292 | */ |
||
293 | protected function initiate_rest_box( $request, $request_type ) { |
||
315 | |||
316 | /** |
||
317 | * Initiates the request property and sets up the initial static properties. |
||
318 | * |
||
319 | * @since 2.2.3 |
||
320 | * |
||
321 | * @param WP_REST_Request $request Request object. |
||
322 | * @param string $request_type A description of the type of request being made. |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | public function initiate_request( $request, $request_type ) { |
||
341 | |||
342 | /** |
||
343 | * Useful when getting `_embed`-ed items |
||
344 | * |
||
345 | * @since 2.2.3 |
||
346 | * |
||
347 | * @return string Initial requested type. |
||
348 | */ |
||
349 | public static function get_intial_request_type() { |
||
352 | |||
353 | /** |
||
354 | * Useful when getting `_embed`-ed items |
||
355 | * |
||
356 | * @since 2.2.3 |
||
357 | * |
||
358 | * @return string Initial requested route. |
||
359 | */ |
||
360 | public static function get_intial_route() { |
||
363 | |||
364 | /** |
||
365 | * Get CMB2 fields schema, conforming to JSON Schema |
||
366 | * |
||
367 | * @since 2.2.3 |
||
368 | * |
||
369 | * @return array |
||
370 | */ |
||
371 | public function get_item_schema() { |
||
403 | |||
404 | /** |
||
405 | * Return an array of contextual links for endpoint/object |
||
406 | * |
||
407 | * @link http://v2.wp-api.org/extending/linking/ |
||
408 | * @link http://www.iana.org/assignments/link-relations/link-relations.xhtml |
||
409 | * |
||
410 | * @since 2.2.3 |
||
411 | * |
||
412 | * @param mixed $object Object to build links from. |
||
413 | * |
||
414 | * @return array Array of links |
||
415 | */ |
||
416 | abstract protected function prepare_links( $object ); |
||
417 | |||
418 | /** |
||
419 | * Get whitelisted query strings from URL for appending to link URLS. |
||
420 | * |
||
421 | * @since 2.2.3 |
||
422 | * |
||
423 | * @return string URL query stringl |
||
424 | */ |
||
425 | public function get_query_string() { |
||
447 | |||
448 | } |
||
449 |
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.