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 |
||
20 | class Notifications extends AbstractModel |
||
21 | { |
||
22 | /** |
||
23 | * Instance manager |
||
24 | * |
||
25 | * @var Notifications |
||
26 | */ |
||
27 | protected static $_instance; |
||
28 | |||
29 | /** |
||
30 | * List of notifications to send |
||
31 | * |
||
32 | * @var \Notifications_Task[] |
||
33 | */ |
||
34 | protected $_to_send; |
||
35 | |||
36 | /** |
||
37 | * Available notification frequencies |
||
38 | * |
||
39 | * @var string[] |
||
40 | */ |
||
41 | protected $_notification_frequencies; |
||
42 | |||
43 | /** |
||
44 | * Available notification frequencies |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $_notifiers; |
||
49 | /** |
||
50 | * Notifications constructor. |
||
51 | * |
||
52 | * Registers the known notifications to the system, allows for integration to add more |
||
53 | * |
||
54 | * @param \Database $db |
||
55 | * @throws Elk_Exception |
||
56 | */ |
||
57 | public function __construct($db) |
||
69 | |||
70 | /** |
||
71 | * We hax a new notification to send out! |
||
72 | * |
||
73 | * @param \Notifications_Task $task |
||
74 | */ |
||
75 | 2 | public function add(\Notifications_Task $task) |
|
79 | |||
80 | /** |
||
81 | * Time to notify our beloved members! YAY! |
||
82 | */ |
||
83 | 2 | public function send() |
|
105 | |||
106 | /** |
||
107 | * Function to register any new notification method. |
||
108 | * |
||
109 | * @param int $id This shall be a unique integer representing the notification method. |
||
110 | * |
||
111 | * <b>WARNING for addons developers</b>: note that this has to be **unique** across |
||
112 | * addons, so if you develop an addon that extends notifications, please verify this id |
||
113 | * has not been "taken" by someone else! 1-4 are reserved system notifications. |
||
114 | * @param int $key The string name identifying the notification method |
||
115 | * |
||
116 | * - _send_email |
||
117 | * - _send_daily_email |
||
118 | * - _send_weekly_email |
||
119 | * @param mixed|mixed[] $callback A callable function/array/whatever that will take care |
||
120 | * of sending the notification |
||
121 | * @param null|string[] $lang_data For the moment an array containing at least: |
||
122 | * |
||
123 | * - 'subject' => 'something' |
||
124 | * - 'body' => 'something_else' |
||
125 | * - 'suffix' => true/false |
||
126 | * |
||
127 | * Used to identify the strings for the subject and body respectively of the notification. |
||
128 | * @throws Elk_Exception |
||
129 | */ |
||
130 | public function register($id, $key, $callback, $lang_data = null) |
||
146 | |||
147 | /** |
||
148 | * Returns the notifications in the system, daily, weekly, etc |
||
149 | * |
||
150 | * @return string[] |
||
151 | */ |
||
152 | 1 | public function getNotifiers() |
|
156 | |||
157 | /** |
||
158 | * Process a certain task in order to send out the notifications. |
||
159 | * |
||
160 | * @param \Notifications_Task $task |
||
161 | */ |
||
162 | 2 | protected function _send_task(\Notifications_Task $task) |
|
193 | |||
194 | /** |
||
195 | * Inserts a new mention in the database (those that appear in the mentions area). |
||
196 | * |
||
197 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
198 | * @param \Notifications_Task $task |
||
199 | * @param mixed[] $bodies |
||
200 | */ |
||
201 | 2 | protected function _send_notification(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
|
216 | |||
217 | /** |
||
218 | * Sends an immediate email notification. |
||
219 | * |
||
220 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
221 | * @param \Notifications_Task $task |
||
222 | * @param mixed[] $bodies |
||
223 | */ |
||
224 | protected function _send_email(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
||
232 | |||
233 | /** |
||
234 | * Stores data in the database to send a daily digest. |
||
235 | * |
||
236 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
237 | * @param \Notifications_Task $task |
||
238 | * @param mixed[] $bodies |
||
239 | */ |
||
240 | View Code Duplication | protected function _send_daily_email(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
|
253 | |||
254 | /** |
||
255 | * Stores data in the database to send a weekly digest. |
||
256 | * |
||
257 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
258 | * @param \Notifications_Task $task |
||
259 | * @param mixed[] $bodies |
||
260 | */ |
||
261 | View Code Duplication | protected function _send_weekly_email(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
|
274 | |||
275 | /** |
||
276 | * Do the insert into the database for daily and weekly digests. |
||
277 | * |
||
278 | * @param mixed[] $insert_array |
||
279 | */ |
||
280 | protected function _insert_delayed($insert_array) |
||
295 | |||
296 | /** |
||
297 | * Loads from the database the notification preferences for a certain type |
||
298 | * of mention for a bunch of members. |
||
299 | * |
||
300 | * @param string[] $notification_frequencies |
||
301 | * @param string $notification_type |
||
302 | * @param int[] $members |
||
303 | */ |
||
304 | 2 | protected function _getNotificationPreferences($notification_frequencies, $notification_type, $members) |
|
353 | |||
354 | /** |
||
355 | * Singleton... until we have something better. |
||
356 | * |
||
357 | * @return Notifications |
||
358 | */ |
||
359 | 3 | public static function instance() |
|
368 | } |
||
369 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: