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 |
||
| 12 | class shoutbox_listener implements \Symfony\Component\EventDispatcher\EventSubscriberInterface |
||
| 13 | { |
||
| 14 | /** @var \phpbb\config\config */ |
||
| 15 | private $config; |
||
| 16 | |||
| 17 | /** @var \phpbb\user */ |
||
| 18 | private $user; |
||
| 19 | |||
| 20 | /** @var \phpbb\template\template */ |
||
| 21 | private $template; |
||
| 22 | |||
| 23 | /** @var \phpbb\controller\helper */ |
||
| 24 | private $helper; |
||
| 25 | |||
| 26 | /** @var \phpbb\auth\auth */ |
||
| 27 | private $auth; |
||
| 28 | |||
| 29 | /** @var \phpbb\request\request */ |
||
| 30 | private $request; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param \phpbb\config\config $config |
||
| 34 | * @param \phpbb\user $user |
||
| 35 | * @param \phpbb\template\template $template |
||
| 36 | * @param \phpbb\controller\helper $helper |
||
| 37 | * @param \phpbb\auth\auth $auth |
||
| 38 | * @param \phpbb\request\request $request |
||
| 39 | */ |
||
| 40 | View Code Duplication | public function __construct(\phpbb\config\config $config, \phpbb\user $user, \phpbb\template\template $template, |
|
|
|
|||
| 41 | \phpbb\controller\helper $helper, \phpbb\auth\auth $auth, \phpbb\request\request $request) |
||
| 42 | { |
||
| 43 | $this->config = $config; |
||
| 44 | $this->user = $user; |
||
| 45 | $this->template = $template; |
||
| 46 | $this->helper = $helper; |
||
| 47 | $this->auth = $auth; |
||
| 48 | $this->request = $request; |
||
| 49 | } |
||
| 50 | |||
| 51 | static public function getSubscribedEvents() |
||
| 52 | { |
||
| 53 | return array( |
||
| 54 | 'core.index_modify_page_title' => 'index', |
||
| 55 | 'core.permissions' => 'add_permission', |
||
| 56 | 'forumhulp.cronstatus.modify_cron_task' => 'modify_cron_date', |
||
| 57 | 'boardtools.cronstatus.modify_cron_task'=> 'modify_cron_date', |
||
| 58 | 'core.ucp_prefs_personal_data' => 'update_ucp_personal_data', |
||
| 59 | 'core.ucp_prefs_personal_update_data' => 'ucp_prefs_personal_update_data', |
||
| 60 | 'core.user_add_modify_data' => 'user_add_modify_data', |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function user_add_modify_data($event) |
||
| 65 | { |
||
| 66 | $sql_ary = $event['sql_ary']; |
||
| 67 | $sql_ary['user_ajaxshoutbox_format'] = $this->config['ajaxshoutbox_date_format']; |
||
| 68 | |||
| 69 | $event['sql_ary'] = $sql_ary; |
||
| 70 | |||
| 71 | return $event; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function ucp_prefs_personal_update_data($event) |
||
| 75 | { |
||
| 76 | $sql_ary = $event['sql_ary']; |
||
| 77 | $sql_ary['user_ajaxshoutbox_format'] = $event['data']['ajaxshoutbox_format']; |
||
| 78 | |||
| 79 | $event['sql_ary'] = $sql_ary; |
||
| 80 | |||
| 81 | return $event; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function update_ucp_personal_data($event) |
||
| 85 | { |
||
| 86 | $data = $event['data']; |
||
| 87 | $data['ajaxshoutbox_format'] = $this->request->variable('ajaxshoutbox_format', $this->user->data['user_ajaxshoutbox_format']); |
||
| 88 | $event['data'] = $data; |
||
| 89 | |||
| 90 | if ($event['submit']) |
||
| 91 | { |
||
| 92 | if (version_compare(PHP_VERSION, '5.5.0') >= 0) |
||
| 93 | { |
||
| 94 | $error = validate_data($data, array( |
||
| 95 | 'ajaxshoutbox_format' => array('timezone'), |
||
| 96 | )); |
||
| 97 | |||
| 98 | if (sizeof($error) > 0) |
||
| 99 | { |
||
| 100 | if (isset($event['error'])) |
||
| 101 | { |
||
| 102 | |||
| 103 | $event['error'] = array_merge($event['error'], $error); |
||
| 104 | } |
||
| 105 | else |
||
| 106 | { |
||
| 107 | // 3.1.3 and lower don't provide us with $error. |
||
| 108 | // Set submit to false instead. |
||
| 109 | $event['submit'] = false; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $dateformat_options = ''; |
||
| 116 | |||
| 117 | foreach ($this->user->lang['dateformats'] as $format => $null) |
||
| 118 | { |
||
| 119 | if (strpos($format, '|') === 0) |
||
| 120 | { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | $dateformat_options .= '<option value="' . $format . '"' . (($format == $data['ajaxshoutbox_format']) ? ' selected="selected"' : '') . '>'; |
||
| 124 | $dateformat_options .= $this->user->format_date(time(), $format, false) . ((strpos($format, '|') !== false) ? $this->user->lang['VARIANT_DATE_SEPARATOR'] . $this->user->format_date(time(), $format, true) : ''); |
||
| 125 | $dateformat_options .= '</option>'; |
||
| 126 | } |
||
| 127 | |||
| 128 | $dateformat_options .= '<option value="custom"'; |
||
| 129 | if (!isset($this->user->lang['dateformats'][$data['ajaxshoutbox_format']])) |
||
| 130 | { |
||
| 131 | $dateformat_options .= ' selected="selected"'; |
||
| 132 | } |
||
| 133 | $dateformat_options .= '>' . $this->user->lang['CUSTOM_DATEFORMAT'] . '</option>'; |
||
| 134 | |||
| 135 | $this->user->add_lang_ext("paul999/ajaxshoutbox", "ajax_shoutbox"); |
||
| 136 | |||
| 137 | $this->template->assign_vars(array( |
||
| 138 | 'S_SHOUTBOX_DATEFORMAT_OPTIONS' => $dateformat_options, |
||
| 139 | 'AJAXSHOUTBOX_DATE_FORMAT' => $data['ajaxshoutbox_format'], |
||
| 140 | 'A_AJAXSHOUTBOX_DATE_FORMAT' => addslashes($data['ajaxshoutbox_format']), |
||
| 141 | )); |
||
| 142 | |||
| 143 | return $event; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function modify_cron_date($event) |
||
| 147 | { |
||
| 148 | if ($event['task_name'] == 'cron.task.shoutbox_prune') |
||
| 149 | { |
||
| 150 | $event['task_date'] = (int) $this->config['shoutbox_prune_gc']; |
||
| 151 | if ($event['task_date'] > 0) |
||
| 152 | { |
||
| 153 | $new_task = $event['task_date'] + 24 * 3600; |
||
| 154 | $event['new_task_date'] = $new_task; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | return $event; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * |
||
| 162 | */ |
||
| 163 | public function index() |
||
| 164 | { |
||
| 165 | $this->user->add_lang_ext('paul999/ajaxshoutbox', 'ajax_shoutbox'); |
||
| 166 | |||
| 167 | add_form_key('ajaxshoutbox_posting'); |
||
| 168 | |||
| 169 | $this->template->assign_vars( |
||
| 170 | array( |
||
| 171 | 'S_AJAX_SHOUTBOX' => $this->auth->acl_get('u_shoutbox_view'), |
||
| 172 | 'S_CAN_POST_SHOUT' => $this->auth->acl_get('u_shoutbox_post') && $this->user->data['user_id'] != ANONYMOUS, |
||
| 173 | 'U_SUBMIT_SHOUTBOX' => $this->helper->route('paul999_ajaxshoutbox_post'), |
||
| 174 | 'U_DELETE_SHOUTBOX' => $this->helper->route('paul999_ajaxshoutbox_delete'), |
||
| 175 | 'UA_GET_POST_ACTION' => htmlspecialchars($this->helper->route('paul999_ajaxshoutbox_get_all')), |
||
| 176 | 'UA_GET_POST_ACTION_NEW' => htmlspecialchars($this->helper->route('paul999_ajaxshoutbox_get_after', array('id' => 0))), |
||
| 177 | 'UA_GET_POST_ACTION_OLD' => htmlspecialchars($this->helper->route('paul999_ajaxshoutbox_get_before', array('id' => 0))), |
||
| 178 | ) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Add administrative permissions |
||
| 185 | * |
||
| 186 | * @param object $event The event object |
||
| 187 | * @return null |
||
| 188 | * @access public |
||
| 189 | */ |
||
| 190 | public function add_permission($event) |
||
| 204 | } |
||
| 205 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.