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 ExtgalleryPersistableObjectHandler 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 ExtgalleryPersistableObjectHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ExtgalleryPersistableObjectHandler extends XoopsPersistableObjectHandler //XoopsObjectHandler |
||
|
|||
28 | { |
||
29 | /**#@+ |
||
30 | * Information about the class, the handler is managing |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | // public $table; |
||
35 | // public $keyName; |
||
36 | // public $className; |
||
37 | // public $identifierName; |
||
38 | /**#@-*/ |
||
39 | |||
40 | /** |
||
41 | * Constructor - called from child classes |
||
42 | * |
||
43 | * @param XoopsDatabase $db {@link XoopsDatabase} |
||
44 | * object |
||
45 | * @param string $tablename Name of database table |
||
46 | * @param string $classname Name of Class, this handler is managing |
||
47 | * @param string $keyname Name of the property, holding the key |
||
48 | * |
||
49 | * @param bool $idenfierName |
||
50 | * |
||
51 | */ |
||
52 | |||
53 | public function __construct(XoopsDatabase $db, $tablename, $classname, $keyname, $idenfierName = false) |
||
64 | |||
65 | /** |
||
66 | * create a new user |
||
67 | * |
||
68 | * @param bool $isNew Flag the new objects as "new"? |
||
69 | * |
||
70 | * @return XoopsObject |
||
71 | */ |
||
72 | |||
73 | public function create($isNew = true) |
||
82 | |||
83 | /** |
||
84 | * delete an object from the database |
||
85 | * |
||
86 | * @param mixed $id id of the object to delete |
||
87 | * @param bool $force |
||
88 | * @return bool FALSE if failed. |
||
89 | */ |
||
90 | public function deleteById($id, $force = false) |
||
113 | |||
114 | /** |
||
115 | * @param $fieldname |
||
116 | * @param $fieldvalue |
||
117 | * @param null $criteria |
||
118 | * @param bool $force |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function updateFieldValue($fieldname, $fieldvalue, $criteria = null, $force = true) |
||
135 | |||
136 | /** |
||
137 | * @param $data |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function _toObject($data) |
||
159 | |||
160 | /** |
||
161 | * @param $objects |
||
162 | * @param array $externalKeys |
||
163 | * @param string $format |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function objectToArray($objects, $externalKeys = array(), $format = 's') |
||
226 | |||
227 | /** |
||
228 | * @param $object |
||
229 | * @param string $format |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function objectToArrayWithoutExternalKey($object, $format = 's') |
||
245 | |||
246 | /** |
||
247 | * @param $fieldname |
||
248 | * @param $criteria |
||
249 | * @param string $op |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function updateCounter($fieldname, $criteria, $op = '+') |
||
264 | |||
265 | /** |
||
266 | * @param null|CriteriaElement $criteria |
||
267 | * @param string $sum |
||
268 | * |
||
269 | * @return array|int|string |
||
270 | */ |
||
271 | View Code Duplication | public function getSum(CriteriaElement $criteria = null, $sum = '*') |
|
306 | |||
307 | /** |
||
308 | * @param null|CriteriaElement $criteria |
||
309 | * @param string $max |
||
310 | * |
||
311 | * @return array|int|string |
||
312 | */ |
||
313 | View Code Duplication | public function getMax(CriteriaElement $criteria = null, $max = '*') |
|
348 | |||
349 | /** |
||
350 | * @param null|CriteriaElement $criteria |
||
351 | * @param string $avg |
||
352 | * |
||
353 | * @return int |
||
354 | */ |
||
355 | public function getAvg(CriteriaElement $criteria = null, $avg = '*') |
||
371 | |||
372 | /** |
||
373 | * @return mixed |
||
374 | */ |
||
375 | public function getInsertId() |
||
379 | } |
||
380 |
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.