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 XoopsObject 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 XoopsObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | abstract class XoopsObject implements \ArrayAccess |
||
51 | { |
||
52 | /** |
||
53 | * holds all variables(properties) of an object |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | public $vars = array(); |
||
58 | |||
59 | /** |
||
60 | * variables cleaned for store in DB |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | public $cleanVars = array(); |
||
65 | |||
66 | /** |
||
67 | * is it a newly created object? |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | private $isNew = false; |
||
72 | |||
73 | /** |
||
74 | * has any of the values been modified? |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | private $isDirty = false; |
||
79 | |||
80 | /** |
||
81 | * errors |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | private $errors = array(); |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | public $plugin_path; |
||
91 | |||
92 | /** |
||
93 | * used for new/clone objects |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | 13 | public function setNew() |
|
101 | |||
102 | /** |
||
103 | * clear new flag |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | 8 | public function unsetNew() |
|
111 | |||
112 | /** |
||
113 | * check new flag |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 12 | public function isNew() |
|
121 | |||
122 | /** |
||
123 | * mark modified objects as dirty |
||
124 | * |
||
125 | * used for modified objects only |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 29 | public function setDirty() |
|
133 | |||
134 | /** |
||
135 | * cleaar dirty flag |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | 14 | public function unsetDirty() |
|
143 | |||
144 | /** |
||
145 | * check dirty flag |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | 14 | public function isDirty() |
|
153 | |||
154 | /** |
||
155 | * initialize variables for the object |
||
156 | * |
||
157 | * @param string $key key |
||
158 | * @param int $data_type set to one of Dtype::TYPE_XXX constants (set to Dtype::TYPE_OTHER |
||
159 | * if no data type checking nor text sanitizing is required) |
||
160 | * @param mixed $value value |
||
161 | * @param bool $required require html form input? |
||
162 | * @param mixed $maxlength for Dtype::TYPE_TEXT_BOX type only |
||
163 | * @param string $options does this data have any select options? |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | 273 | public function initVar($key, $data_type, $value = null, $required = false, $maxlength = null, $options = '') |
|
178 | |||
179 | /** |
||
180 | * assign a value to a variable |
||
181 | * |
||
182 | * @param string $key name of the variable to assign |
||
183 | * @param mixed $value value to assign |
||
184 | * |
||
185 | * @return void |
||
186 | */ |
||
187 | 49 | public function assignVar($key, $value) |
|
193 | |||
194 | /** |
||
195 | * assign values to multiple variables in a batch |
||
196 | * |
||
197 | * @param array $var_arr associative array of values to assign |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | 40 | public function assignVars($var_arr) |
|
209 | |||
210 | /** |
||
211 | * assign a value to a variable |
||
212 | * |
||
213 | * @param string $key name of the variable to assign |
||
214 | * @param mixed $value value to assign |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | 27 | public function setVar($key, $value) |
|
226 | |||
227 | /** |
||
228 | * assign values to multiple variables in a batch |
||
229 | * |
||
230 | * @param array $var_arr associative array of values to assign |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | 1 | public function setVars($var_arr) |
|
242 | |||
243 | /** |
||
244 | * unset variable(s) for the object |
||
245 | * |
||
246 | * @param mixed $var variable(s) |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 1 | public function destroyVars($var) |
|
264 | |||
265 | /** |
||
266 | * Assign values to multiple variables in a batch |
||
267 | * |
||
268 | * Meant for a CGI context: |
||
269 | * - prefixed CGI args are considered save |
||
270 | * - avoids polluting of namespace with CGI args |
||
271 | * |
||
272 | * @param mixed $var_arr associative array of values to assign |
||
273 | * @param string $pref prefix (only keys starting with the prefix will be set) |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | 1 | public function setFormVars($var_arr = null, $pref = 'xo_') |
|
288 | |||
289 | /** |
||
290 | * returns all variables for the object |
||
291 | * |
||
292 | * @return array associative array of key->value pairs |
||
293 | */ |
||
294 | 26 | public function getVars() |
|
298 | |||
299 | /** |
||
300 | * Returns the values of the specified variables |
||
301 | * |
||
302 | * @param mixed $keys An array containing the names of the keys to retrieve, or null to get all of them |
||
303 | * @param string $format Format to use (see getVar) |
||
304 | * @param int $maxDepth Maximum level of recursion to use if some vars are objects themselves |
||
305 | * |
||
306 | * @return array associative array of key->value pairs |
||
307 | */ |
||
308 | 3 | public function getValues($keys = null, $format = Dtype::FORMAT_SHOW, $maxDepth = 1) |
|
331 | |||
332 | /** |
||
333 | * returns a specific variable for the object in a proper format |
||
334 | * |
||
335 | * @param string $key key of the object's variable to be returned |
||
336 | * @param string $format format to use for the output |
||
337 | * |
||
338 | * @return mixed formatted value of the variable |
||
339 | */ |
||
340 | 181 | public function getVar($key, $format = Dtype::FORMAT_SHOW) |
|
349 | |||
350 | /** |
||
351 | * clean values of all variables of the object for storage. |
||
352 | * |
||
353 | * @return bool true if successful |
||
354 | */ |
||
355 | 3 | public function cleanVars() |
|
373 | |||
374 | /** |
||
375 | * create a clone(copy) of the current object |
||
376 | * |
||
377 | * @return object clone |
||
378 | */ |
||
379 | 1 | public function xoopsClone() |
|
384 | |||
385 | /** |
||
386 | * Adjust a newly cloned object |
||
387 | */ |
||
388 | 1 | public function __clone() |
|
393 | |||
394 | /** |
||
395 | * add an error |
||
396 | * |
||
397 | * @param string $err_str to add |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | 2 | public function setErrors($err_str) |
|
409 | |||
410 | /** |
||
411 | * return the errors for this object as an array |
||
412 | * |
||
413 | * @return array an array of errors |
||
414 | */ |
||
415 | 14 | public function getErrors() |
|
419 | |||
420 | /** |
||
421 | * return the errors for this object as html |
||
422 | * |
||
423 | * @return string html listing the errors |
||
424 | * @todo remove hardcoded HTML strings |
||
425 | */ |
||
426 | 1 | public function getHtmlErrors() |
|
438 | |||
439 | /** |
||
440 | * toArray |
||
441 | * |
||
442 | * @deprecated |
||
443 | * @return array |
||
444 | */ |
||
445 | 1 | public function toArray() |
|
449 | |||
450 | /** |
||
451 | * ArrayAccess methods |
||
452 | */ |
||
453 | |||
454 | /** |
||
455 | * offsetExists |
||
456 | * |
||
457 | * @param mixed $offset array key |
||
458 | * |
||
459 | * @return bool true if offset exists |
||
460 | */ |
||
461 | 2 | public function offsetExists($offset) |
|
465 | |||
466 | /** |
||
467 | * offsetGet |
||
468 | * |
||
469 | * @param mixed $offset array key |
||
470 | * |
||
471 | * @return mixed value |
||
472 | */ |
||
473 | 2 | public function offsetGet($offset) |
|
477 | |||
478 | /** |
||
479 | * offsetSet |
||
480 | * |
||
481 | * @param mixed $offset array key |
||
482 | * @param mixed $value |
||
483 | * |
||
484 | * @return void |
||
485 | */ |
||
486 | 6 | public function offsetSet($offset, $value) |
|
490 | |||
491 | /** |
||
492 | * offsetUnset |
||
493 | * |
||
494 | * @param mixed $offset array key |
||
495 | * |
||
496 | * @return void |
||
497 | */ |
||
498 | 1 | public function offsetUnset($offset) |
|
504 | } |
||
505 |