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 | /** |
||
| 51 | * Disallows to register notification types with id < 5 |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $_protect_id = true; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Notifications constructor. |
||
| 59 | * |
||
| 60 | * Registers the known notifications to the system, allows for integration to add more |
||
| 61 | * |
||
| 62 | * @param \Database $db |
||
| 63 | * @throws Elk_Exception |
||
| 64 | */ |
||
| 65 | public function __construct($db) |
||
| 66 | { |
||
| 67 | parent::__construct($db); |
||
| 68 | |||
| 69 | $this->_protect_id = false; |
||
| 70 | |||
| 71 | // Let's register all the notifications we know by default |
||
| 72 | $this->register(1, 'notification', array($this, '_send_notification')); |
||
| 73 | $this->register(2, 'email', array($this, '_send_email'), array('subject' => 'subject', 'body' => 'body', 'suffix' => true)); |
||
|
|
|||
| 74 | $this->register(3, 'email_daily', array($this, '_send_daily_email'), array('subject' => 'subject', 'body' => 'snippet', 'suffix' => true)); |
||
| 75 | $this->register(4, 'email_weekly', array($this, '_send_weekly_email'), array('subject' => 'subject', 'body' => 'snippet', 'suffix' => true)); |
||
| 76 | $this->_protect_id = true; |
||
| 77 | |||
| 78 | call_integration_hook('integrate_notifications_methods', array($this)); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * We hax a new notification to send out! |
||
| 83 | * |
||
| 84 | * @param \Notifications_Task $task |
||
| 85 | */ |
||
| 86 | 2 | public function add(\Notifications_Task $task) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Time to notify our beloved members! YAY! |
||
| 93 | */ |
||
| 94 | 2 | public function send() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Function to register any new notification method. |
||
| 119 | * |
||
| 120 | * @param int $id This shall be a unique integer representing the notification method. |
||
| 121 | * |
||
| 122 | * <b>WARNING for addons developers</b>: note that this has to be **unique** across |
||
| 123 | * addons, so if you develop an addon that extends notifications, please verify this id |
||
| 124 | * has not been "taken" by someone else! 1-4 are reserved system notifications. |
||
| 125 | * @param int $key The string name identifying the notification method |
||
| 126 | * |
||
| 127 | * - _send_email |
||
| 128 | * - _send_daily_email |
||
| 129 | * - _send_weekly_email |
||
| 130 | * @param mixed|mixed[] $callback A callable function/array/whatever that will take care |
||
| 131 | * of sending the notification |
||
| 132 | * @param null|string[] $lang_data For the moment an array containing at least: |
||
| 133 | * |
||
| 134 | * - 'subject' => 'something' |
||
| 135 | * - 'body' => 'something_else' |
||
| 136 | * - 'suffix' => true/false |
||
| 137 | * |
||
| 138 | * Used to identify the strings for the subject and body respectively of the notification. |
||
| 139 | * @throws Elk_Exception |
||
| 140 | */ |
||
| 141 | public function register($id, $key, $callback, $lang_data = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the notifications in the system, daily, weekly, etc |
||
| 161 | * |
||
| 162 | * @return string[] |
||
| 163 | */ |
||
| 164 | 1 | public function getNotifiers() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Process a certain task in order to send out the notifications. |
||
| 171 | * |
||
| 172 | * @param \Notifications_Task $task |
||
| 173 | */ |
||
| 174 | 2 | protected function _send_task(\Notifications_Task $task) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Inserts a new mention in the database (those that appear in the mentions area). |
||
| 208 | * |
||
| 209 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
| 210 | * @param \Notifications_Task $task |
||
| 211 | * @param mixed[] $bodies |
||
| 212 | */ |
||
| 213 | 2 | protected function _send_notification(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Sends an immediate email notification. |
||
| 231 | * |
||
| 232 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
| 233 | * @param \Notifications_Task $task |
||
| 234 | * @param mixed[] $bodies |
||
| 235 | */ |
||
| 236 | protected function _send_email(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Stores data in the database to send a daily digest. |
||
| 247 | * |
||
| 248 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
| 249 | * @param \Notifications_Task $task |
||
| 250 | * @param mixed[] $bodies |
||
| 251 | */ |
||
| 252 | View Code Duplication | protected function _send_daily_email(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Stores data in the database to send a weekly digest. |
||
| 268 | * |
||
| 269 | * @param \ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj |
||
| 270 | * @param \Notifications_Task $task |
||
| 271 | * @param mixed[] $bodies |
||
| 272 | */ |
||
| 273 | View Code Duplication | protected function _send_weekly_email(\ElkArte\sources\subs\MentionType\Mention_Type_Interface $obj, \Notifications_Task $task, $bodies) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Do the insert into the database for daily and weekly digests. |
||
| 289 | * |
||
| 290 | * @param mixed[] $insert_array |
||
| 291 | */ |
||
| 292 | protected function _insert_delayed($insert_array) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Loads from the database the notification preferences for a certain type |
||
| 310 | * of mention for a bunch of members. |
||
| 311 | * |
||
| 312 | * @param string[] $notification_frequencies |
||
| 313 | * @param string $notification_type |
||
| 314 | * @param int[] $members |
||
| 315 | */ |
||
| 316 | 2 | protected function _getNotificationPreferences($notification_frequencies, $notification_type, $members) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Singleton... until we have something better. |
||
| 368 | * |
||
| 369 | * @return Notifications |
||
| 370 | */ |
||
| 371 | 3 | public static function instance() |
|
| 380 | } |
||
| 381 |
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: