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 namespace XoopsModules\Extcal; |
||
| 32 | class CategoryHandler extends ExtcalPersistableObjectHandler |
||
| 33 | { |
||
| 34 | public $_extcalPerm; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param $db |
||
| 38 | */ |
||
| 39 | public function __construct(\XoopsDatabase $db) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $data |
||
| 48 | * |
||
| 49 | * @return bool |
||
| 50 | */ |
||
| 51 | public function createCat($data) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param $catId |
||
| 94 | * @param $data |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function modifyCat($catId, $data) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param $catId |
||
| 108 | */ |
||
| 109 | public function deleteCat($catId) |
||
| 116 | |||
| 117 | // Return one cat selected by his id |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $catId |
||
| 121 | * @param bool $skipPerm |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function getCat($catId, $skipPerm = false) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $user |
||
| 142 | * @param string $perm |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function getAllCat($user, $perm = 'extcal_cat_view') |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $user |
||
| 158 | * @param string $perm |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function getAllCatById($user, $perm = 'all') |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param \CriteriaElement $criteria |
||
| 181 | * @param $user |
||
| 182 | * @param string $perm |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function addCatPermCriteria(\CriteriaElement $criteria, $user, $perm = 'extcal_cat_view') |
|
| 185 | { |
||
| 186 | $authorizedAccessCats = $this->_extcalPerm->getAuthorizedCat($user, 'extcal_cat_view'); |
||
| 187 | $count = count($authorizedAccessCats); |
||
| 188 | if ($count > 0) { |
||
| 189 | $in = '(' . $authorizedAccessCats[0]; |
||
| 190 | array_shift($authorizedAccessCats); |
||
| 191 | foreach ($authorizedAccessCats as $authorizedAccessCat) { |
||
| 192 | $in .= ',' . $authorizedAccessCat; |
||
| 193 | } |
||
| 194 | $in .= ')'; |
||
| 195 | $criteria->add(new \Criteria('cat_id', $in, 'IN')); |
||
| 196 | } else { |
||
| 197 | $criteria->add(new \Criteria('cat_id', '(0)', 'IN')); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param XoopsUser|string $xoopsUser |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function haveSubmitRight(&$xoopsUser) |
||
| 210 | } |
||
| 211 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.