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:
| 1 | <?php |
||
| 40 | class XoopsModuleHandler extends XoopsPersistableObjectHandler |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * holds an array of cached module references, indexed by module id |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | * @access private |
||
| 47 | */ |
||
| 48 | private $cachedModulesByMid = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * holds an array of cached module references, indexed by module dirname |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | * @access private |
||
| 55 | */ |
||
| 56 | private $cachedModulesByDirname = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructor |
||
| 60 | * |
||
| 61 | * @param Connection|null $db database |
||
| 62 | */ |
||
| 63 | 8 | public function __construct(Connection $db = null) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Load a module from the database |
||
| 70 | * |
||
| 71 | * @param int $id ID of the module |
||
| 72 | * |
||
| 73 | * @return XoopsModule|bool on fail |
||
| 74 | */ |
||
| 75 | 2 | public function getById($id = null) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Load a module by its dirname |
||
| 96 | * |
||
| 97 | * @param string $dirname module directory name |
||
| 98 | * |
||
| 99 | * @return XoopsModule|bool FALSE on fail |
||
| 100 | */ |
||
| 101 | 5 | public function getByDirname($dirname) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Write a module to the database |
||
| 124 | * |
||
| 125 | * @param XoopsModule $module module to insert |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 1 | public function insertModule(XoopsModule $module) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Delete a module from the database |
||
| 149 | * |
||
| 150 | * @param XoopsModule $module module to delete |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 1 | public function deleteModule(XoopsModule $module) |
|
| 155 | { |
||
| 156 | 1 | if (!parent::delete($module)) { |
|
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | 1 | $mid = $module->getVar('mid'); |
|
| 161 | 1 | $dirname = $module->getVar('dirname'); |
|
| 162 | |||
| 163 | // delete admin and read permissions assigned for this module |
||
| 164 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
| 165 | 1 | $eb = $qb->expr(); |
|
| 166 | 1 | $qb ->deletePrefix('system_permission') |
|
| 167 | 1 | ->where( |
|
| 168 | 1 | $eb->orX( |
|
| 169 | 1 | $eb->eq('gperm_name', $eb->literal('module_admin')), |
|
| 170 | 1 | $eb->eq('gperm_name', $eb->literal('module_read')) |
|
| 171 | 1 | ) |
|
| 172 | 1 | ) |
|
| 173 | 1 | ->andWhere($eb->eq('gperm_itemid', ':itemid')) |
|
| 174 | 1 | ->setParameter(':itemid', $mid, \PDO::PARAM_INT) |
|
| 175 | 1 | ->execute(); |
|
| 176 | |||
| 177 | 1 | $qb->resetQueryParts(); // reset |
|
| 178 | 1 | $qb ->select('block_id') |
|
| 179 | 1 | ->fromPrefix('system_blockmodule', null) |
|
| 180 | 1 | ->where($eb->eq('module_id', ':mid')) |
|
| 181 | 1 | ->setParameter(':mid', $mid, \PDO::PARAM_INT); |
|
| 182 | 1 | $result = $qb->execute(); |
|
| 183 | 1 | $block_id_arr = array(); |
|
| 184 | 1 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
| 185 | array_push($block_id_arr, $myrow['block_id']); |
||
| 186 | } |
||
| 187 | |||
| 188 | 1 | foreach ($block_id_arr as $i) { |
|
| 189 | $qb->resetQueryParts(); // reset |
||
| 190 | $qb ->select('COUNT(*)') |
||
| 191 | ->fromPrefix('system_blockmodule', null) |
||
| 192 | ->where($eb->ne('module_id', ':mid')) |
||
| 193 | ->setParameter(':mid', $mid, \PDO::PARAM_INT) |
||
| 194 | ->andWhere($eb->eq('block_id', ':bid')) |
||
| 195 | ->setParameter(':bid', $i, \PDO::PARAM_INT); |
||
| 196 | $result = $qb->execute(); |
||
| 197 | $count = $result->fetchColumn(0); |
||
| 198 | |||
| 199 | if ($count > 0) { |
||
| 200 | // this block has other entries, so delete the entry for this module |
||
| 201 | $qb->resetQueryParts(); // reset |
||
| 202 | $qb ->deletePrefix('system_blockmodule') |
||
| 203 | ->where($eb->eq('module_id', ':mid')) |
||
| 204 | ->setParameter(':mid', $mid, \PDO::PARAM_INT) |
||
| 205 | ->andWhere($eb->eq('block_id', ':bid')) |
||
| 206 | ->setParameter(':bid', $i, \PDO::PARAM_INT) |
||
| 207 | ->execute(); |
||
| 208 | } else { |
||
| 209 | // this block does not have other entries, so disable the block and let it show |
||
| 210 | // on top page only. otherwise, this block will not display anymore on block admin page! |
||
| 211 | $qb->resetQueryParts(); // reset |
||
| 212 | $qb ->updatePrefix('system_block') |
||
| 213 | ->set('visible', ':notvisible') |
||
| 214 | ->where($eb->eq('bid', ':bid')) |
||
| 215 | ->setParameter(':bid', $i, \PDO::PARAM_INT) |
||
| 216 | ->setParameter(':notvisible', 0, \PDO::PARAM_INT) |
||
| 217 | ->execute(); |
||
| 218 | |||
| 219 | $qb->resetQueryParts(); // reset |
||
| 220 | $qb ->updatePrefix('system_blockmodule') |
||
| 221 | ->set('module_id', ':nomid') |
||
| 222 | ->where($eb->eq('module_id', ':mid')) |
||
| 223 | ->setParameter(':mid', $mid, \PDO::PARAM_INT) |
||
| 224 | ->setParameter(':nomid', -1, \PDO::PARAM_INT) |
||
| 225 | ->execute(); |
||
| 226 | } |
||
| 227 | 1 | } |
|
| 228 | |||
| 229 | 1 | if (!empty($this->cachedModulesByDirname[$dirname])) { |
|
| 230 | unset($this->cachedModulesByDirname[$dirname]); |
||
| 231 | } |
||
| 232 | 1 | if (!empty($this->cachedModulesByMid[$mid])) { |
|
| 233 | unset($this->cachedModulesByMid[$mid]); |
||
| 234 | } |
||
| 235 | 1 | $cache = \Xoops::getInstance()->cache(); |
|
| 236 | 1 | $cache->delete("system/module/id/{$mid}"); |
|
| 237 | 1 | $cache->delete("system/module/dirname/{$dirname}"); |
|
| 238 | 1 | $cache->delete("module/{$dirname}"); |
|
| 239 | 1 | return true; |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Load some modules |
||
| 244 | * |
||
| 245 | * @param CriteriaElement|null $criteria criteria to match |
||
| 246 | * @param boolean $id_as_key Use the ID as key into the array |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 9 | public function getObjectsArray(CriteriaElement $criteria = null, $id_as_key = false) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * returns an array of module names |
||
| 288 | * |
||
| 289 | * @param CriteriaElement|null $criteria criteria |
||
| 290 | * @param boolean $dirname_as_key true = array key is module directory |
||
| 291 | * false = array key is module id |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 1 | View Code Duplication | public function getNameList(CriteriaElement $criteria = null, $dirname_as_key = false) |
| 308 | } |
||
| 309 |
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 theSoncalls the wrong method in the parent class.