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 | 20 | 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) |
|
64 | { |
||
65 | 1 | if (!parent::delete($obj)) { |
|
66 | return false; |
||
67 | } |
||
68 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
69 | 1 | $eb = $qb->expr(); |
|
70 | 1 | $qb ->deletePrefix('system_permission', null) |
|
71 | 1 | ->where($eb->eq('gperm_name', $eb->literal('block_read'))) |
|
72 | 1 | ->andWhere($eb->eq('gperm_itemid', $qb->createNamedParameter($obj->getVar('bid'), \PDO::PARAM_INT))) |
|
73 | 1 | ->andWhere($eb->eq('gperm_modid', $qb->createNamedParameter(1, \PDO::PARAM_INT))) |
|
74 | 1 | ->execute(); |
|
75 | |||
76 | 1 | $qb ->deletePrefix('system_blockmodule', null) |
|
77 | 1 | ->where($eb->eq('block_id', $qb->createNamedParameter($obj->getVar('bid'), \PDO::PARAM_INT))) |
|
78 | 1 | ->execute(); |
|
79 | |||
80 | 1 | return true; |
|
81 | } |
||
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) |
|
92 | { |
||
93 | 2 | $ret = array(); |
|
94 | |||
95 | 2 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
96 | 2 | $eb = $qb->expr(); |
|
97 | 2 | $qb ->select('DISTINCT(b.bid)') |
|
98 | 2 | ->addSelect('b.*') |
|
99 | 2 | ->fromPrefix('system_block', 'b') |
|
100 | 2 | ->leftJoinPrefix('b', 'system_blockmodule', 'l', $eb->eq('b.bid', 'l.block_id')); |
|
101 | |||
102 | 2 | if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
|
103 | 1 | $criteria->renderQb($qb); |
|
104 | } |
||
105 | |||
106 | 2 | $result = $qb->execute(); |
|
107 | 2 | if (!$result) { |
|
108 | return $ret; |
||
109 | } |
||
110 | 2 | View Code Duplication | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
111 | 1 | $block = new XoopsBlock(); |
|
112 | 1 | $block->assignVars($myrow); |
|
113 | 1 | if (!$id_as_key) { |
|
114 | 1 | $ret[] = $block; |
|
115 | } else { |
||
116 | $ret[$myrow['bid']] = $block; |
||
117 | } |
||
118 | 1 | unset($block); |
|
119 | } |
||
120 | 2 | return $ret; |
|
121 | |||
122 | } |
||
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( |
|
162 | $groupid, |
||
163 | $asobject = true, |
||
164 | $side = null, |
||
165 | $visible = null, |
||
166 | $orderby = "b.weight,b.bid", |
||
167 | $isactive = 1 |
||
168 | ) { |
||
169 | 1 | $ret = array(); |
|
170 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
171 | 1 | $eb = $qb->expr(); |
|
172 | 1 | if ($asobject) { |
|
173 | 1 | $qb ->select('b.*'); |
|
174 | } else { |
||
175 | 1 | $qb ->select('b.bid'); |
|
176 | } |
||
177 | 1 | $qb ->fromPrefix('system_block', 'b') |
|
178 | 1 | ->leftJoinPrefix('b', 'system_permission', 'l', $eb->eq('b.bid', 'l.gperm_itemid')) |
|
179 | 1 | ->where($eb->eq('gperm_name', $eb->literal('block_read'))) |
|
180 | 1 | ->andWhere($eb->eq('gperm_modid', 1)); |
|
181 | |||
182 | 1 | if (is_array($groupid)) { |
|
183 | 1 | if (count($groupid) > 1) { |
|
184 | 1 | $in=array(); |
|
185 | 1 | foreach ($groupid as $gid) { |
|
186 | 1 | $in[] = $qb->createNamedParameter($gid, \PDO::PARAM_INT); |
|
187 | } |
||
188 | 1 | $qb->andWhere($eb->in('l.gperm_groupid', $in)); |
|
189 | } |
||
190 | } else { |
||
191 | 1 | $qb->andWhere($eb->eq('l.gperm_groupid', $qb->createNamedParameter($groupid, \PDO::PARAM_INT))); |
|
192 | } |
||
193 | 1 | $qb->andWhere($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT))); |
|
194 | 1 | View Code Duplication | if (isset($side)) { |
195 | // get both sides in sidebox? (some themes need this) |
||
196 | 1 | if ($side == XOOPS_SIDEBLOCK_BOTH) { |
|
197 | 1 | $qb->andWhere($eb->in('b.side', array(0,1))); |
|
198 | 1 | } elseif ($side == XOOPS_CENTERBLOCK_ALL) { |
|
199 | 1 | $qb->andWhere($eb->in('b.side', array(3,4,5,7,8,9))); |
|
200 | } else { |
||
201 | 1 | $qb->andWhere($eb->eq('b.side', $qb->createNamedParameter($side, \PDO::PARAM_INT))); |
|
202 | } |
||
203 | } |
||
204 | 1 | if (isset($visible)) { |
|
205 | 1 | $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT))); |
|
206 | } |
||
207 | 1 | $qb->orderBy($orderby); |
|
208 | 1 | $result = $qb->execute(); |
|
209 | 1 | $added = array(); |
|
210 | 1 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
211 | 1 | if (!in_array($myrow['bid'], $added)) { |
|
212 | 1 | if (!$asobject) { |
|
213 | 1 | $ret[] = $myrow['bid']; |
|
214 | } else { |
||
215 | 1 | $ret[] = new XoopsBlock($myrow); |
|
216 | } |
||
217 | 1 | array_push($added, $myrow['bid']); |
|
218 | } |
||
219 | } |
||
220 | 1 | return $ret; |
|
221 | } |
||
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 | 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( |
|
337 | $groupid, |
||
338 | $module_id = 0, |
||
339 | $toponlyblock = false, |
||
340 | $visible = null, |
||
341 | $orderby = 'b.weight, m.block_id', |
||
342 | $isactive = 1 |
||
343 | ) { |
||
344 | 3 | $ret = array(); |
|
345 | |||
346 | 3 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
347 | 3 | $eb = $qb->expr(); |
|
348 | |||
349 | 3 | $blockids=null; |
|
350 | 3 | if (isset($groupid)) { |
|
351 | 3 | $qb ->select('DISTINCT gperm_itemid') |
|
352 | 3 | ->fromPrefix('system_permission', null) |
|
353 | 3 | ->where($eb->eq('gperm_name', $eb->literal('block_read'))) |
|
354 | 3 | ->andWhere('gperm_modid=1'); |
|
355 | |||
356 | 3 | View Code Duplication | if (is_array($groupid) && !empty($groupid)) { |
357 | 3 | $qb->andWhere($eb->in('gperm_groupid', $groupid)); |
|
358 | } else { |
||
359 | 1 | if ((int)($groupid) > 0) { |
|
360 | 1 | $qb->andWhere($eb->eq('gperm_groupid', $groupid)); |
|
361 | } |
||
362 | } |
||
363 | 3 | $result = $qb->execute(); |
|
364 | 3 | $blockids = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
365 | } |
||
366 | |||
367 | 3 | $qb->resetQueryParts(); |
|
368 | |||
369 | 3 | $qb ->select('b.*') |
|
370 | 3 | ->fromPrefix('system_block', 'b') |
|
371 | 3 | ->where($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT))); |
|
372 | 3 | if (isset($visible)) { |
|
373 | 2 | $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT))); |
|
374 | } |
||
375 | 3 | View Code Duplication | if (isset($module_id)) { |
376 | 3 | $qb ->fromPrefix('system_blockmodule', 'm') |
|
377 | 3 | ->andWhere($eb->eq('m.block_id', 'b.bid')); |
|
378 | 3 | if (!empty($module_id)) { |
|
379 | 1 | $in=array(); |
|
380 | 1 | $in[]=0; |
|
381 | 1 | $in[]=(int)($module_id); |
|
382 | 1 | if ($toponlyblock) { |
|
383 | 1 | $in[]=(int)(-1); |
|
384 | } |
||
385 | } else { |
||
386 | 3 | if ($toponlyblock) { |
|
387 | $in=array(0, -1); |
||
388 | } else { |
||
389 | 3 | $in=0; |
|
390 | } |
||
391 | } |
||
392 | 3 | if (is_array($in)) { |
|
393 | 1 | $qb->andWhere($eb->in('m.module_id', $in)); |
|
394 | } else { |
||
395 | 3 | $qb->andWhere($eb->eq('m.module_id', $in)); |
|
396 | } |
||
397 | } |
||
398 | 3 | if (!empty($blockids)) { |
|
399 | 3 | $qb->andWhere($eb->in('b.bid', $blockids)); |
|
400 | } |
||
401 | 3 | $qb->orderBy($orderby); |
|
402 | 3 | $result = $qb->execute(); |
|
403 | 3 | View Code Duplication | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
404 | 3 | $block = new XoopsBlock($myrow); |
|
405 | 3 | $ret[$myrow['bid']] = $block; |
|
406 | 3 | unset($block); |
|
407 | } |
||
408 | 3 | return $ret; |
|
409 | } |
||
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( |
|
423 | $module_id = 0, |
||
424 | $toponlyblock = false, |
||
425 | $visible = null, |
||
426 | $orderby = 'b.weight, m.block_id', |
||
427 | $isactive = 1 |
||
428 | ) { |
||
429 | 1 | $ret = array(); |
|
430 | |||
431 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
432 | 1 | $eb = $qb->expr(); |
|
433 | |||
434 | 1 | $qb ->select('DISTINCT(bid)') |
|
435 | 1 | ->fromPrefix('system_block', null); |
|
436 | 1 | $result = $qb->execute(); |
|
437 | 1 | $bids = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
438 | |||
439 | 1 | $qb->resetQueryParts(); |
|
440 | |||
441 | 1 | $qb ->select('DISTINCT(p.gperm_itemid)') |
|
442 | 1 | ->fromPrefix('system_permission', 'p') |
|
443 | 1 | ->fromPrefix('system_group', 'g') |
|
444 | 1 | ->where($eb->eq('g.groupid', 'p.gperm_groupid')) |
|
445 | 1 | ->andWhere($eb->eq('p.gperm_name', $eb->literal('block_read'))); |
|
446 | 1 | $result = $qb->execute(); |
|
447 | 1 | $grouped = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
448 | |||
449 | 1 | $non_grouped = array_diff($bids, $grouped); |
|
450 | |||
451 | 1 | if (!empty($non_grouped)) { |
|
452 | $qb->resetQueryParts(); |
||
453 | |||
454 | $qb ->select('b.*') |
||
455 | ->fromPrefix('system_block', 'b') |
||
456 | ->where($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT))); |
||
457 | if (isset($visible)) { |
||
458 | $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT))); |
||
459 | } |
||
460 | |||
461 | View Code Duplication | if (isset($module_id)) { |
|
462 | $qb ->fromPrefix('system_blockmodule', 'm') |
||
463 | ->andWhere($eb->eq('m.block_id', 'b.bid')); |
||
464 | if (!empty($module_id)) { |
||
465 | $in=array(); |
||
466 | $in[]=0; |
||
467 | $in[]=(int)($module_id); |
||
468 | if ($toponlyblock) { |
||
469 | $in[]=(int)(-1); |
||
470 | } |
||
471 | } else { |
||
472 | if ($toponlyblock) { |
||
473 | $in=array(0, -1); |
||
474 | } else { |
||
475 | $in=0; |
||
476 | } |
||
477 | } |
||
478 | if (is_array($in)) { |
||
479 | $qb->andWhere($eb->in('m.module_id', $in)); |
||
480 | } else { |
||
481 | $qb->andWhere($eb->eq('m.module_id', $in)); |
||
482 | } |
||
483 | } |
||
484 | $qb->andWhere($eb->in('b.bid', $non_grouped)); |
||
485 | $qb->orderBy($orderby); |
||
486 | $result = $qb->execute(); |
||
487 | View Code Duplication | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
488 | $block = new XoopsBlock($myrow); |
||
489 | $ret[$myrow['bid']] = $block; |
||
490 | unset($block); |
||
491 | } |
||
492 | } |
||
493 | 1 | return $ret; |
|
494 | } |
||
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.