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 |
||
11 | abstract class CMB2_Base { |
||
|
|||
12 | |||
13 | /** |
||
14 | * Current CMB2 instance ID |
||
15 | * @var string |
||
16 | * @since 2.2.3 |
||
17 | */ |
||
18 | protected $cmb_id = ''; |
||
19 | |||
20 | /** |
||
21 | * The deprecated object properties name. |
||
22 | * @var string |
||
23 | * @since 2.2.3 |
||
24 | */ |
||
25 | protected $properties_name = 'meta_box'; |
||
26 | |||
27 | /** |
||
28 | * Object ID |
||
29 | * @var mixed |
||
30 | * @since 2.2.3 |
||
31 | */ |
||
32 | protected $object_id = 0; |
||
33 | |||
34 | /** |
||
35 | * Type of object being handled. (e.g., post, user, comment, or term) |
||
36 | * @var string |
||
37 | * @since 2.2.3 |
||
38 | */ |
||
39 | protected $object_type = 'post'; |
||
40 | |||
41 | /** |
||
42 | * Array of key => value data for saving. Likely $_POST data. |
||
43 | * @var array |
||
44 | * @since 2.2.3 |
||
45 | */ |
||
46 | public $data_to_save = array(); |
||
47 | |||
48 | /** |
||
49 | * Array of field param callback results |
||
50 | * @var array |
||
51 | * @since 2.0.0 |
||
52 | */ |
||
53 | protected $callback_results = array(); |
||
54 | |||
55 | /** |
||
56 | * Get started |
||
57 | * @since 2.2.3 |
||
58 | * @param array $args Object properties array |
||
59 | */ |
||
60 | public function __construct( $args = array() ) { |
||
75 | |||
76 | /** |
||
77 | * Returns the object ID |
||
78 | * @since 2.2.3 |
||
79 | * @param integer $object_id Object ID |
||
80 | * @return integer Object ID |
||
81 | */ |
||
82 | 5 | public function object_id( $object_id = 0 ) { |
|
89 | |||
90 | /** |
||
91 | * Returns the object type |
||
92 | * @since 2.2.3 |
||
93 | * @param string $object_type Object Type |
||
94 | * @return string Object type |
||
95 | */ |
||
96 | 5 | public function object_type( $object_type = '' ) { |
|
103 | |||
104 | /** |
||
105 | * Get the object type for the current page, based on the $pagenow global. |
||
106 | * @since 2.2.2 |
||
107 | * @return string Page object type name. |
||
108 | */ |
||
109 | View Code Duplication | public function current_object_type() { |
|
127 | |||
128 | /** |
||
129 | * Set object property. |
||
130 | * @since 2.2.2 |
||
131 | * @param string $property Metabox config property to retrieve |
||
132 | * @param mixed $value Value to set if no value found |
||
133 | * @return mixed Metabox config property value or false |
||
134 | */ |
||
135 | public function set_prop( $property, $value ) { |
||
140 | |||
141 | /** |
||
142 | * Get object property and optionally set a fallback |
||
143 | * @since 2.0.0 |
||
144 | * @param string $property Metabox config property to retrieve |
||
145 | * @param mixed $fallback Fallback value to set if no value found |
||
146 | * @return mixed Metabox config property value or false |
||
147 | */ |
||
148 | 43 | public function prop( $property, $fallback = null ) { |
|
155 | |||
156 | /** |
||
157 | * Get default field arguments specific to this CMB2 object. |
||
158 | * @since 2.2.0 |
||
159 | * @param array $field_args Metabox field config array. |
||
160 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
161 | * @return array Array of field arguments. |
||
162 | */ |
||
163 | 5 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
180 | |||
181 | /** |
||
182 | * Get a new field object specific to this CMB2 object. |
||
183 | * @since 2.2.0 |
||
184 | * @param array $field_args Metabox field config array. |
||
185 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
186 | * @return CMB2_Field CMB2_Field object |
||
187 | */ |
||
188 | 5 | protected function get_new_field( $field_args, $field_group = null ) { |
|
191 | |||
192 | /** |
||
193 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
194 | * |
||
195 | * @since 2.0.9 |
||
196 | * |
||
197 | * @return bool Whether this cmb should be shown. |
||
198 | */ |
||
199 | 43 | public function should_show() { |
|
210 | |||
211 | /** |
||
212 | * Displays the results of the param callbacks. |
||
213 | * |
||
214 | * @since 2.0.0 |
||
215 | * @param string $param Field parameter |
||
216 | */ |
||
217 | 80 | public function peform_param_callback( $param ) { |
|
220 | |||
221 | /** |
||
222 | * Store results of the param callbacks for continual access |
||
223 | * @since 2.0.0 |
||
224 | * @param string $param Field parameter |
||
225 | * @return mixed Results of param/param callback |
||
226 | */ |
||
227 | 85 | public function get_param_callback_result( $param ) { |
|
258 | |||
259 | /** |
||
260 | * Handles the property callbacks, and passes this object as property. |
||
261 | * @since 2.2.3 |
||
262 | * @param callable $cb The callback method/function/closure |
||
263 | * @return mixed Return of the callback function. |
||
264 | */ |
||
265 | 49 | protected function do_callback( $cb ) { |
|
268 | |||
269 | /** |
||
270 | * Checks if field has a callback value |
||
271 | * @since 1.0.1 |
||
272 | * @param string $cb Callback string |
||
273 | * @return mixed NULL, false for NO validation, or $cb string if it exists. |
||
274 | */ |
||
275 | 96 | public function maybe_callback( $cb ) { |
|
295 | |||
296 | /** |
||
297 | * Magic getter for our object. |
||
298 | * @param string $field |
||
299 | * @throws Exception Throws an exception if the field is invalid. |
||
300 | * @return mixed |
||
301 | */ |
||
302 | 63 | public function __get( $field ) { |
|
319 | |||
320 | } |
||
321 |
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.