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 XoopsBlockHandler 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 XoopsBlockHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class XoopsBlockHandler extends XoopsPersistableObjectHandler |
||
31 | { |
||
32 | /** |
||
33 | * Constructor |
||
34 | * |
||
35 | * @param Connection|null $db database |
||
36 | */ |
||
37 | 19 | public function __construct(Connection $db = null) |
|
41 | |||
42 | /** |
||
43 | * Insert a block |
||
44 | * |
||
45 | * @param XoopsBlock $obj block object to persist |
||
46 | * @param bool $force force insert even in 'safe' requests |
||
47 | * |
||
48 | * @return int|false id of insert, or false on error |
||
49 | */ |
||
50 | 1 | public function insertBlock(XoopsBlock $obj, $force = false) |
|
55 | |||
56 | /** |
||
57 | * Delete a ID from the database |
||
58 | * |
||
59 | * @param XoopsBlock $obj object to delete |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | 1 | public function deleteBlock(XoopsBlock $obj) |
|
82 | |||
83 | /** |
||
84 | * retrieve array of XoopsBlock objects meeting certain conditions |
||
85 | * |
||
86 | * @param CriteriaElement|null $criteria criteria to match |
||
87 | * @param bool $id_as_key should the blocks' bid be the key for the returned array? |
||
88 | * |
||
89 | * @return XoopsBlock[] |
||
90 | **/ |
||
91 | 2 | public function getDistinctObjects(CriteriaElement $criteria = null, $id_as_key = false) |
|
123 | |||
124 | /** |
||
125 | * get a list of blocks matching certain conditions |
||
126 | * |
||
127 | * @param CriteriaElement|null $criteria conditions to match |
||
128 | * |
||
129 | * @return array array of blocks matching the conditions |
||
130 | **/ |
||
131 | 1 | View Code Duplication | public function getNameList(CriteriaElement $criteria = null) |
141 | |||
142 | /** |
||
143 | * get all the blocks that match the supplied parameters |
||
144 | * |
||
145 | * @param int|int[] $groupid groupid (can be an array) |
||
146 | * @param bool $asobject retrieve as objects |
||
147 | * @param int $side values: |
||
148 | * 0: sideblock - left |
||
149 | * 1: sideblock - right |
||
150 | * 2: sideblock - left and right |
||
151 | * 3: centerblock - left |
||
152 | * 4: centerblock - right |
||
153 | * 5: centerblock - center |
||
154 | * 6: centerblock - left, right, center |
||
155 | * @param int|null $visible 0: not visible 1: visible |
||
156 | * @param string $orderby order of the blocks |
||
157 | * @param int $isactive 1: active or 0:inactive blocks |
||
158 | * |
||
159 | * @return array of block objects |
||
160 | */ |
||
161 | 1 | public function getAllBlocksByGroup( |
|
222 | |||
223 | /** |
||
224 | * getAllBlocks matching selection criteria |
||
225 | * |
||
226 | * @param string $rettype what to return, values can be object, list or id |
||
227 | * @param int $side block location (side) |
||
228 | * @param int|null $visible null for all, 0 not visible, 1 for visible only |
||
229 | * @param string $orderby comma separated columns to order by |
||
230 | * @param int $isactive 1: active or 0:inactive blocks |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | 1 | public function getAllBlocks( |
|
235 | $rettype = "object", |
||
236 | $side = null, |
||
237 | $visible = null, |
||
238 | $orderby = "side,weight,bid", |
||
239 | $isactive = 1 |
||
240 | ) { |
||
241 | 1 | $ret = array(); |
|
242 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
243 | 1 | $eb = $qb->expr(); |
|
244 | |||
245 | 1 | $qb ->fromPrefix('system_block', null) |
|
246 | 1 | ->where($eb->eq('isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT))); |
|
247 | 1 | View Code Duplication | if (isset($side)) { |
248 | // get both sides in sidebox? (some themes need this) |
||
249 | 1 | if ($side == XOOPS_SIDEBLOCK_BOTH) { |
|
250 | 1 | $qb->andWhere($eb->in('side', array(0,1))); |
|
251 | } elseif ($side == XOOPS_CENTERBLOCK_ALL) { |
||
252 | $qb->andWhere($eb->in('side', array(3,4,5,7,8,9))); |
||
253 | } else { |
||
254 | $qb->andWhere($eb->eq('side', $qb->createNamedParameter($side, \PDO::PARAM_INT))); |
||
255 | } |
||
256 | } |
||
257 | 1 | if (isset($visible)) { |
|
258 | 1 | $qb->andWhere($eb->eq('visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT))); |
|
259 | } |
||
260 | 1 | $qb->orderBy($orderby); |
|
261 | 1 | switch ($rettype) { |
|
262 | 1 | View Code Duplication | case "object": |
263 | 1 | $qb->select('*'); |
|
264 | 1 | $result = $qb->execute(); |
|
265 | 1 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
266 | 1 | $ret[] = new XoopsBlock($myrow); |
|
267 | } |
||
268 | 1 | break; |
|
269 | 1 | case "list": |
|
270 | 1 | $qb->select('*'); |
|
271 | 1 | $result = $qb->execute(); |
|
272 | 1 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
273 | 1 | $block = new XoopsBlock($myrow); |
|
274 | 1 | $title = $block->getVar("title"); |
|
275 | 1 | $title = empty($title) ? $block->getVar("name") : $title; |
|
276 | 1 | $ret[$block->getVar("bid")] = $title; |
|
277 | } |
||
278 | 1 | break; |
|
279 | 1 | View Code Duplication | case "id": |
280 | 1 | $qb->select('bid'); |
|
281 | 1 | $result = $qb->execute(); |
|
282 | 1 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
283 | 1 | $ret[] = $myrow['bid']; |
|
284 | } |
||
285 | 1 | break; |
|
286 | } |
||
287 | |||
288 | 1 | return $ret; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * get blocks by module id |
||
293 | * |
||
294 | * @param int $moduleid module id |
||
295 | * @param bool $asobject true to fetch as objects, otherwise associative array |
||
296 | * |
||
297 | * @return array of block information |
||
298 | */ |
||
299 | 1 | public function getByModule($moduleid, $asobject = true) |
|
323 | |||
324 | /** |
||
325 | * XoopsBlock::getAllByGroupModule() |
||
326 | * |
||
327 | * @param mixed $groupid int group id, int[] of group ids, |
||
328 | * @param integer $module_id module id |
||
329 | * @param boolean $toponlyblock only for top block |
||
330 | * @param mixed $visible restrict by visible values |
||
331 | * @param string $orderby comma separated list of columns to order by |
||
332 | * @param integer $isactive restrict by isactive values |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | 3 | public function getAllByGroupModule( |
|
410 | |||
411 | /** |
||
412 | * XoopsBlock::getNonGroupedBlocks() |
||
413 | * |
||
414 | * @param integer $module_id module id |
||
415 | * @param boolean $toponlyblock only for top block |
||
416 | * @param mixed $visible restrict by visible values |
||
417 | * @param string $orderby comma separated list of columns to order by |
||
418 | * @param integer $isactive restrict by isactive values |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | 1 | public function getNonGroupedBlocks( |
|
495 | |||
496 | /** |
||
497 | * XoopsBlock::countSimilarBlocks() |
||
498 | * |
||
499 | * @param int $moduleId module id |
||
500 | * @param string $funcNum func number |
||
501 | * @param string $showFunc show function |
||
502 | * |
||
503 | * @return int count |
||
504 | */ |
||
505 | 1 | public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) |
|
532 | |||
533 | /** |
||
534 | * Aligns the content of a block |
||
535 | * |
||
536 | * @param integer $position order of content |
||
537 | * 0 -> content in DB is positioned before the original content |
||
538 | * 1 -> content in DB is positioned after the original content |
||
539 | * @param string $content content |
||
540 | * @param string $contentdb content from database |
||
541 | * |
||
542 | * @return string |
||
543 | */ |
||
544 | 1 | public function buildContent($position, $content = "", $contentdb = "") |
|
556 | |||
557 | /** |
||
558 | * Enter description here... appears to be unused? |
||
559 | * |
||
560 | * @param string $originaltitle original title |
||
561 | * @param string $newtitle new title |
||
562 | * |
||
563 | * @return string title winner of the title war? |
||
564 | */ |
||
565 | 1 | public function buildTitle($originaltitle, $newtitle = '') |
|
574 | |||
575 | /************ system ***************/ |
||
576 | |||
577 | /** |
||
578 | * get list of ids of block that a group has permission to views |
||
579 | * |
||
580 | * @param null|integer $groupid group |
||
581 | * |
||
582 | * @return int[] |
||
583 | */ |
||
584 | 1 | public function getBlockByPerm($groupid) |
|
611 | } |
||
612 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.