Conditions | 22 |
Paths | 448 |
Total Lines | 140 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 22 | ||
Bugs | 8 | Features | 4 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
26 | function main($id, $mode) |
||
27 | { |
||
28 | global $user, $template, $request; |
||
29 | global $config, $phpbb_dispatcher, $phpbb_log; |
||
30 | |||
31 | $user->add_lang_ext("paul999/ajaxshoutbox", "acp_ajax_shoutbox"); |
||
32 | $user->add_lang('acp/board'); |
||
33 | |||
34 | $submit = $request->is_set('submit'); |
||
35 | |||
36 | $form_key = 'acp_shoutbox'; |
||
37 | add_form_key($form_key); |
||
38 | |||
39 | $display_vars = array( |
||
40 | 'title' => 'ACP_AJAXSHOUTBOX_SETTINGS', |
||
41 | 'vars' => array( |
||
42 | 'legend1' => 'ACP_AJAXSHOUTBOX_PRUNE', |
||
43 | 'ajaxshoutbox_enable_prune' => array('lang' => 'AJAXSHOUTBOX_ENABLE_PRUNE', 'validate' => 'bool', 'type' => 'radio:yes_no','explain' => false), |
||
44 | 'ajaxshoutbox_prune_days' => array('lang' => 'AJAXSHOUTBOX_PRUNE_DAYS', 'validate' => 'int', 'type' => 'number:1:9999','explain' => false, 'append' => ' ' . $user->lang['DAYS']), |
||
45 | 'legend2' => 'ACP_AJAXSHOUTBOX_SETTINGS', |
||
46 | 'ajaxshoutbox_date_format' => array('lang' => 'AJAXSHOUTBOX_DEFAULT_DATE_FORMAT', 'validate' => 'string', 'type' => 'custom', 'method' => 'dateformat_select', 'explain' => true), |
||
47 | |||
48 | 'legend4' => 'ACP_SUBMIT_CHANGES', |
||
49 | ) |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * Event to add and/or modify acp_board configurations |
||
54 | * |
||
55 | * @event paul999.ajaxshoutbox.shoutbox_config_edit_add |
||
56 | * @var array display_vars Array of config values to display and process |
||
57 | * @var string mode Mode of the config page we are displaying |
||
58 | * @var boolean submit Do we display the form or process the submission |
||
59 | * @since 1.0.0-b2 |
||
60 | */ |
||
61 | $vars = array('display_vars', 'mode', 'submit'); |
||
62 | extract($phpbb_dispatcher->trigger_event('paul999.ajaxshoutbox.shoutbox_config_edit_add', compact($vars))); |
||
63 | |||
64 | $this->new_config = $config; |
||
65 | // Copied from acp_board.php |
||
66 | $cfg_array = (isset($_REQUEST['config'])) ? utf8_normalize_nfc($request->variable('config', array('' => ''), true)) : $this->new_config; |
||
67 | $error = array(); |
||
68 | |||
69 | // We validate the complete config if wished |
||
70 | validate_config_vars($display_vars['vars'], $cfg_array, $error); |
||
71 | |||
72 | if ($submit && !check_form_key($form_key)) |
||
73 | { |
||
74 | $error[] = $user->lang['FORM_INVALID']; |
||
75 | } |
||
76 | // Do not write values if there is an error |
||
77 | if (sizeof($error)) |
||
78 | { |
||
79 | $submit = false; |
||
80 | } |
||
81 | |||
82 | // We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to... |
||
83 | foreach ($display_vars['vars'] as $config_name => $null) |
||
84 | { |
||
85 | if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false || $config_name == 'ajaxshoutbox_validation_id' || $config_name == 'ajaxshoutbox_push_disabled') |
||
86 | { |
||
87 | continue; |
||
88 | } |
||
89 | |||
90 | $this->new_config[$config_name] = $config_value = $cfg_array[$config_name]; |
||
91 | |||
92 | if ($submit) |
||
93 | { |
||
94 | $config->set($config_name, $config_value); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if ($submit) |
||
99 | { |
||
100 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_AJAX_SHOUTBOX_CONFIG_' . strtoupper($mode)); |
||
101 | |||
102 | $message = $user->lang('CONFIG_UPDATED'); |
||
103 | $message_type = E_USER_NOTICE; |
||
104 | |||
105 | trigger_error($message . adm_back_link($this->u_action), $message_type); |
||
106 | } |
||
107 | |||
108 | $this->tpl_name = 'acp_board'; |
||
109 | $this->page_title = $display_vars['title']; |
||
110 | |||
111 | $template->assign_vars(array( |
||
112 | 'L_TITLE' => $user->lang[$display_vars['title']], |
||
113 | 'L_TITLE_EXPLAIN' => $user->lang[$display_vars['title'] . '_EXPLAIN'], |
||
114 | |||
115 | 'S_ERROR' => (sizeof($error)) ? true : false, |
||
116 | 'ERROR_MSG' => implode('<br />', $error), |
||
117 | |||
118 | 'U_ACTION' => $this->u_action, |
||
119 | )); |
||
120 | |||
121 | // Output relevant page |
||
122 | foreach ($display_vars['vars'] as $config_key => $vars) |
||
123 | { |
||
124 | if (!is_array($vars) && strpos($config_key, 'legend') === false) |
||
125 | { |
||
126 | continue; |
||
127 | } |
||
128 | |||
129 | if (strpos($config_key, 'legend') !== false) |
||
130 | { |
||
131 | $template->assign_block_vars('options', array( |
||
132 | 'S_LEGEND' => true, |
||
133 | 'LEGEND' => (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars) |
||
134 | ); |
||
135 | |||
136 | continue; |
||
137 | } |
||
138 | |||
139 | $type = explode(':', $vars['type']); |
||
140 | |||
141 | $l_explain = ''; |
||
142 | if ($vars['explain'] && isset($user->lang[$vars['lang'] . '_EXPLAIN'])) |
||
143 | { |
||
144 | $l_explain = $user->lang[$vars['lang'] . '_EXPLAIN']; |
||
145 | } |
||
146 | |||
147 | $content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars); |
||
148 | |||
149 | if (empty($content)) |
||
150 | { |
||
151 | continue; |
||
152 | } |
||
153 | |||
154 | $template->assign_block_vars('options', array( |
||
155 | 'KEY' => $config_key, |
||
156 | 'TITLE' => (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'], |
||
157 | 'S_EXPLAIN' => $vars['explain'], |
||
158 | 'TITLE_EXPLAIN' => $l_explain, |
||
159 | 'CONTENT' => $content, |
||
160 | |||
161 | )); |
||
162 | |||
163 | unset($display_vars['vars'][$config_key]); |
||
164 | } |
||
165 | } |
||
166 | |||
211 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.