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:
Complex classes like CMB2_Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CMB2_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class CMB2_Base { |
||
|
|||
19 | |||
20 | /** |
||
21 | * Current CMB2 instance ID |
||
22 | * @var string |
||
23 | * @since 2.2.3 |
||
24 | */ |
||
25 | protected $cmb_id = ''; |
||
26 | |||
27 | /** |
||
28 | * The object properties name. |
||
29 | * @var string |
||
30 | * @since 2.2.3 |
||
31 | */ |
||
32 | protected $properties_name = 'meta_box'; |
||
33 | |||
34 | /** |
||
35 | * Object ID |
||
36 | * @var mixed |
||
37 | * @since 2.2.3 |
||
38 | */ |
||
39 | protected $object_id = 0; |
||
40 | |||
41 | /** |
||
42 | * Type of object being handled. (e.g., post, user, comment, or term) |
||
43 | * @var string |
||
44 | * @since 2.2.3 |
||
45 | */ |
||
46 | protected $object_type = 'post'; |
||
47 | |||
48 | /** |
||
49 | * Array of key => value data for saving. Likely $_POST data. |
||
50 | * @var array |
||
51 | * @since 2.2.3 |
||
52 | */ |
||
53 | public $data_to_save = array(); |
||
54 | |||
55 | /** |
||
56 | * Array of field param callback results |
||
57 | * @var array |
||
58 | * @since 2.0.0 |
||
59 | */ |
||
60 | protected $callback_results = array(); |
||
61 | |||
62 | /** |
||
63 | * The deprecated_param method deprecated param message signature. |
||
64 | */ |
||
65 | const DEPRECATED_PARAM = 1; |
||
66 | |||
67 | /** |
||
68 | * The deprecated_param method deprecated callback param message signature. |
||
69 | */ |
||
70 | const DEPRECATED_CB_PARAM = 2; |
||
71 | |||
72 | /** |
||
73 | * Get started |
||
74 | * @since 2.2.3 |
||
75 | * @param array $args Object properties array |
||
76 | */ |
||
77 | public function __construct( $args = array() ) { |
||
92 | |||
93 | /** |
||
94 | * Returns the object ID |
||
95 | * @since 2.2.3 |
||
96 | * @param integer $object_id Object ID |
||
97 | * @return integer Object ID |
||
98 | */ |
||
99 | 15 | public function object_id( $object_id = 0 ) { |
|
100 | 15 | if ( $object_id ) { |
|
101 | 10 | $this->object_id = $object_id; |
|
102 | 10 | } |
|
103 | |||
104 | 15 | return $this->object_id; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the object type |
||
109 | * @since 2.2.3 |
||
110 | * @param string $object_type Object Type |
||
111 | * @return string Object type |
||
112 | */ |
||
113 | 15 | public function object_type( $object_type = '' ) { |
|
114 | 15 | if ( $object_type ) { |
|
115 | 10 | $this->object_type = $object_type; |
|
116 | 10 | } |
|
117 | |||
118 | 15 | return $this->object_type; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Get the object type for the current page, based on the $pagenow global. |
||
123 | * @since 2.2.2 |
||
124 | * @return string Page object type name. |
||
125 | */ |
||
126 | View Code Duplication | public function current_object_type() { |
|
144 | |||
145 | /** |
||
146 | * Set object property. |
||
147 | * @since 2.2.2 |
||
148 | * @param string $property Metabox config property to retrieve |
||
149 | * @param mixed $value Value to set if no value found |
||
150 | * @return mixed Metabox config property value or false |
||
151 | */ |
||
152 | public function set_prop( $property, $value ) { |
||
157 | |||
158 | /** |
||
159 | * Get object property and optionally set a fallback |
||
160 | * @since 2.0.0 |
||
161 | * @param string $property Metabox config property to retrieve |
||
162 | * @param mixed $fallback Fallback value to set if no value found |
||
163 | * @return mixed Metabox config property value or false |
||
164 | */ |
||
165 | 48 | public function prop( $property, $fallback = null ) { |
|
172 | |||
173 | /** |
||
174 | * Get default field arguments specific to this CMB2 object. |
||
175 | * @since 2.2.0 |
||
176 | * @param array $field_args Metabox field config array. |
||
177 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
178 | * @return array Array of field arguments. |
||
179 | */ |
||
180 | 5 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
197 | |||
198 | /** |
||
199 | * Get a new field object specific to this CMB2 object. |
||
200 | * @since 2.2.0 |
||
201 | * @param array $field_args Metabox field config array. |
||
202 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
203 | * @return CMB2_Field CMB2_Field object |
||
204 | */ |
||
205 | 5 | protected function get_new_field( $field_args, $field_group = null ) { |
|
208 | |||
209 | /** |
||
210 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
211 | * |
||
212 | * @since 2.0.9 |
||
213 | * |
||
214 | * @return bool Whether this cmb should be shown. |
||
215 | */ |
||
216 | 48 | public function should_show() { |
|
227 | |||
228 | /** |
||
229 | * Displays the results of the param callbacks. |
||
230 | * |
||
231 | * @since 2.0.0 |
||
232 | * @param string $param Field parameter |
||
233 | */ |
||
234 | 85 | public function peform_param_callback( $param ) { |
|
237 | |||
238 | /** |
||
239 | * Store results of the param callbacks for continual access |
||
240 | * @since 2.0.0 |
||
241 | * @param string $param Field parameter |
||
242 | * @return mixed Results of param/param callback |
||
243 | */ |
||
244 | 89 | public function get_param_callback_result( $param ) { |
|
275 | |||
276 | /** |
||
277 | * Handles the property callbacks, and passes this object as property. |
||
278 | * @since 2.2.3 |
||
279 | * @param callable $cb The callback method/function/closure |
||
280 | * @return mixed Return of the callback function. |
||
281 | */ |
||
282 | 54 | protected function do_callback( $cb ) { |
|
285 | |||
286 | /** |
||
287 | * Checks if field has a callback value |
||
288 | * @since 1.0.1 |
||
289 | * @param string $cb Callback string |
||
290 | * @return mixed NULL, false for NO validation, or $cb string if it exists. |
||
291 | */ |
||
292 | 107 | public function maybe_callback( $cb ) { |
|
312 | |||
313 | /** |
||
314 | * Mark a param as deprecated and inform when it has been used. |
||
315 | * |
||
316 | * There is a default WordPress hook deprecated_argument_run that will be called |
||
317 | * that can be used to get the backtrace up to what file and function used the |
||
318 | * deprecated argument. |
||
319 | * |
||
320 | * The current behavior is to trigger a user error if WP_DEBUG is true. |
||
321 | * |
||
322 | * @since 2.2.3 |
||
323 | * |
||
324 | * @param string $function The function that was called. |
||
325 | * @param string $version The version of CMB2 that deprecated the argument used. |
||
326 | * @param string $message Optional. A message regarding the change, or numeric |
||
327 | * key to generate message from additional arguments. |
||
328 | * Default null. |
||
329 | */ |
||
330 | 1 | protected function deprecated_param( $function, $version, $message = null ) { |
|
331 | |||
332 | 1 | if ( is_numeric( $message ) ) { |
|
333 | 1 | $args = func_get_args(); |
|
334 | |||
335 | switch ( $message ) { |
||
336 | |||
337 | 1 | View Code Duplication | case self::DEPRECATED_PARAM: |
338 | $message = sprintf( __( 'The "%s" field parameter has been deprecated in favor of the "%s" parameter.', 'cmb2' ), $args[3], $args[4] ); |
||
339 | break; |
||
340 | |||
341 | 1 | View Code Duplication | case self::DEPRECATED_CB_PARAM: |
342 | 1 | $message = sprintf( __( 'Using the "%s" field parameter as a callback has been deprecated in favor of the "%s" parameter.', 'cmb2' ), $args[3], $args[4] ); |
|
343 | 1 | break; |
|
344 | |||
345 | default: |
||
346 | $message = null; |
||
347 | break; |
||
348 | } |
||
349 | 1 | } |
|
350 | |||
351 | /** |
||
352 | * Fires when a deprecated argument is called. This is a WP core action. |
||
353 | * |
||
354 | * @since 2.2.3 |
||
355 | * |
||
356 | * @param string $function The function that was called. |
||
357 | * @param string $message A message regarding the change. |
||
358 | * @param string $version The version of CMB2 that deprecated the argument used. |
||
359 | */ |
||
360 | 1 | do_action( 'deprecated_argument_run', $function, $message, $version ); |
|
361 | |||
362 | /** |
||
363 | * Filters whether to trigger an error for deprecated arguments. This is a WP core filter. |
||
364 | * |
||
365 | * @since 2.2.3 |
||
366 | * |
||
367 | * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true. |
||
368 | */ |
||
369 | 1 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) { |
|
370 | if ( function_exists( '__' ) ) { |
||
371 | if ( ! is_null( $message ) ) { |
||
372 | trigger_error( sprintf( __( '%1$s was called with a parameter that is <strong>deprecated</strong> since version %2$s! %3$s', 'cmb2' ), $function, $version, $message ) ); |
||
373 | } |
||
374 | else { |
||
375 | trigger_error( sprintf( __( '%1$s was called with a parameter that is <strong>deprecated</strong> since version %2$s with no alternative available.', 'cmb2' ), $function, $version ) ); |
||
376 | } |
||
377 | } else { |
||
378 | if ( ! is_null( $message ) ) { |
||
379 | trigger_error( sprintf( '%1$s was called with a parameter that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) ); |
||
380 | } |
||
381 | else { |
||
382 | trigger_error( sprintf( '%1$s was called with a parameter that is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) ); |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | 1 | } |
|
387 | |||
388 | /** |
||
389 | * Magic getter for our object. |
||
390 | * @param string $field |
||
391 | * @throws Exception Throws an exception if the field is invalid. |
||
392 | * @return mixed |
||
393 | */ |
||
394 | 105 | public function __get( $field ) { |
|
411 | |||
412 | /** |
||
413 | * Allows overloading the object with methods... Whooaaa oooh it's magic, y'knoooow. |
||
414 | * @since 1.0.0 |
||
415 | * @param string $method Non-existent method. |
||
416 | * @param array $arguments All arguments passed to the method |
||
417 | */ |
||
418 | 2 | public function __call( $method, $args ) { |
|
446 | } |
||
447 |
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.