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 | 2 | public function create($mention_obj, $data) |
|
133 | |||
134 | /** |
||
135 | * Prepares the data send through Mentioning::create to be ready for the |
||
136 | * actual insert. |
||
137 | * |
||
138 | * @param mixed[] $data must contain uid, type and msg at a minimum |
||
139 | */ |
||
140 | 2 | protected function _prepareData($data) |
|
162 | |||
163 | /** |
||
164 | * Did you read the mention? Then let's move it to the graveyard. |
||
165 | * Used in Display.controller.php, it may be merged to action_updatestatus |
||
166 | * though that would require to add an optional parameter to avoid the redirect |
||
167 | * |
||
168 | * @param int $mention_id |
||
169 | * @return bool if successfully changed or not |
||
170 | */ |
||
171 | 1 | public function markread($mention_id) |
|
175 | |||
176 | /** |
||
177 | * Updating the status from the listing? |
||
178 | * |
||
179 | * @param int|int[] $items |
||
180 | * @param string $mark |
||
181 | * @return bool if successfully changed or not |
||
182 | */ |
||
183 | 1 | public function updateStatus($items, $mark) |
|
206 | |||
207 | /** |
||
208 | * Of the passed IDs returns those accessible to the user. |
||
209 | * |
||
210 | * @param int[] $mention_ids |
||
211 | * @param string $action |
||
212 | * @return int[] |
||
213 | */ |
||
214 | 1 | protected function _getAccessible($mention_ids, $action) |
|
238 | |||
239 | /** |
||
240 | * Check if the user can do what he is supposed to do, and validates the input. |
||
241 | */ |
||
242 | 2 | protected function _isValid() |
|
276 | |||
277 | /** |
||
278 | * Changes a specific mention status for a member. |
||
279 | * |
||
280 | * - Can be used to mark as read, new, deleted, etc |
||
281 | * - note that delete is a "soft-delete" because otherwise anyway we have to remember |
||
282 | * - when a user was already mentioned for a certain message (e.g. in case of editing) |
||
283 | * |
||
284 | * @package Mentions |
||
285 | * @param int|int[] $id_mentions the mention id in the db |
||
286 | * @param string $status status to update, 'new', 'read', 'deleted', 'unapproved' |
||
287 | * @return bool if successfully changed or not |
||
288 | */ |
||
289 | 1 | protected function _changeStatus($id_mentions, $status = 'read') |
|
312 | |||
313 | /** |
||
314 | * Updates the mention count as a result of an action, read, new, delete, etc |
||
315 | * |
||
316 | * @package Mentions |
||
317 | * @param int $status |
||
318 | * @param int $member_id |
||
319 | */ |
||
320 | 3 | View Code Duplication | protected function _updateMenuCount($status, $member_id) |
334 | } |
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.