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 | * additional filters registered dynamically by a child class object |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | private $filters = array(); |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | */ |
||
97 | public $plugin_path; |
||
98 | |||
99 | /** |
||
100 | * used for new/clone objects |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | 13 | public function setNew() |
|
108 | |||
109 | /** |
||
110 | * clear new flag |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | 8 | public function unsetNew() |
|
118 | |||
119 | /** |
||
120 | * check new flag |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | 12 | public function isNew() |
|
128 | |||
129 | /** |
||
130 | * mark modified objects as dirty |
||
131 | * |
||
132 | * used for modified objects only |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | 29 | public function setDirty() |
|
140 | |||
141 | /** |
||
142 | * cleaar dirty flag |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | 14 | public function unsetDirty() |
|
150 | |||
151 | /** |
||
152 | * check dirty flag |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | 14 | public function isDirty() |
|
160 | |||
161 | /** |
||
162 | * initialize variables for the object |
||
163 | * |
||
164 | * @param string $key key |
||
165 | * @param int $data_type set to one of Dtype::TYPE_XXX constants (set to Dtype::TYPE_OTHER |
||
166 | * if no data type checking nor text sanitizing is required) |
||
167 | * @param mixed $value value |
||
168 | * @param bool $required require html form input? |
||
169 | * @param mixed $maxlength for Dtype::TYPE_TEXT_BOX type only |
||
170 | * @param string $options does this data have any select options? |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | 273 | public function initVar($key, $data_type, $value = null, $required = false, $maxlength = null, $options = '') |
|
185 | |||
186 | /** |
||
187 | * assign a value to a variable |
||
188 | * |
||
189 | * @param string $key name of the variable to assign |
||
190 | * @param mixed $value value to assign |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 49 | public function assignVar($key, $value) |
|
200 | |||
201 | /** |
||
202 | * assign values to multiple variables in a batch |
||
203 | * |
||
204 | * @param array $var_arr associative array of values to assign |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | 40 | public function assignVars($var_arr) |
|
216 | |||
217 | /** |
||
218 | * assign a value to a variable |
||
219 | * |
||
220 | * @param string $key name of the variable to assign |
||
221 | * @param mixed $value value to assign |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 27 | public function setVar($key, $value) |
|
233 | |||
234 | /** |
||
235 | * assign values to multiple variables in a batch |
||
236 | * |
||
237 | * @param array $var_arr associative array of values to assign |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | 1 | public function setVars($var_arr) |
|
249 | |||
250 | /** |
||
251 | * unset variable(s) for the object |
||
252 | * |
||
253 | * @param mixed $var variable(s) |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | 1 | public function destroyVars($var) |
|
271 | |||
272 | /** |
||
273 | * Assign values to multiple variables in a batch |
||
274 | * |
||
275 | * Meant for a CGI context: |
||
276 | * - prefixed CGI args are considered save |
||
277 | * - avoids polluting of namespace with CGI args |
||
278 | * |
||
279 | * @param mixed $var_arr associative array of values to assign |
||
280 | * @param string $pref prefix (only keys starting with the prefix will be set) |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | 1 | public function setFormVars($var_arr = null, $pref = 'xo_') |
|
295 | |||
296 | /** |
||
297 | * returns all variables for the object |
||
298 | * |
||
299 | * @return array associative array of key->value pairs |
||
300 | */ |
||
301 | 26 | public function getVars() |
|
305 | |||
306 | /** |
||
307 | * Returns the values of the specified variables |
||
308 | * |
||
309 | * @param mixed $keys An array containing the names of the keys to retrieve, or null to get all of them |
||
310 | * @param string $format Format to use (see getVar) |
||
311 | * @param int $maxDepth Maximum level of recursion to use if some vars are objects themselves |
||
312 | * |
||
313 | * @return array associative array of key->value pairs |
||
314 | */ |
||
315 | 3 | public function getValues($keys = null, $format = Dtype::FORMAT_SHOW, $maxDepth = 1) |
|
338 | |||
339 | /** |
||
340 | * returns a specific variable for the object in a proper format |
||
341 | * |
||
342 | * @param string $key key of the object's variable to be returned |
||
343 | * @param string $format format to use for the output |
||
344 | * |
||
345 | * @return mixed formatted value of the variable |
||
346 | */ |
||
347 | 181 | public function getVar($key, $format = Dtype::FORMAT_SHOW) |
|
356 | |||
357 | /** |
||
358 | * clean values of all variables of the object for storage. |
||
359 | * |
||
360 | * @return bool true if successful |
||
361 | */ |
||
362 | 3 | public function cleanVars() |
|
380 | |||
381 | /** |
||
382 | * dynamically register additional filter for the object |
||
383 | * |
||
384 | * @param string $filtername name of the filter |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | public function registerFilter($filtername) |
||
392 | |||
393 | /** |
||
394 | * load all additional filters that have been registered to the object |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | private function internalLoadFilters() |
||
418 | |||
419 | /** |
||
420 | * load all local filters for the object |
||
421 | * |
||
422 | * Filter distribution: |
||
423 | * In each module folder there is a folder "filter" containing filter files with, |
||
424 | * filename: [name_of_target_class][.][function/action_name][.php]; |
||
425 | * function name: [dirname][_][name_of_target_class][_][function/action_name]; |
||
426 | * parameter: the target object |
||
427 | * |
||
428 | * @param string $method function or action name |
||
429 | * |
||
430 | * @return void |
||
431 | */ |
||
432 | public function loadFilters($method) |
||
452 | |||
453 | /** |
||
454 | * create a clone(copy) of the current object |
||
455 | * |
||
456 | * @return object clone |
||
457 | */ |
||
458 | 1 | public function xoopsClone() |
|
463 | |||
464 | /** |
||
465 | * Adjust a newly cloned object |
||
466 | */ |
||
467 | 1 | public function __clone() |
|
472 | |||
473 | /** |
||
474 | * add an error |
||
475 | * |
||
476 | * @param string $err_str to add |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | 2 | public function setErrors($err_str) |
|
488 | |||
489 | /** |
||
490 | * return the errors for this object as an array |
||
491 | * |
||
492 | * @return array an array of errors |
||
493 | */ |
||
494 | 14 | public function getErrors() |
|
498 | |||
499 | /** |
||
500 | * return the errors for this object as html |
||
501 | * |
||
502 | * @return string html listing the errors |
||
503 | * @todo remove hardcoded HTML strings |
||
504 | */ |
||
505 | 1 | public function getHtmlErrors() |
|
517 | |||
518 | /** |
||
519 | * toArray |
||
520 | * |
||
521 | * @deprecated |
||
522 | * @return array |
||
523 | */ |
||
524 | public function toArray() |
||
525 | { |
||
526 | return $this->getValues(); |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * ArrayAccess methods |
||
531 | */ |
||
532 | |||
533 | /** |
||
534 | * offsetExists |
||
535 | * |
||
536 | * @param mixed $offset array key |
||
537 | * |
||
538 | * @return bool true if offset exists |
||
539 | */ |
||
540 | 2 | public function offsetExists($offset) |
|
544 | |||
545 | /** |
||
546 | * offsetGet |
||
547 | * |
||
548 | * @param mixed $offset array key |
||
549 | * |
||
550 | * @return mixed value |
||
551 | */ |
||
552 | 2 | public function offsetGet($offset) |
|
556 | |||
557 | /** |
||
558 | * offsetSet |
||
559 | * |
||
560 | * @param mixed $offset array key |
||
561 | * @param mixed $value |
||
562 | * |
||
563 | * @return void |
||
564 | */ |
||
565 | 6 | public function offsetSet($offset, $value) |
|
569 | |||
570 | /** |
||
571 | * offsetUnset |
||
572 | * |
||
573 | * @param mixed $offset array key |
||
574 | * |
||
575 | * @return void |
||
576 | */ |
||
577 | 1 | public function offsetUnset($offset) |
|
583 | } |
||
584 |