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 |
||
| 19 | class Mentioning extends AbstractModel |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Value assumed by a new mention |
||
| 23 | * |
||
| 24 | * @const int |
||
| 25 | */ |
||
| 26 | const MNEW = 0; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Value assumed by a mention that has been read |
||
| 30 | * |
||
| 31 | * @const int |
||
| 32 | */ |
||
| 33 | const READ = 1; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Value assumed by a mention that has been deleted |
||
| 37 | * |
||
| 38 | * @const int |
||
| 39 | */ |
||
| 40 | const DELETED = 2; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Value assumed by an unapproved mention |
||
| 44 | * |
||
| 45 | * @const int |
||
| 46 | */ |
||
| 47 | const UNAPPROVED = 3; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Will hold all available mention types |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $_known_mentions = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Will hold all available mention status |
||
| 58 | * 'new' => 0, 'read' => 1, 'deleted' => 2, 'unapproved' => 3, |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $_known_status = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Holds the instance of the data validation class |
||
| 66 | * |
||
| 67 | * @var object |
||
| 68 | */ |
||
| 69 | protected $_validator = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Holds the passed data for this instance, is passed through the validator |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $_data = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Mentioning constructor. |
||
| 80 | * |
||
| 81 | * @param Database $db |
||
| 82 | * @param Data_Validator $validator |
||
| 83 | * @param string $enabled_mentions |
||
| 84 | */ |
||
| 85 | 3 | public function __construct($db, $validator, $enabled_mentions = '') |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Inserts a new mention. |
||
| 103 | * |
||
| 104 | * @param ElkArte\sources\subs\MentionType\Mention_Type_Interface $mention_obj The object that knows how to store |
||
| 105 | * the mention in the database |
||
| 106 | * @param mixed[] $data must contain uid, type and msg at a minimum |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | 2 | public function create($mention_obj, $data) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Prepares the data send through Mentioning::create to be ready for the |
||
| 138 | * actual insert. |
||
| 139 | * |
||
| 140 | * @param mixed[] $data must contain uid, type and msg at a minimum |
||
| 141 | * |
||
| 142 | * @return array|mixed[] |
||
| 143 | */ |
||
| 144 | 2 | protected function _prepareData($data) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Did you read the mention? Then let's move it to the graveyard. |
||
| 169 | * Used in Display.controller.php, it may be merged to action_updatestatus |
||
| 170 | * though that would require to add an optional parameter to avoid the redirect |
||
| 171 | * |
||
| 172 | * @param int $mention_id |
||
| 173 | * @return bool if successfully changed or not |
||
| 174 | */ |
||
| 175 | 1 | public function markread($mention_id) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Updating the status from the listing? |
||
| 182 | * |
||
| 183 | * @param int|int[] $items |
||
| 184 | * @param string $mark |
||
| 185 | * @return bool if successfully changed or not |
||
| 186 | */ |
||
| 187 | 1 | public function updateStatus($items, $mark) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Of the passed IDs returns those accessible to the user. |
||
| 213 | * |
||
| 214 | * @param int[] $mention_ids |
||
| 215 | * @param string $action |
||
| 216 | * @return int[] |
||
| 217 | */ |
||
| 218 | 1 | protected function _getAccessible($mention_ids, $action) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Check if the user can do what he is supposed to do, and validates the input. |
||
| 245 | */ |
||
| 246 | 2 | protected function _isValid() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Changes a specific mention status for a member. |
||
| 283 | * |
||
| 284 | * - Can be used to mark as read, new, deleted, etc |
||
| 285 | * - note that delete is a "soft-delete" because otherwise anyway we have to remember |
||
| 286 | * - when a user was already mentioned for a certain message (e.g. in case of editing) |
||
| 287 | * |
||
| 288 | * @package Mentions |
||
| 289 | * @param int|int[] $id_mentions the mention id in the db |
||
| 290 | * @param string $status status to update, 'new', 'read', 'deleted', 'unapproved' |
||
| 291 | * @return bool if successfully changed or not |
||
| 292 | */ |
||
| 293 | 1 | protected function _changeStatus($id_mentions, $status = 'read') |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Updates the mention count as a result of an action, read, new, delete, etc |
||
| 319 | * |
||
| 320 | * @package Mentions |
||
| 321 | * @param int $status |
||
| 322 | * @param int $member_id |
||
| 323 | */ |
||
| 324 | 3 | View Code Duplication | protected function _updateMenuCount($status, $member_id) |
| 338 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.