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 XoopsObjectHandler //XoopsPersistableObjectHandler |
||
|
|||
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 | public function __construct(XoopsDatabase $db, $tablename, $classname, $keyname, $idenfierName = false) |
||
63 | |||
64 | /** |
||
65 | * create a new user |
||
66 | * |
||
67 | * @param bool $isNew Flag the new objects as "new"? |
||
68 | * |
||
69 | * @return XoopsObject |
||
70 | */ |
||
71 | /* function &create($isNew = true) { |
||
72 | //DNPROSSI - 5.3.0 Assigning the return value of new by reference is deprecated PHP 5.3 |
||
73 | //Kept for backward compatability |
||
74 | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
||
75 | $obj = new $this->className(); |
||
76 | } else { |
||
77 | $obj = new $this->className(); |
||
78 | } |
||
79 | if ($isNew === true) { |
||
80 | $obj->setNew(); |
||
81 | } |
||
82 | |||
83 | return $obj; |
||
84 | } */ |
||
85 | |||
86 | public function create($isNew = true) |
||
95 | |||
96 | /** |
||
97 | * retrieve an object |
||
98 | * |
||
99 | * @param mixed $id ID of the object - or array of ids for joint keys. Joint keys MUST be given in the same order as in the constructor |
||
100 | * @param bool $as_object whether to return an object or an array |
||
101 | * @return mixed reference to the object, FALSE if failed |
||
102 | */ |
||
103 | public function get($id, $as_object = true) |
||
121 | |||
122 | /** |
||
123 | * retrieve objects from the database |
||
124 | * |
||
125 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
126 | * @param bool $id_as_key use the ID as key for the array? |
||
127 | * @param bool $as_object return an array of objects? |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true) |
||
132 | { |
||
133 | $ret = array(); |
||
134 | $limit = $start = 0; |
||
135 | $sql = 'SELECT * FROM ' . $this->table; |
||
136 | View Code Duplication | if (null !== $criteria && is_subclass_of($criteria, 'criteriaelement')) { |
|
137 | $sql .= ' ' . $criteria->renderWhere(); |
||
138 | if ($criteria->getSort() != '') { |
||
139 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
140 | } |
||
141 | $limit = $criteria->getLimit(); |
||
142 | $start = $criteria->getStart(); |
||
143 | } |
||
144 | $result = $this->db->query($sql, $limit, $start); |
||
145 | if (!$result) { |
||
146 | return $ret; |
||
147 | } |
||
148 | |||
149 | $ret = $this->convertResultSet($result, $id_as_key, $as_object); |
||
150 | |||
151 | return $ret; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Convert a database resultset to a returnable array |
||
156 | * |
||
157 | * @param XoopsObject $result database resultset |
||
158 | * @param bool $id_as_key - should NOT be used with joint keys |
||
159 | * @param bool $as_object |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function convertResultSet($result, $id_as_key = false, $as_object = true) |
||
197 | |||
198 | /** |
||
199 | * Retrieve a list of objects as arrays - DON'T USE WITH JOINT KEYS |
||
200 | * |
||
201 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
202 | * @param int $limit Max number of objects to fetch |
||
203 | * @param int $start Which record to start at |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0) |
||
244 | |||
245 | /** |
||
246 | * count objects matching a condition |
||
247 | * |
||
248 | * @param CriteriaElement $criteria {@link CriteriaElement} to match |
||
249 | * @return int|array count of objects |
||
250 | */ |
||
251 | View Code Duplication | public function getCount(CriteriaElement $criteria = null) |
|
285 | |||
286 | /** |
||
287 | * delete an object from the database |
||
288 | * |
||
289 | * @param int $id id of the object to delete |
||
290 | * @param bool $force |
||
291 | * @return bool FALSE if failed. |
||
292 | */ |
||
293 | public function delete($id, $force = false) |
||
316 | |||
317 | /** |
||
318 | * insert a new object in the database |
||
319 | * |
||
320 | * @param XoopsObject $obj reference to the object |
||
321 | * @param bool $force whether to force the query execution despite security settings |
||
322 | * @param bool $checkObject check if the object is dirty and clean the attributes |
||
323 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
324 | */ |
||
325 | |||
326 | public function insert(XoopsObject $obj, $force = false, $checkObject = true) |
||
402 | |||
403 | /** |
||
404 | * Change a value for objects with a certain criteria |
||
405 | * |
||
406 | * @param string $fieldname Name of the field |
||
407 | * @param string $fieldvalue Value to write |
||
408 | * @param CriteriaElement $criteria {@link CriteriaElement} |
||
409 | * |
||
410 | * @param bool $force |
||
411 | * @return bool |
||
412 | */ |
||
413 | public function updateAll($fieldname, $fieldvalue, $criteria = null, $force = false) |
||
434 | |||
435 | /** |
||
436 | * @param $fieldname |
||
437 | * @param $fieldvalue |
||
438 | * @param null $criteria |
||
439 | * @param bool $force |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function updateFieldValue($fieldname, $fieldvalue, $criteria = null, $force = true) |
||
456 | |||
457 | /** |
||
458 | * delete all objects meeting the conditions |
||
459 | * |
||
460 | * @param CriteriaElement $criteria {@link CriteriaElement} with conditions to meet |
||
461 | * @return bool |
||
462 | */ |
||
463 | |||
464 | public function deleteAll(CriteriaElement $criteria = null) |
||
479 | |||
480 | /** |
||
481 | * @param $data |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | public function _toObject($data) |
||
503 | |||
504 | /** |
||
505 | * @param $objects |
||
506 | * @param array $externalKeys |
||
507 | * @param string $format |
||
508 | * |
||
509 | * @return array |
||
510 | */ |
||
511 | public function objectToArray($objects, $externalKeys = array(), $format = 's') |
||
568 | |||
569 | /** |
||
570 | * @param $object |
||
571 | * @param string $format |
||
572 | * |
||
573 | * @return array |
||
574 | */ |
||
575 | public function objectToArrayWithoutExternalKey($object, $format = 's') |
||
587 | |||
588 | /** |
||
589 | * @param $fieldname |
||
590 | * @param $criteria |
||
591 | * @param string $op |
||
592 | * |
||
593 | * @return bool |
||
594 | */ |
||
595 | public function updateCounter($fieldname, $criteria, $op = '+') |
||
606 | |||
607 | /** |
||
608 | * @param null|CriteriaElement $criteria |
||
609 | * @param string $sum |
||
610 | * |
||
611 | * @return array|int|string |
||
612 | */ |
||
613 | View Code Duplication | public function getSum(CriteriaElement $criteria = null, $sum = '*') |
|
647 | |||
648 | /** |
||
649 | * @param null|CriteriaElement $criteria |
||
650 | * @param string $max |
||
651 | * |
||
652 | * @return array|int|string |
||
653 | */ |
||
654 | View Code Duplication | public function getMax(CriteriaElement $criteria = null, $max = '*') |
|
688 | |||
689 | /** |
||
690 | * @param null|CriteriaElement $criteria |
||
691 | * @param string $avg |
||
692 | * |
||
693 | * @return int |
||
694 | */ |
||
695 | public function getAvg(CriteriaElement $criteria = null, $avg = '*') |
||
711 | |||
712 | /** |
||
713 | * @return mixed |
||
714 | */ |
||
715 | public function getInsertId() |
||
719 | } |
||
720 |
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.